What you’ll build
A Next.js API route that limits each user to a set number of requests per time window. Excess requests get rejected with a 429. Time to complete: ~5 minutesPrerequisites
- Unkey account (free)
- Root key with
ratelimit.*.limitpermission - Node.js 18+
1
Create a Next.js app
Skip if you have an existing project.
2
Install the SDK
3
Add your root key
Create or update
.env.local:.env.local
4
Create a rate-limited route
app/api/protected/route.ts
5
Run your app
6
Test it
200. Requests 11+ return 429:What’s in the response?
limiter.limit() returns:
Choosing an identifier
The identifier determines who gets rate limited. Common choices:Creating a reusable limiter
For cleaner code, create a utility:lib/ratelimit.ts
app/api/login/route.ts
Next steps
How it works
Understand the rate limiting architecture
Per-user overrides
Give specific users higher limits
SDK Reference
All configuration options
Add API key auth
Combine rate limiting with authentication
Troubleshooting
Rate limit not working?
Rate limit not working?
- Check that
UNKEY_ROOT_KEYis set in.env.local- Verify your root key hasratelimit.*.limitpermission - Make sure you’re using the same identifier each request - Restart the dev server after changing.env.local
Getting network errors?
Getting network errors?
- Unkey’s SDK retries failed requests automatically - If errors persist, check status.unkey.com - Check status.unkey.com if issues persist
Want different limits per route?
Want different limits per route?
Create multiple
Ratelimit instances with different namespaces and limits.
Each namespace tracks limits independently.