Prerequisites

1

Create Hono Application

Run the following command to create your Hono project

npm create hono@latest
2

Install

Now install the @unkey/ratelimit package

  npm install @unkey/ratelimit
3

Add Root Key to env

Add your root key to your .env file

UNKEY_ROOT_KEY="YOUR_KEY"
4

Update index to use our Hono SDK

Create a new route and add the following code

/src/index.ts
import { Hono } from "hono";
import { Ratelimit } from "@unkey/ratelimit";

const app = new Hono();

const limiter = new Ratelimit({
	namespace: "hono-example",
 	limit: 2,
 	duration: "30s",
 	rootKey: process.env.UNKEY_ROOT_KEY
});

app.get("/", (c) => {
  const identifier = getUserId(); // or ip or anything else you want
  const ratelimit = await limiter.limit(identifier)
  if (!ratelimit.success){
   return c.status(429).text("Please try again later")
  }
	return c.text("Hello Hono!");
});

export default app;
5

Running it

  npm run dev
6

Try it out

curl -XPOST 'http://localhost:8787/'

You will need to curl a few times to see the ratelimiting error. Once you do, you, you will need to wait to perform the action again.

What is next?

Now that you’ve seen the power of Unkey, check out some resources below to continue your journey.