Skip to main content

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

1

Create a Bun project

2

Install the SDK

3

Add your root key

Create a .env file:
.env
4

Create your server

Replace index.ts:
index.ts
5

Run your server

6

Test it

First 10 requests return data. Requests 11+ get:

What’s in the response?

limiter.limit() returns:

Multiple rate limiters

Create different limiters for different use cases:
index.ts

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 in your .env file - Use Bun.env.UNKEY_ROOT_KEY (not process.env) - Check your root key has ratelimit.*.limit permission
Bun loads .env automatically. Make sure the file is in your project root and restart the server.
Last modified on May 21, 2026