Skip to main content

Per-User Rate Limits

Apply different rate limits based on user subscription tiers using Unkey. Give free, pro, and enterprise users separate request quotas.
1 min read

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#

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#

Use identifiers consistently

Always use the same identifier format (user ID, org ID) for accurate limiting across requests.

Communicate limits clearly

Return rate limit headers so clients know their limits and can back off gracefully.

Consider burst allowance

Pro/Enterprise users often expect some burst capacity. Consider slightly higher limits with shorter windows.

Log limit hits

Track when users hit limits to inform pricing decisions and identify potential abuse.

Next steps#