Skip to main content

What you’ll build

An Express server with rate-limited endpoints. Users who exceed the limit get a 429 response. Time to complete: ~5 minutes

Prerequisites

1

Create your Express app

2

Add your root key

Create a .env file:
.env
Never commit your .env file. Add it to .gitignore.
3

Create your server

Create index.js:
index.js
4

Run your server

5

Test it

First 10 requests succeed. Requests 11+ get:

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:
Use on any route:

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

  • Verify UNKEY_ROOT_KEY is set and has ratelimit.*.limit permission - Make sure you’re using a consistent identifier per user - Check that .env is loaded before creating the limiter
Install types and use imports:
Last modified on May 22, 2026