Cloudflare API Rate Limiting by ASN: A Staging Guide
An ASN-scoped Cloudflare rule can add a second rate limit to a login API that already has a per-IP limit. It counts matching requests by Cloudflare data center, visitor ID, and User-Agent. It throttles bursts within a single bucket without identifying the caller or replacing account-level protections.

A login API with only a per-IP limit can see repeated credential attempts without any one address hitting its threshold. An ASN-scoped Cloudflare rule can add a second limit on that route, counting matching requests by Cloudflare data center, visitor ID, and User-Agent. It throttles a burst within a single bucket; it does not identify the caller or replace account-level protections.

Match the control to the gap
| Threat | Impact | Control | Verification |
|---|---|---|---|
| Repeated requests from a selected network | Login load or credential guessing | Scope the rate limit to the route and ASN. Country and ASN describe the source network, not the caller; both can include legitimate users and miss traffic elsewhere. | Compare the same request from selected and unselected ASNs. |
| Repeated attempts within an application session | Abuse of a stateful flow | Session counting can cap requests within one session, but sessions may be absent or rotated. | Test with a stable session, a fresh session, and no session. |
| Bursts with a consistent client profile | Pressure on authentication services | Visitor ID plus User-Agent is a counting key, not an identity. User-Agent and JA3 values can be shared, and a changed key can split the count. | Hold the key steady for a burst and check ordinary clients for unintended blocks. |
Check your plan and choose a threshold
Use a proxied staging hostname and a test route that does not touch real accounts. The example uses Enterprise Advanced Rate Limiting for its custom header characteristic. Check the zone's entitlements in Cloudflare's rate limiting documentation and rule parameters. JA3 is not needed here; if you plan to use it elsewhere, check the Enterprise Bot Management requirements and confirm the characteristic is available on your zone.
Illustrative scenario: If a legitimate client sends six requests during a busy minute, a threshold of ten leaves four requests of headroom in that bucket. A retry storm can consume it quickly. Budgeting two staff hours at an internal rate of $75 per hour gives $150 for staging and rollback checks, excluding any Cloudflare charges. Use your own traffic and costs to make the production decision.
Staging example: add a second limit to login
1. Scope the match. Start with a staging login route that already has a per-IP limit. In the current dashboard, go to Security → Security rules → Create rule → Rate limiting rules. In the older layout, use Security → WAF → Rate limiting rules. Match the exact host, path, method, and selected ASNs. The expression below uses Cloudflare Rules language, with a space-separated ASN set.
2. Set the counting keys and action. Count by Cloudflare data center (cf.colo.id, required in the API), visitor ID (cf.unique_visitor_id, labeled IP with NAT support), and User-Agent. The match requires a nonempty User-Agent but does not require an application session. Use Block for this staging test. Log is not available on every plan, and a browser challenge is neither logging nor a safe default for machine clients.
Paste this rule into your Rulesets API workflow as the body of a POST to /zones/<ZONE_ID>/rulesets/<RULESET_ID>/rules, using the staging zone's http_ratelimit ruleset. Replace the hostname and documentation ASNs with your staging host and known test egress ASNs.
{
"description": "Staging login: selected ASN visitor and User-Agent limit",
"expression": "(http.host eq \"staging-api.example.com\" and http.request.uri.path eq \"/api/v1/login\" and http.request.method eq \"POST\" and ip.src.asnum in {64500 64501} and any(http.request.headers[\"user-agent\"][*] ne \"\"))",
"action": "block",
"ratelimit": {
"characteristics": ["cf.colo.id", "cf.unique_visitor_id", "http.request.headers[\"user-agent\"]"],
"period": 60,
"requests_per_period": 10,
"mitigation_timeout": 60
}
}3. Prove which requests get blocked. Record the normal response before enabling the rule. From a selected ASN, send a bounded burst of 20 requests within 60 seconds using the same client, stable visitor state, and one User-Agent. Keep the test in one Cloudflare data center. Look for an edge 429 and a Security Event tied to the new rule ID; a 429 from your origin or existing per-IP rule does not count. Counter updates can lag, so don't expect the eleventh request to be the first one blocked.
Repeat from an unselected ASN and against a different staging path. Neither test should trigger the new rule. After the mitigation window clears, repeat the matching burst without an application session identifier: it should still match and be throttled if the requests share a counting bucket. If they don't, investigate visitor grouping rather than assuming an IP fallback. A request with no User-Agent is outside this example's match; keep the existing limits in place.
Rehearse rollback before production
Save the new rule ID and keep an authenticated dashboard or API recovery route that does not depend on the protected login. In staging, disable only the new rule, rerun the matching test, and confirm that the baseline response returns and existing protections remain active. Re-enable it in staging only after that check passes.
If a partner needs an exception, narrow it to the specific host, path, method, and verified fixed egress. Put the exclusion in this rule rather than skipping protections for an entire partner ASN. Keep account-level throttling and authorization checks: traffic outside the match or spread across buckets can still reach the API.
Verification checklist
- Confirm the zone supports the counting keys and action.
- Check matching, nonmatching, and sessionless traffic against the new rule ID.
- Confirm legitimate clients can handle a
429without retrying immediately. - Verify rollback leaves other rules active and administrative access intact.
Next action: Pick one staging login route and record its baseline response before adding the rule.
Related resources
Frequently Asked Questions
Do I need a session cookie?
No. The rule counts by Cloudflare data center, visitor ID, and User-Agent, not by application session.
Does the rule block every request from a listed ASN?
No. Only requests that also match the host, path, method, and nonempty User-Agent conditions count toward the threshold in their bucket.
How do I verify that the new rule blocked a request?
Look for an edge 429 and a Security Event tied to the new rule ID. A 429 from the origin or the existing per-IP rule does not count.
How do I verify rollback in staging?
Disable only the new rule and rerun the matching test. Confirm that the baseline response returns and existing protections remain active.
Get the Weekly SMB AI & SaaS Brief
Practical software benchmarks, prompt templates, and ROI breakdowns delivered every Tuesday. No fluff, unsubscribe anytime.