Skip to main content

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

1

Create a Hono app

Choose your preferred runtime.
2

Install the SDK

3

Add your root key

Create a .env file:
.env
4

Add rate limiting

Update src/index.ts:
src/index.ts
5

Run your app

6

Test it

First 10 requests succeed. Requests 11+ get:

What’s in the response?

limiter.limit() returns:

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

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 correctly - For Workers: use c.env.UNKEY_ROOT_KEY, not process.env - Check your root key has ratelimit.*.limit permission
  • For Node.js: Install dotenv and add import 'dotenv/config' - For Bun: .env loads automatically - Restart the dev server after changes
Last modified on May 21, 2026