What you'll build#
A Hono app with rate-limited endpoints. Users who exceed the limit get a 429 response.
Time to complete: ~5 minutes
Prerequisites#
- Unkey account (free)
- Root key with
ratelimit.*.limitpermission - Node.js 18+ or Bun
Create a Hono app#
Choose your preferred runtime.
Install the SDK#
Add your root key#
Create a .env file:
.env
Add rate limiting#
Update src/index.ts:
src/index.ts
Run your app#
Test it#
First 10 requests succeed. 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 |
Using as middleware#
Create reusable middleware for cleaner code:
src/middleware/ratelimit.ts
Apply to routes:
src/index.ts
Different limits per route group#
Deploying to Cloudflare Workers#
For Cloudflare Workers, access env through the context:
Set your secret with wrangler:
Next steps#
Troubleshooting#
Rate limit not applying?
- Verify
UNKEY_ROOT_KEYis set correctly - For Workers: usec.env.UNKEY_ROOT_KEY, notprocess.env- Check your root key hasratelimit.*.limitpermission
Environment not loading locally?
- For Node.js: Install
dotenvand addimport 'dotenv/config'- For Bun:.envloads automatically - Restart the dev server after changes