Apply different rate limits to different users based on their subscription tier, role, or any other criteria. This recipe shows how to implement tiered rate limiting without hardcoding limits in your application.
The pattern#
Full implementation#
Next.js API Route#
Express Middleware#
Using Unkey overrides (recommended)#
Instead of managing limits in your code, use Unkey overrides to set per-user limits dynamically. Overrides are managed via the separate Overrides class:
This approach means:
- No code changes when limits change
- Overrides can be managed via API or dashboard
- Default limit applies to users without overrides
With API key verification#
If you're already using Unkey for API keys, attach rate limits directly to keys:
Best practices#
Always use the same identifier format (user ID, org ID) for accurate limiting across requests.
Return rate limit headers so clients know their limits and can back off gracefully.
Pro/Enterprise users often expect some burst capacity. Consider slightly higher limits with shorter windows.
Track when users hit limits to inform pricing decisions and identify potential abuse.