Rate limiting best practices·续

POST https://api.store.com/merchant
Cookie: session_id=12345

Body:
{
  "action": "lookup_price",
  "product_id": 215
}

In this scenario, you could write a rule to limit the number of actions from individual sessions:

Setting Value
Matching criteria URI Path equals /merchant and JSON String action equals lookup_price
Expression http.request.uri.path eq "/merchant" and lookup_json_string(http.request.body.raw, "action") eq "lookup_price"
Counting characteristics Cookie (session_id)
Rate (Requests / Period) 10 requests / 2 minutes
Action Managed Challenge

This example rule requires Advanced Rate Limiting and payload inspection.

You could also limit the number of lookups of each product_id regardless of the client making the requests by deploying a rule like the following:

Setting Value
Matching criteria URI Path equals /merchant and JSON field action equals lookup_price
Expression http.request.uri.path eq "/merchant" and lookup_json_string(http.request.body.raw, "action") eq "lookup_price"
Counting characteristics JSON field (product_id)
Rate (Requests / Period) 50 requests / 10 seconds
Action Block

This example rule requires Advanced Rate Limiting and payload inspection.

Note

If the request body is not JSON, you can use the http.request.body.raw field and regular expressions (along with the matches operator) to achieve the same goal.

Limit requests from bots

A general approach to identify traffic from bots is to rate limit requests that trigger a large volume of 403 or 404 response status codes from the origin server. This usually indicates automated activity from scraping applications.

In this situation, you could configure a rule similar to the following:

Setting Value
Matching criteria Hostname equals example.com
Expression http.host eq "example.com"
Counting characteristics IP
Increment counter when Response Status Code is in (403, 404)
Counting expression http.response.code in {403 404}
Rate (Requests / Period) 5 requests / 3 minutes
Action Managed Challenge

This example rule requires a Business plan or above.

To control the rate of actions performed by automated sources, consider use rate limiting rules together with Bot Management. With Bot Management, you can use the bot score as part of the matching criteria to apply the rule only to automated or likely automated traffic. For example, you can use a maximum score (or threshold) of 30 for likely automated traffic and 10 for automated traffic.

If your application tracks sessions using a cookie, you can use the cookie to set the rate limiting context (that is, use it as a counting characteristic). By setting the rate limiting characteristic to Cookie, the rule will group together requests from different IP addresses but belonging to the same session, which is a common scenario when dealing with a bot network performing a distributed attack.

Rule #1

Setting Value
Matching criteria Bot Score less than 30 and URI Query String contains action=delete
Expression cf.bot_management.score lt 30 and http.request.uri.query contains "action=delete"
Counting characteristics Cookie (session_id)
Rate (Requests / Period) 10 requests / 1 minute
Action Managed Challenge

Rule #2

Setting Value
Matching criteria Bot Score less than 10 and URI Query String contains action=delete
Expression cf.bot_management.score lt 10 and http.request.uri.query contains "action=delete"
Counting characteristics Cookie (session_id)
Rate (Requests / Period) 20 requests / 5 minute
Action Block

These example rules require Advanced Rate Limiting and Bot Management.

If the application does not use a session cookie, you can use JA3 fingerprints to identify individual clients. A JA3 fingerprint is a unique identifier, available to customers with Bot Management, that allows Cloudflare to identify requests coming from the same client. All clients have an associated fingerprint, whether they are automated or not.

Setting Value
Matching criteria URI Path equals /merchant and Bot Score less than 10
Expression http.request.uri.path eq "/merchant" and cf.bot_management.score lt 10
Counting characteristics JA3 Fingerprint
Rate (Requests / Period) 10 requests / 1 minute
Action Managed Challenge

This example rule requires Advanced Rate Limiting and Bot Management.

Protecting REST APIs

APIs can put significant strain on the application backend because API requests can be expensive to compute or serve. These requests may also require complex operations (such as data processing and large data lookups) that, if abused, can eventually bring down an origin server.

Prevent volumetric attacks

Advanced Rate Limiting can mitigate many types of volumetric attacks, like DDoS attacks, mass assignment, and data exfiltration.

A common concern is to limit POST actions. For authenticated traffic, you can use API Discovery to identify a suitable rate of request per endpoint, and then create a rate limiting rule like the following:

Setting Value
Matching criteria URI Path equals /endpoint1 and Request Method equals POST
Expression http.request.uri.path eq "/endpoint1" and http.request.method eq "POST"
Counting characteristics Header (x-api-key)
Rate (Requests / Period) As suggested by API Discovery or assessed by analyzing past traffic.
Action Block

This example rule requires Advanced Rate Limiting. API Discovery requires an additional license.

The counting characteristic can be any header, key, token, cookie, query parameter, or even JSON body field, since some APIs include a session ID or user ID as part of the JSON body. Refer to the following sections for additional information:

Protect resources

GET requests can also create excessive strain on an application or have an impact on costly resources, such as bandwidth. For example, consider an application with a large amount of stored files (such as images) where clients can download a file by accessing their specific URL:

GET https://api.store.com/files/<FILE_ID>
Header: x-api-key=9375

You probably wish to limit the number of downloads to avoid abuse, but you do not want to write individual rules for each file, given the size of the data storage. In this case, you could write a rule such as the following:

