Why Rate Limiting?
Prevent abuse
Stop bad actors from hammering your endpoints, scraping data, or launching DDoS attacks
Protect infrastructure costs
Limit expensive operations (AI calls, database queries) before they blow up your bill
Fair usage enforcement
Ensure no single user monopolizes shared resources or degrades service for others
Compliance & SLAs
Enforce contractual limits (“10,000 requests/month on Basic plan”)
How It Works
Unkey’s rate limiting uses a sliding window algorithm for smooth, accurate enforcement:1
Choose an identifier
Decide what you’re limiting: user ID, API key, IP address, organization ID, or any string that uniquely identifies the requester.
2
Set the limit
Define how many requests are allowed and over what duration. Example: 100 requests per minute.
3
Check on each request
Call
limiter.limit(identifier) and Unkey tells you whether to allow or reject the request.Rate Limiting Approaches
Unkey offers two complementary ways to implement rate limiting:Standalone Rate Limiting
Protect any endpoint with identifier-based rate limiting.- TypeScript
- Next.js Middleware
- Express.js
Configuration Options
string
required
Your Unkey root key with
ratelimit.*.limit permissionstring
required
Logical grouping for your rate limits. Separate namespaces are isolated from each other. Examples:
"api", "login", "webhooks"number
required
Maximum number of requests allowed in the duration window
string | number
required
Time window for the limit. String format:
"30s", "5m", "1h", "1d". Number format: milliseconds (e.g., 60000 for 1 minute)object
Configure behavior when Unkey is unreachable:
function
Error handler for network failures:
Key-Attached Rate Limiting
Configure rate limits directly on API keys — they’re automatically enforced during verification.- Create Key with Limits
- Verify with Rate Limit Check
- Custom Cost per Request
Multiple Rate Limits per Key
Apply different limits to different operation types:Algorithms & Architecture
Sliding Window Algorithm
Unkey uses sliding windows to provide smooth rate limiting without the “burst at window reset” problem.- Problem: Fixed Windows
- Solution: Sliding Windows
Fixed windows allow burst exploitation:
- Limit: 100 requests per minute
- User sends 100 requests at 00:59
- Window resets at 01:00
- User sends 100 more at 01:01
- Result: 200 requests in 2 seconds ❌
Global Consistency
Rate limits are enforced consistently across all regions. A user can’t bypass limits by hitting different geographic endpoints.See real-time global performance metrics at ratelimit.unkey.com — latency and throughput benchmarks updated live.
Advanced Features
Custom Overrides
Give specific users higher (or lower) limits without code changes.- Dashboard
- API
- Wildcard Patterns
- Go to Ratelimit → Select namespace → Overrides tab
- Click Add Override
- Enter identifier and custom limits
- Changes propagate globally in ~60 seconds

Per-User vs Per-Endpoint Limits
- Per-User
- Per-Endpoint
- Per-IP
Cost-Based Limiting
Different operations can consume different amounts from the limit:Timeout & Fallback
Configure resilient behavior when Unkey is unreachable:Response Format
Every rate limit check returns:Handling Rate Limit Responses
Common Patterns
Tiered limits by plan
Tiered limits by plan
Multiple namespaces
Multiple namespaces
Progressive throttling
Progressive throttling
Burst allowance
Burst allowance
Best Practices
Choose appropriate windows
- Seconds: Real-time APIs, live updates
- Minutes: Standard APIs, search
- Hours: Expensive operations, AI calls
- Days: Free tier quotas, trial limits
Return helpful headers
Always include
X-RateLimit-* headers so clients know their limit status and when to retry.Use multiple namespaces
Separate rate limits for different endpoint categories (auth, api, webhooks) for better control.
Monitor and adjust
Watch analytics to see which identifiers are hitting limits. Adjust thresholds based on real usage.
Combine with usage limits
Use rate limits for frequency control and usage limits (credits) for total volume quotas.
Implement fallback behavior
Configure timeout and error handlers to maintain availability during network issues.
Next Steps
Identities
Share rate limits across multiple keys per user
Analytics
Track rate limit violations and usage patterns
API Reference
Complete rate limiting API documentation
Quickstart
Framework-specific implementation guides