Rate limiting method
Use therequest.limit_rate method to implement rate limiting:
limit_rate fixed method signature
tag parameter when configuring tag-based rate limiting rules.
The method returns true, and the enclosing rule condition is satisfied when the request count (4), under the granularity (8), exceeds the configured threshold within the specified time window (3), using the configured filters (1, 2, 5, 6, 7, 9).
| # | Parameter name | Required | Description |
|---|---|---|---|
| 1 | ip_list | False | List of IP addresses that the rule applies to. If there are no IPs in the list, counting will be done for all IPs. |
| 2 | url | True | A regex pattern used to match the request’s URI (URL + query_params). |
| 3 | interval | True | The time limit, in seconds, within which we only allow n number of requests to URI matching the pattern. |
| 4 | requests | True | The maximum number of requests accepted within the given period before an action is taken (minimum value is 21 requests). |
| 5 | method_list | False | List of method types the request aggregation will be applied to. |
| 6 | status_list | False | List of status codes the request aggregation will be applied to. |
| 7 | content_type | False | Regex pattern to match request content_type against. |
| 8 | scope (granularity) | False. If the granularity isn’t set to cluster, the default aggregation will be set to cluster. | When protecting the origin, you can configure rate limit rules that apply to either IPs or all requests that come through the cluster. If you set it to ip, once the IP exceeds the threshold, the rule is triggered ΓÇö this helps against individual attackers. If you set it to cluster, once the total requests from any IP exceed the threshold, the rule is triggered ΓÇö this helps protect the origin from getting too many requests. |
| 9 | tag | False | Aggregation of tagged (user-defined tags) requests will be applied for each IP. |
Implementation
An API token is required along with the ID of a WAAP-protected domain. For SDK examples, install the Python or Go SDK. Set these as environment variables before running any of the examples:Rate limiting with exclusions
Rate-limit IPs that exceed 200 requests in 5 seconds on any/events path, while excluding requests from known mobile and web clients that carry session cookies.
- Python SDK
- Go SDK
- curl
Best practices
Block IPs that exceed request limit for any URL
Each request is counted individually per IP. If1.2.3.4 sends more than 200 requests within 5 seconds, it is blocked. Another IP, 1.2.3.5, is only blocked when it independently exceeds the same threshold.
- Python SDK
- Go SDK
- curl
Embed additional conditions
Combine rate limiting with other request attributes. In this example, the block action is applied only toFirefox user-agent requests from specific IPs that exceed the rate limit. The rate limit itself still aggregates across all configured IPs regardless of user-agent.
- Python SDK
- Go SDK
- curl
Rate limit complex URL regex
Block requests to any URL that does not end in a known static asset extension ΓÇö images, fonts, scripts, archives. Requests to dynamic paths are rate-limited at 20 per 120 seconds per IP.- Python SDK
- Go SDK
- curl
Embed IP range to the condition
Rate-limit JPEG requests from a specific private IP range. Therequest.ip_in_range condition acts as a filter ΓÇö the rate limit aggregation still counts per IP within the range.
- Python SDK
- Go SDK
- curl
Cluster (PoP) granularity
Rate-limit allGET or HEAD requests that return a 302 redirect with a specific content type. The cluster scope counts requests globally across all IPs rather than per individual IP ΓÇö useful for protecting origin from aggregate traffic spikes.
- Python SDK
- Go SDK
- curl
Rate limit by tag filter
Apply rate limiting only to requests that carry a specific user-defined tag. The tag must be assigned by a separate tag rule that runs before this one. This allows targeting specific sessions or clients identified by earlier rules.- Python SDK
- Go SDK
- curl
Clarification: rate limit aggregation and rule triggering
Rate limit aggregation is defined exclusively by the parameters of therequest.limit_rate(...) condition, such as the aggregation scope, IP list, or tags.
When request.limit_rate(...) is combined with additional conditions in the same rule, those additional conditions control when the rule action is applied, not how requests are aggregated.
For example, if a rate limit condition is configured without an IP list and combined with a condition such as request.ip == '1.2.3.4', request aggregation will still occur according to the rate limit definition. However, the rule action will only be triggered when the full rule condition evaluates to true ΓÇö in this case, only for requests originating from 1.2.3.4.
This distinction is important when combining rate limiting with other conditions to help ensure the rule behaves as expected.