Add rules to a custom ruleset
Last updated Apr 16, 2026 To add rules to an existing custom ruleset, use the Update an account or zone ruleset operation and pass the rules in an array. Each rule has an expression and an action.
Choose the appropriate API method
The Update an account or zone ruleset operation replaces all the rules in the ruleset with the rules in the request. Use this API operation when you need to add or update several rules at once. This operation updates the ruleset version number only once.
Depending on the update you want to perform, consider using one of the following API operations instead:
- Add a single rule to an existing custom ruleset: Use the Create an account or zone ruleset rule operation. Refer to Add a rule to a ruleset for an example.
- Update a single rule in a custom ruleset: Use the Update an account or zone ruleset rule operation. Refer to Update a rule in a ruleset for an example.
If you are using Terraform, refer to WAF custom rules configuration using Terraform for examples of creating and deploying custom rulesets.
If you are using the Cloudflare dashboard, refer to Work with custom rulesets in the dashboard.
Add rules
The following request adds two rules to a custom ruleset at the account level with ID $RULESET_ID. These will be the only two rules in the ruleset.
The response will include the rule ID of the new rules in the id field.
<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>Mass URL Redirects Write</code>
- <code>Magic Firewall Write</code>
- <code>L4 DDoS Managed Ruleset Write</code>
- <code>Transform Rules Write</code>
- <code>Select Configuration Write</code>
- <code>Account WAF Write</code>
- <code>Account Rulesets Write</code>
- <code>Logs Write</code>
</details>
Update an account rulesetbash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/rulesets/$RULESET_ID" \
--request PUT \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"rules": [
{
"expression": "(ip.src.country in {\"GB\" \"FR\"} and cf.bot_management.score < 20 and not cf.bot_management.verified_bot)",
"action": "challenge",
"description": "challenge GB and FR based on bot score"
},
{
"expression": "not http.request.uri.path matches \"^/api/.*$\"",
"action": "challenge",
"description": "challenge not /api"
}
]
}'
{
"result": {
"id": "<CUSTOM_RULESET_ID>",
"name": "Custom Ruleset 1",
"kind": "custom",
"version": "2",
"rules": [
{
"id": "<CUSTOM_RULE_ID_1>",
"version": "1",
"action": "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": "2021-03-18T18:25:08.122758Z",
"ref": "<CUSTOM_RULE_REF_1>",
"enabled": true
},
{
"id": "<CUSTOM_RULE_ID_2>",
"version": "1",
"action": "challenge",
"expression": "not http.request.uri.path matches \"^/api/.*$\"",
"description": "challenge not /api",
"last_updated": "2021-03-18T18:25:08.122758Z",
"ref": "<CUSTOM_RULE_REF_2>",
"enabled": true
}
],
"last_updated": "2021-03-18T18:25:08.122758Z",
"phase": "http_request_firewall_custom"
},
"success": true,
"errors": [],
"messages": []
}
Update rules
To update one or more rules in a custom ruleset, use the Update an account or zone ruleset operation. Include the ID of the rules you want to modify in the rules array and add the fields you wish to update. The request replaces the entire ruleset with a new version. Therefore, you must include the ID of all the rules you wish to keep.
The following PUT request edits one rule in a custom ruleset at the account level and updates the execution order of the rules.
The response will include the modified custom ruleset. Note that the updated rule and ruleset version number increment.
<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>Mass URL Redirects Write</code>
- <code>Magic Firewall Write</code>
- <code>L4 DDoS Managed Ruleset Write</code>
- <code>Transform Rules Write</code>
- <code>Select Configuration Write</code>
- <code>Account WAF Write</code>
- <code>Account Rulesets Write</code>
- <code>Logs Write</code>
</details>
Update an account rulesetbash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/rulesets/$RULESET_ID" \
--request PUT \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"rules": [
{
"id": "<CUSTOM_RULE_ID_2>",
"expression": "not http.request.uri.path matches \"^/api/.*$\"",
"action": "js_challenge",
"description": "js_challenge when not /api"
},
{
"id": "<CUSTOM_RULE_ID_1>"
}
]
}'
{
"result": {
"id": "<CUSTOM_RULESET_ID>",
"name": "Custom Ruleset 1",
"kind": "custom",
"version": "3",
"rules": [
{
"id": "<CUSTOM_RULE_ID_2>",
"version": "2",
"action": "js_challenge",
"expression": "not http.request.uri.path matches \"^/api/.*$\"",
"description": "js_challenge when not /api",
"last_updated": "2021-03-18T18:30:08.122758Z",
"ref": "<CUSTOM_RULE_ID_2>",
"enabled": true
},
{
"id": "<CUSTOM_RULE_ID_1>",
"version": "1",
"action": "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": "2021-03-18T18:25:08.122758Z",
"ref": "<CUSTOM_RULE_ID_1>",
"enabled": true
}
],
"last_updated": "2021-03-18T18:30:08.122758Z",
"phase": "http_request_firewall_custom"
},
"success": true,
"errors": [],
"messages": []
}
Caution
The request above completely replaces the list of rules in the ruleset. If you omit an existing rule from the rules array, it will not appear in the new version of the ruleset.
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/ruleset-engine/custom-rulesets/add-rules-ruleset/#page","headline":"Add rules to a custom ruleset","description":"Add rules to an existing custom ruleset using the API.","url":"https://developers.cloudflare.com/ruleset-engine/custom-rulesets/add-rules-ruleset/","inLanguage":"en","image":"https://developers.cloudflare.com/ruleset-engine/custom-rulesets/add-rules-ruleset/og.png?v=a1e99c8df0b2d666","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 custom ruleset with the Rulesets API. title: Create a custom ruleset image: https://developers.cloudflare.com/ruleset-engine/custom-rulesets/create-custom-ruleset/og.png?v=7956e876705cecf1
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