| [ Web Proxy ] |
| Viewing: https://developers.cloudflare.com/waf/account/custom-rulesets/create-api/ | [Back] [Original] |
Note
This feature requires an Enterprise plan.
To deploy custom rules at the account level:
execute action.Use the Rulesets API to work with custom rulesets using the API.
If you are using Terraform, refer to WAF custom rules configuration using Terraform.
To deploy a custom ruleset, follow these general steps:
http_request_firewall_custom phase with one or more rules.http_request_firewall_custom phase.The following example creates a custom ruleset at the account level with a single rule in the rules array.
Required API token permissions
At least one of the following token permissions is required:Account WAF WriteAccount Rulesets Writecurl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/rulesets" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"description": "",
"kind": "custom",
"name": "My custom ruleset",
"rules": [
{
"description": "Challenge web traffic (not /api)",
"expression": "not starts_with(http.request.uri.path, \"/api/\")",
"action": "managed_challenge"
}
],
"phase": "http_request_firewall_custom"
}'
Save the ruleset ID in the response for the next step.
To deploy the custom ruleset, add a rule with "action": "execute" to the http_request_firewall_custom phase entry point ruleset.
Invoke the Get an account entry point ruleset operation to obtain the definition of the entry point ruleset for the http_request_firewall_custom phase. You will need the account ID for this task.
Required API token permissions
At least one of the following token permissions is required:Account WAF WriteAccount WAF ReadAccount Rulesets ReadAccount Rulesets Writecurl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/rulesets/phases/http_request_firewall_custom/entrypoint" \
--request GET \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"
{
"result": {
"description": "Account-level phase entry point",
"id": "<RULESET_ID>",
"kind": "root",
"last_updated": "2024-03-16T15:40:08.202335Z",
"name": "root",
"phase": "http_request_firewall_custom",
"rules": [
// ...
],
"version": "9"
},
"success": true,
"errors": [],
"messages": []
}
If the entry point ruleset already exists (that is, if you received a 200 OK status code and the ruleset definition), take note of the ruleset ID in the response. Then, invoke the Create an account ruleset rule operation to add an execute rule to the existing ruleset deploying the custom ruleset. By default, the rule will be added at the end of the list of rules already in the ruleset.
The following request creates a rule that executes the custom ruleset with ID <CUSTOM_RULESET_ID> for all Enterprise zones in the account:
Required API token permissions
At least one of the following token permissions is required:Account WAF WriteAccount Rulesets Writecurl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/rulesets/$RULESET_ID/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"description": "Execute custom ruleset",
"expression": "(cf.zone.plan eq \"ENT\")",
"action": "execute",
"action_parameters": {
"id": "<CUSTOM_RULESET_ID>"
},
"enabled": true
}'Caution
At the account level, you can only apply custom rulesets to incoming traffic of zones on an Enterprise plan. To enforce this requirement, you must include cf.zone.plan eq "ENT" in the expression of the execute rule deploying the custom ruleset.
If the entry point ruleset does not exist (that is, if you received a 404 Not Found status code in step 1), create it using the Create an account ruleset operation. Include a single rule in the rules array that executes the custom ruleset for all incoming requests of Enterprise zones in your account.
Required API token permissions
At least one of the following token permissions is required:Account WAF WriteAccount Rulesets Writecurl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/rulesets" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"description": "",
"kind": "root",
"name": "Account-level phase entry point",
"rules": [
{
"action": "execute",
"expression": "(cf.zone.plan eq \"ENT\")",
"action_parameters": {
"id": "<CUSTOM_RULESET_ID>"
}
}
],
"phase": "http_request_firewall_custom"
}'Use the different operations in the Rulesets API to work with the custom ruleset you created and deployed. The following table has a list of common tasks for working with custom rulesets at the account level:
| Task | Procedure |
|---|---|
| Get list of custom rulesets | Use the List account rulesets operation and search for rulesets with For more information, refer to List existing rulesets. |
| List all rules in a custom ruleset | Use the Get an account ruleset operation with the custom ruleset ID to obtain the list of configured rules and their IDs. For more information, refer to View a specific ruleset. |
| Update a custom rule | Use the Update an account ruleset rule operation. You will need to provide the custom ruleset ID and the rule ID. For more information, refer to Update a rule in a ruleset. |
| Delete a custom rule | Use the Delete an account ruleset rule operation. You will need to provide the custom ruleset ID and the rule ID. For more information, refer to Delete a rule in a ruleset. |
For instructions on creating a custom ruleset at the zone level via API, refer to Custom rulesets (zone level).
For more information on working with custom rulesets, refer to Work with custom rulesets in the Ruleset Engine documentation.
| Web Proxy Viewer | New URL | Original Page |