Protect Your APIs with Cloudflare Rate Limiting: A Practical Guide
Cloudflare Rate Limiting lets you protect API endpoints at the edge. This guide shows how to configure thresholds, bursts, and custom responses, explains trade‑offs, and gives a step‑by‑step example to help you deploy the feature safely.
05 Aug 2026, 23:14 UTC

Why Rate Limiting Matters for APIs
When an API is exposed to the public internet, every request is a potential attack vector. A sudden surge of traffic—whether a malicious bot, a misbehaving client, or a simple traffic spike—can overwhelm your origin servers, drive up costs, and degrade service for legitimate users. Cloudflare’s Rate Limiting feature sits at the edge of the network, allowing you to define granular thresholds that protect your endpoints before they hit your infrastructure.
How Cloudflare Rate Limiting Works
At its core, Rate Limiting is a rule‑based system that tracks request counts per URL pattern, HTTP method, or IP range. When the number of requests exceeds a configured threshold within a specified time window, Cloudflare blocks or throttles further traffic. The edge enforcement means the origin server never sees the traffic that violates the rule, saving compute and bandwidth.
Key Configuration Options
- Threshold – Maximum requests allowed in the time window (e.g., 100 requests).
- Window – Duration of the counting period (e.g., 60 seconds).
- Burst – Extra capacity that can be used for short spikes beyond the steady threshold.
- Response – Custom HTTP status code and body (e.g., 429 with a JSON message).
- Matchers – URL pattern, HTTP method, IP range, or geographic location.
Worked Example: Protecting a REST Endpoint
Assume you have a public endpoint https://api.example.com/v1/users that accepts GET requests. You want to allow up to 200 requests per minute per IP, but also want to permit short bursts of up to 400 requests. Below is a concise configuration you can create via the Cloudflare dashboard or API.
# API endpoint to create a rate‑limiting rule
POST https://api.cloudflare.com/client/v4/zones/<zone_identifier>/rate_limiting/rules
{
"mode": "simulate", // Use "block" for production
"description": "Protect /v1/users GET",
"threshold": 200,
"window": 60,
"burst": 200,
"response": {
"status": 429,
"content": {
"type": "application/json",
"value": "{\"error\":\"Rate limit exceeded\"}"
}
},
"match": {
"url_pattern": "/v1/users*",
"http_method": "GET",
"ip_range": "*" // All IPs; replace with CIDR if needed
}
}
After creating the rule, you can verify it by sending a burst of requests from a test client. Observe the Requests graph in the Cloudflare dashboard: the edge should show a spike, but your origin logs should remain flat once the threshold is exceeded.
Trade‑Offs and Limitations
- Plan Restrictions: The default rate‑limiting engine is only available on Business or Enterprise plans. Smaller accounts may need to rely on page rules or custom code.
- Legitimate Traffic Impact: Setting a threshold too low can block real users, especially on high‑traffic sites or during promotional events. Test thresholds in simulate mode first.
- IP‑Based Counting: Rate limits are applied per IP address. If users share a proxy or NAT, they may hit the same limit. Consider combining with rate limit per token if your API uses authentication tokens.
- Post‑TLS Enforcement: Rate limiting is applied after the TLS handshake. The origin still sees the TLS handshake and any initial request metadata, but the heavy request body is blocked at the edge.
Best Practices for Production Use
- Start with Simulate Mode: Deploy the rule with
mode: "simulate"to see how many requests would be blocked without actually affecting traffic. - Use Gradual Rollout: Enable the rule for a subset of IP ranges or geographic regions first, then widen coverage.
- Combine with Other Firewall Features: Add IP allow/deny lists or geo‑blocking to reduce noise from known bad actors.
- Monitor and Adjust: Use the Cloudflare dashboard’s analytics to track blocked requests and tweak thresholds.
- Custom Response Messaging: Provide clear feedback to clients (e.g., JSON error body) so they can back‑off gracefully.
Actionable Checklist
- Confirm your Cloudflare plan supports Rate Limiting.
- Define the API endpoint(s) and desired threshold/window.
- Create a rule in
simulatemode and review the Requests graph. - Switch to
blockmode once satisfied. - Set up monitoring alerts for sudden spikes or high block rates.
- Document the rule configuration and rationale for future reference.
By leveraging Cloudflare’s edge‑based rate limiting, you can protect your APIs from abuse, reduce load on your origin, and provide a smoother experience for legitimate users—all without writing custom throttling logic.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.