Skip to main content
Rate limiting controls how many requests a user, API key, IP address, or any identifier can make within a given time window. Unkey provides globally distributed rate limiting that works at the edge without you managing any infrastructure.

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:
Use both! Apply standalone rate limiting to public endpoints (login, signup) and key-attached limits to authenticated API calls.

Standalone Rate Limiting

Protect any endpoint with identifier-based rate limiting.

Configuration Options

string
required
Your Unkey root key with ratelimit.*.limit permission
string
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.

Multiple Rate Limits per Key

Apply different limits to different operation types:
Then check specific limits during verification:

Algorithms & Architecture

Sliding Window Algorithm

Unkey uses sliding windows to provide smooth rate limiting without the “burst at window reset” problem.
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.
  1. Go to Ratelimit → Select namespace → Overrides tab
  2. Click Add Override
  3. Enter identifier and custom limits
  4. Changes propagate globally in ~60 seconds
Override form

Per-User vs Per-Endpoint Limits

Use when: You want to cap total requests per user regardless of which endpoint they hit.

Cost-Based Limiting

Different operations can consume different amounts from the limit:

Timeout & Fallback

Configure resilient behavior when Unkey is unreachable:
Fail open (allow on timeout) prioritizes availability over strict enforcement. Fail closed (deny on timeout) prioritizes security over availability. Choose based on your requirements.

Response Format

Every rate limit check returns:

Handling Rate Limit Responses

Common Patterns

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