g14o
Packages@g14o/ratelimit-hono

Overview

Hono rate limiting with Upstash Redis for middleware and route handlers.

Hono rate limiting with Upstash Redis. Middleware and route handler wrappers over @g14o/ratelimit.

Install

pnpm add @g14o/ratelimit-hono @upstash/redis @upstash/ratelimit hono

@upstash/redis and @upstash/ratelimit are optional peers (in-memory fallback without Redis). hono is required.

Quick start

Define your app's Hono Env, then pass it to the factory so handlers get a typed Context:

src/types.ts
export interface Bindings {
  TOKEN: string;
}

export interface Variables {
  user: User;
}

export type AppEnv = { Bindings: Bindings; Variables: Variables };
lib/ratelimit.ts
import { createRateLimit } from "@g14o/ratelimit-hono";
import { env } from "./env";
import type { AppEnv } from "../types";

export const { middleware, withRateLimit, checkRateLimit } =
  createRateLimit<AppEnv>({
    redis: {
      url: env.UPSTASH_REDIS_REST_URL,
      token: env.UPSTASH_REDIS_REST_TOKEN,
    },
  });

Import helpers from lib/ratelimit.ts in route modules or inline on your app — c.env and c.get(...) are typed automatically. See Setup for full examples.

Framework alternatives

Import paths

Use caseImport
Rate limit factoryimport { createRateLimit } from "@g14o/ratelimit-hono"
Custom store helpersimport { createStore, defineStore } from "@g14o/ratelimit-hono"
In-memory storeimport { memoryStore } from "@g14o/ratelimit-hono/memory"
Upstash storeimport { upstashStore } from "@g14o/ratelimit-hono/upstash"
Redis store (node-redis / ioredis)import { redisStore } from "@g14o/ratelimit-hono/redis"
Redis / env helpersimport { createRedisClient, isBuildLikePhase } from "@g14o/ratelimit/config"

On this page