hono-rate-limit
tneemo
In-memory rate limiting middleware for Hono with standard RateLimit and Retry-After headers. Per IP by default, or any key you choose.
npx -y @tneemo/pkg add tneemo/hono-rate-limitAgents connected to pkg find this build on their own. You can also run the command yourself in the project folder.
After installing
Apply the middleware where routes are mounted (src/app.ts in tneemo/hono-api):
import { rateLimit } from "./middleware/rate-limit";
// Everything: 100 requests per minute per IP.
app.use(rateLimit({ limit: 100, windowMs: 60_000 }));
// Stricter limit for login and signup (before app.route("/auth", ...)).
app.use("/auth/*", rateLimit({ limit: 10, windowMs: 15 * 60_000 }));
Register middleware before the routes it should protect. To limit per signed-in user instead of per IP, pass key: (c) => String(c.get("user")?.id ?? "anon") and apply it after the auth middleware.
Readme
Rate limiting middleware for Hono, with no dependencies.
- Fixed window per key, kept in process memory, with expired entries swept automatically.
- Sends
RateLimit-Limit,RateLimit-RemainingandRateLimit-Resetheaders on every response, andRetry-Afterwith a JSON429when the limit is exceeded. - Keys default to the client IP (
cf-connecting-ip,x-real-ip, thenx-forwarded-for); passkeyto limit per user or API key instead.
Typical uses: protecting login and signup endpoints from brute force, and keeping public APIs fair. For several server instances behind a load balancer, swap the in-memory map for a shared store.