Skip to main content

Hono

Step-by-step Hono rate limiting tutorial with @unkey/ratelimit. Protect routes on Node.js, Bun, or Cloudflare Workers without running Redis.
2 min read

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#

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:

FieldTypeDescription
successbooleantrue if allowed, false if rate limited
remainingnumberRequests left in current window
resetnumberUnix timestamp (ms) when window resets
limitnumberThe 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_KEY is set correctly - For Workers: use c.env.UNKEY_ROOT_KEY, not process.env - Check your root key has ratelimit.*.limit permission
Environment not loading locally?
  • For Node.js: Install dotenv and add import 'dotenv/config' - For Bun: .env loads automatically - Restart the dev server after changes