Create a rate limiting rule via API
Use the Rulesets API to create a rate limiting rule via API.
A rate limiting rule is similar to a regular rule handled by the Ruleset Engine, but contains an additional ratelimit
object with the rate limiting configuration. Refer to Rate limiting parameters for more information on this field and its parameters.
You must deploy rate limiting rules to the http_ratelimit
phase entry point ruleset.
Rate limiting rules must appear at the end of the rules list.
To create a rate limiting rule for a zone, add a rule with a ratelimit
object to the http_ratelimit
phase entry point ruleset.
-
Invoke the Get a zone entry point ruleset operation to obtain the definition of the entry point ruleset for the
http_ratelimit
phase. You will need the zone ID for this task. -
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 a zone ruleset rule operation to add a rate limiting rule to the existing ruleset. Refer to the examples below for details. -
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 a zone ruleset operation. Include your rate limiting rule in therules
array. Refer to Create ruleset for an example.
This example adds a rate limiting rule to the http_ratelimit
phase entry point ruleset for the zone with ID {zone_id}
. The phase entry point ruleset already exists, with ID {ruleset_id}
.
To define a specific position for the new rule, include a position
object in the request body according to the guidelines in Change the order of a rule in a ruleset.
For instructions on creating an entry point ruleset and defining its rules using a single API call, refer to Add rules to phase entry point rulesets.
This example adds a rate limiting rule to the http_ratelimit
phase entry point ruleset for the zone with ID {zone_id}
. The phase entry point ruleset already exists, with ID {ruleset_id}
.
The new rule defines a custom response for requests blocked due to rate limiting.
To define a specific position for the new rule, include a position
object in the request body according to the guidelines in Change the order of a rule in a ruleset.
For instructions on creating an entry point ruleset and defining its rules using a single API call, refer to Add rules to phase entry point rulesets.
This example adds a rate limiting rule to the http_ratelimit
phase entry point ruleset for the zone with ID {zone_id}
. The phase entry point ruleset already exists, with ID {ruleset_id}
.
The new rule does not consider requests for cached assets when calculating the rate.
To define a specific position for the new rule, include a position
object in the request body according to the guidelines in Change the order of a rule in a ruleset.
For instructions on creating an entry point ruleset and defining its rules using a single API call, refer to Add rules to phase entry point rulesets.
This example adds a rate limiting rule to the http_ratelimit
phase entry point ruleset for the zone with ID {zone_id}
. The phase entry point ruleset already exists, with ID {ruleset_id}
.
The new rule is a complexity-based rate limiting rule that takes the my-score
HTTP response header into account to calculate a total complexity score for the client. The counter with the total score is updated when there is a match for the rate limiting rule’s counting expression (in this case, the same as the rule expression). When this total score becomes larger than 400
during a 60-second period, any later client requests will be blocked for a period of 600 seconds (10 minutes).
To define a specific position for the new rule, include a position
object in the request body according to the guidelines in Change the order of a rule in a ruleset.
For instructions on creating an entry point ruleset and defining its rules using a single API call, refer to Add rules to phase entry point rulesets.
Use the different operations in the Rulesets API to work with the rule you just created. The following table has a list of common tasks for working with rate limiting rules at the zone level:
Task | Procedure |
---|---|
List all rules in ruleset | Use the Get a zone entry point ruleset operation with the For more information, refer to View a specific ruleset. |
Update a rule | Use the Update a zone ruleset rule operation. You will need to provide the ruleset ID and the rule ID. To obtain these IDs, you can use the Get a zone entry point ruleset operation with the For more information, refer to Update a rule in a ruleset. |
Delete a rule | Use the Delete a zone ruleset rule operation. You will need to provide the ruleset ID and the rule ID. To obtain these IDs, you can use the Get a zone entry point ruleset operation with the For more information, refer to Delete a rule in a ruleset. |
These operations are covered in the Ruleset Engine documentation. The Ruleset Engine powers different Cloudflare products, including rate limiting rules.