What you'll build#
A Bun HTTP server with rate limiting. Users who exceed the limit get a 429 response.
Time to complete: ~3 minutes
Prerequisites#
- Unkey account (free)
- Root key with
ratelimit.*.limitpermission - Bun installed
Create a Bun project#
Install the SDK#
Add your root key#
Create a .env file:
.env
Create your server#
Replace index.ts:
index.ts
Run your server#
Test it#
First 10 requests return data. Requests 11+ get:
What's in the response?#
limiter.limit() returns:
| Field | Type | Description |
|---|---|---|
success | boolean | true if allowed, false if rate limited |
remaining | number | Requests left in current window |
reset | number | Unix timestamp (ms) when window resets |
limit | number | The configured limit |
Multiple rate limiters#
Create different limiters for different use cases:
index.ts
Next steps#
Troubleshooting#
Rate limit not applying?
- Verify
UNKEY_ROOT_KEYis in your.envfile - UseBun.env.UNKEY_ROOT_KEY(notprocess.env) - Check your root key hasratelimit.*.limitpermission
Environment not loading?
Bun loads .env automatically. Make sure the file is in your project root
and restart the server.