Setting Value
Matching criteria Hostname equals api.example.com and Request Method equals GET
Expression http.host eq "api.example.com" and http.request.method eq "GET"
Counting characteristics Path
Rate (Requests / Period) As suggested by API Discovery or assessed by analyzing past traffic.
Action Block

This example rule requires Advanced Rate Limiting.

The rule defines a limit of 10 downloads in 10 minutes for every file under https://api.store.com/files/*. By using Path as the rule characteristic, you avoid having to write a new rule every time there is a new uploaded file with a different <FILE_ID>. With this rule, the rate is computed on every request, regardless of their source IP or session identifier.

You could also combine Path with the x-api-key header (or IP, if you do not have a key or token) to set the maximum number of downloads that a specific client, as identified by x-api-key, can make of a given file:

Setting Value
Matching criteria Hostname equals api.store.com and Request Method equals GET
Expression http.host eq "api.example.com" and http.request.method eq "GET"
Counting characteristics Path and Header (x-api-key)
Rate (Requests / Period) As suggested by API Discovery or assessed by analyzing past traffic.
Action Block

This example rule requires Advanced Rate Limiting.

Protecting GraphQL APIs

Preventing server overload for GraphQL APIs can be different from preventing overload for RESTful APIs. One of the biggest challenges posed by applications built on GraphQL is that a single path manages all queries to the server, and every request is usually a POST operation. This prevents different rate limits for different API use cases based on the HTTP method and URI path.

However, instead of using the method and path like a RESTful API, the purpose of the request is usually embedded in the body, which has information on what data the client wants to fetch or mutate (according to GraphQL's terminology ↗︎ for server-side data modification), along with any additional data required to carry out the action.

To prevent server overload, consider the following approaches:

  1. Limit the number of times a particular user can call the same GraphQL operation name.
  2. Limit the total amount of query complexity any given user is allowed to request.
  3. Limit any individual request's query complexity.

The following examples are based on an application that accepts reviews for movies. A GraphQL request could look like the following:

POST https://moviereviews.example.com/graphql
Cookie: session_id=12345

Body:
{
  "data": {
    "createReview": {
      "stars": 5,
      "commentary": "This is a great movie!"
    }
  }
}

Limit the number of operations

To limit the rate of actions, you could use the following rule:

Setting Value
Matching criteria URI Path equals /graphql and Body contains createReview
Expression http.request.uri.path eq "/graphql" and http.request.body.raw contains "createReview"
Counting characteristics Cookie (session_id)
Rate (Requests / Period) 5 requests / 1 hour
Action Block

This example rule requires Advanced Rate Limiting and payload inspection.

Limit the total amount of query complexity

The complexity necessary to handle a GraphQL request can vary significantly. Since the API uses a single endpoint, it is difficult to figure out the complexity of each request before it has been served.

To protect the origin server from resource exhaustion, rather than limiting the number of requests you need to limit the amount of complexity necessary to handle a single client over a period of time. Cloudflare Rate Limiting allows you to create rules that track complexity over time and block subsequent requests after reaching a complexity budget or limit.

This type of rate limiting requires that the server scores every served request according to the request's complexity. Additionally, the server must add this score to the response as an HTTP header. Then, the rate limiting mechanism will use this information to update the budget for that specific client.

For example, the following rule defines a total complexity budget of 1,000 per hour:

Setting Value
Matching criteria URI Path contains /graphql
Expression http.request.uri.path eq "/graphql"
Counting characteristics Cookie (session_id)
Score per period 1,000
Period 1 hour
Response header name score
Action Block

This example rule requires Advanced Rate Limiting and payload inspection.

When the origin server processes a request, it adds a score HTTP header to the response with a value representing how much work the origin has performed to handle it — for example, 100. In the next hour, the same client can perform requests up to an additional budget of 900. As soon as this budget is exceeded, later requests will be blocked until the timeout expires.

Limit any individual query’s complexity

API Shield customers can use GraphQL malicious query protection to protect their GraphQL APIs. GraphQL malicious query protection scans your GraphQL traffic for queries that could overload your origin and result in a denial of service. You can build rules that limit the query depth and size of incoming GraphQL queries in order to block suspiciously large or complex queries.

Refer to API Shield documentation ↗︎ for more information on GraphQL malicious query protection.

Footnotes

  1. The HTTP header name uses a misspelling of "referrer". ↩

Was this helpful?

YesNo

On this page

Rate limiting best practices·续 - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/waf/rate-limiting-rules/best-practices/#page","headline":"Rate limiting best practices","description":"Typical rate limiting configurations for login protection, API abuse, and more.","url":"https://developers.cloudflare.com/waf/rate-limiting-rules/best-practices/","inLanguage":"en","image":"https://developers.cloudflare.com/waf/rate-limiting-rules/best-practices/og.png?v=f868180d3560b699","dateModified":"2026-10-23","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/"},"keywords":["GraphQL","Account takeover","Authentication","Scraping"]}


description: Create zone-level rate limiting rules using the Rulesets API. title: Create a rate limiting rule via API image: https://developers.cloudflare.com/waf/rate-limiting-rules/create-api/og.png?v=826ea4630ff8d408


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/waf/llms.txt
Use this file to discover all available pages before exploring further.