What you’ll build
An Express server with rate-limited endpoints. Users who exceed the limit get a 429 response. Time to complete: ~5 minutesPrerequisites
- Unkey account (free)
- Root key with
ratelimit.*.limitpermission - Node.js 18+
1
Create your Express app
2
Add your root key
Create a
.env file:.env
3
Create your server
Create
index.js:index.js
4
Run your server
5
Test it
What’s in the response?
limiter.limit() returns:
Using as middleware
For cleaner code, create reusable middleware:middleware/ratelimit.js
createMiddleware helper
Create an Express middleware from a Ratelimit instance:Different limits per route
Create multiple limiters:Next steps
How it works
Understand the architecture
Per-user overrides
Give specific users higher limits
SDK Reference
All configuration options
Add API key auth
Combine with API key authentication
Troubleshooting
Rate limit not applying?
Rate limit not applying?
- Verify
UNKEY_ROOT_KEYis set and hasratelimit.*.limitpermission - Make sure you’re using a consistent identifier per user - Check that.envis loaded before creating the limiter
TypeScript version?
TypeScript version?
Install types and use imports: