Add a rule to a ruleset
Last updated Apr 16, 2026 Adds a single rule to an existing ruleset. Use this endpoint to add a rule without having to include all the existing ruleset rules in the request.
Use one of the following API endpoints:
- Create an account ruleset rule
POST /accounts/{account_id}/rulesets/{ruleset_id}/rules - Create a zone ruleset rule
POST /zones/{zone_id}/rulesets/{ruleset_id}/rules
Include the rule definition in the request body.
By default, the rule will be added to the end of the existing list of rules in the ruleset. To define a specific position for the rule, include a position object in the request body according to the guidelines in Change the order of a rule in a ruleset.
Invoking this method creates a new version of the ruleset.
Example
The following POST request adds a rule to ruleset $RULESET_ID of zone $ZONE_ID. The ruleset ID was previously obtained using the List zone rulesets operation, and corresponds to the entry point ruleset for the http_request_firewall_custom phase.
The response will include the complete ruleset after adding the rule.
<details>
<summary>
Required API token permissions
</summary>
At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required:
- <code>Response Compression Write</code>
- <code>Config Settings Write</code>
- <code>Dynamic URL Redirects Write</code>
- <code>Cache Settings Write</code>
- <code>Custom Errors Write</code>
- <code>Origin Write</code>
- <code>Managed headers Write</code>
- <code>Zone Transform Rules Write</code>
- <code>Mass URL Redirects Write</code>
- <code>Magic Firewall Write</code>
- <code>L4 DDoS Managed Ruleset Write</code>
- <code>HTTP DDoS Managed Ruleset Write</code>
- <code>Sanitize Write</code>
- <code>Transform Rules Write</code>
- <code>Select Configuration Write</code>
- <code>Bot Management Write</code>
- <code>Zone WAF Write</code>
- <code>Account WAF Write</code>
- <code>Account Rulesets Write</code>
- <code>Logs Write</code>
- <code>Logs Write</code>
</details>
Create a zone ruleset rulebash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"action": "js_challenge",
"expression": "(ip.src.country in {\"GB\" \"FR\"} and cf.bot_management.score < 20 and not cf.bot_management.verified_bot)",
"description": "challenge GB and FR based on bot score"
}'
{
"result": {
"id": "<RULESET_ID>",
"name": "Zone Ruleset 1",
"description": "My phase entry point ruleset at the zone level",
"kind": "zone",
"version": "11",
"rules": [
{
"id": "<RULE_ID_1>",
"version": "1",
"action": "challenge",
"expression": "not http.request.uri.path matches \"^/api/.*$\"",
"last_updated": "2023-11-23T11:36:24.192361Z",
"ref": "<RULE_REF_1>",
"enabled": true
},
{
"id": "<NEW_RULE_ID>",
"version": "1",
"action": "js_challenge",
"expression": "(ip.src.country in {\"GB\" \"FR\"} and cf.bot_management.score < 20 and not cf.bot_management.verified_bot)",
"description": "challenge GB and FR based on bot score",
"last_updated": "2024-06-22T12:35:58.144683Z",
"ref": "<NEW_RULE_REF>",
"enabled": true
}
],
"last_updated": "2024-06-22T12:35:58.144683Z",
"phase": "http_request_firewall_custom"
},
"success": true,
"errors": [],
"messages": []
}
Define the rule position in the ruleset
To define the position of the new rule in the ruleset, include a position object in the request, containing one of the following:
"before": "<RULE_ID>"— Places the rule before rule<RULE_ID>. Use this argument with an empty rule ID value ("") to set the rule as the first rule in the ruleset."after": "<RULE_ID>"— Places the rule after rule<RULE_ID>. Use this argument with an empty rule ID value ("") to set the rule as the last rule in the ruleset."index": <POSITION_NUMBER>— Places the rule in the exact position specified by the integer number<POSITION_NUMBER>. Position numbers start with1. Existing rules in the ruleset from the specified position number onward are shifted one position (no rule is overwritten). For example, when you place a rule in positionnusingindex, existing rules with indexn,n+1,n+2, and so on, are shifted one position — their new position will ben+1,n+2,n+3, and so forth. If the index is out of range, the method returns a400HTTP status code.
Important
You can only use one of the before, after, and index fields at a time.
For examples of using a position object, refer to Update a rule in a ruleset.
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/ruleset-engine/rulesets-api/add-rule/#page","headline":"Add a rule to a ruleset","description":"Add a single rule to an existing ruleset using the API.","url":"https://developers.cloudflare.com/ruleset-engine/rulesets-api/add-rule/","inLanguage":"en","image":"https://developers.cloudflare.com/ruleset-engine/rulesets-api/add-rule/og.png?v=5b6e455ec3fce8cf","dateModified":"2026-04-16","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
description: Create a new ruleset using the Rulesets API. title: Create a ruleset image: https://developers.cloudflare.com/ruleset-engine/rulesets-api/create/og.png?v=c403c538fee4a24b
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/ruleset-engine/llms.txt
Use this file to discover all available pages before exploring further.
Docs