Documentation
Ratelimiting endpoints with Hono
ratelimit.*.create_namespace
ratelimit.*.limit
npm create hono@latest
@unkey/ratelimit
npm install @unkey/ratelimit
.env
UNKEY_ROOT_KEY="YOUR_KEY"
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;
npm run dev
curl -XPOST 'http://localhost:8787/'
Was this page helpful?