Protecting API Endpoints
API endpoints are particularly exposed to credential stuffing, rate limit bypass, and aggressive scraping.
Basic Rules
Section titled “Basic Rules”# Strict rate limit for POST /loginIF http.request.method == POSTAND http.request.base_uri == /loginTHEN ratelimit 20/60s
# CAPTCHA for API requests from hosting/datacenter IPsIF http.request.base_uri has_prefix /apiAND waf.client.ip_type == hostingTHEN captcha
# Increment failban counter on 401 responses (credential stuffing)IF http.response.status == 401THEN failbanWith these selections in the console:
- Rate limit the login — Field HTTP Method · Operator equals · Value
POSTAND Field Path (without query) · Operator equals · Value/login→ Action Rate Limit (20 requests / 60 s) - CAPTCHA for API requests from hosting IPs — Field Path (without query) · Operator starts with · Value
/apiAND Field IP Infrastructure Type · Operator equals · Valuehosting→ Action CAPTCHA - Count 401s toward auto-ban — Field Origin Response Status · Operator equals · Value
401→ Action Auto-Block
The /login endpoint is the primary target of credential stuffing attacks. A limit of 20 requests per 60 seconds covers normal legitimate user behavior while effectively stopping automated brute-force attempts.
Data center and hosting IPs rarely originate from end users using a real browser. Routing requests to API endpoints from these sources through CAPTCHA narrows the attack surface without accidentally blocking legitimate B2B partner systems.
failban increments a counter on every 401 Unauthorized response. IPs that exceed the threshold are automatically blocked. This mechanism is especially effective against credential stuffing tools that achieve partial success, since errors accumulate and eventually trigger the block.
Advanced Variant
Section titled “Advanced Variant”# Block requests missing an API keyIF http.request.base_uri has_prefix /apiAND http.header not_contains Authorization=THEN block
# Catch bots making very rapid sequential requestsIF http.request.base_uri has_prefix /apiAND waf.bot_score ge 70THEN captchaWith these selections in the console:
- Block requests without an API key — Field Path (without query) · Operator starts with · Value
/apiAND Field Header · Operator does not contain · ValueAuthorization=→ Action Block - CAPTCHA for high bot-score API traffic — Field Path (without query) · Operator starts with · Value
/apiAND Field Bot Score (0–100) · Operator greater or equal · Value70→ Action CAPTCHA
Blocking API requests that do not include an Authorization header cuts off the bulk of abuse before it ever reaches your authentication layer. Enable this only after confirming that all your clients send the Authorization header.
High bot-score API traffic is most likely a scraper or automated testing tool. Combining this layer with the basic rate limit rules above provides defense-in-depth against attacks.
- Use Bypass Rate Limit for trusted partner IPs
- Combining Auto-Block with an Origin Response Status == 401 condition is highly effective against credential stuffing
- Gradually increase rate limit values in production to avoid affecting API response times