Packages@g14o/ratelimit-nextjs
Overview
Next.js rate limiting with Upstash Redis for NextRequest and NextResponse.
Next.js rate limiting with Upstash Redis. Typed for NextRequest / NextResponse route handlers; delegates to @g14o/ratelimit at runtime.
Install
pnpm add @g14o/ratelimit-nextjs @upstash/redis @upstash/ratelimit next@upstash/redis and @upstash/ratelimit are optional peers (in-memory fallback without Redis). next is required.
Quick start
import { createRateLimit } from "@g14o/ratelimit-nextjs";
import { logger } from "@/lib/logger";
import { env } from "@/lib/env";
export const { withRateLimit, checkRateLimit, withUserRateLimit } =
createRateLimit({
redis: {
url: env.UPSTASH_REDIS_REST_URL,
token: env.UPSTASH_REDIS_REST_TOKEN,
},
logger,
});Framework alternatives
For plain Request / Response, use @g14o/ratelimit.
For Express middleware and route handlers, use @g14o/ratelimit-express.
For Hono middleware and route handlers, use @g14o/ratelimit-hono.
Import paths
| Use case | Import |
|---|---|
| Rate limit factory | import { createRateLimit } from "@g14o/ratelimit-nextjs" |
| Custom store helpers | import { createStore, defineStore } from "@g14o/ratelimit-nextjs" |
| In-memory store | import { memoryStore } from "@g14o/ratelimit-nextjs/memory" |
| Upstash store | import { upstashStore } from "@g14o/ratelimit-nextjs/upstash" |
| Redis store (node-redis / ioredis) | import { redisStore } from "@g14o/ratelimit-nextjs/redis" |
| Redis / env helpers | import { createRedisClient, isBuildLikePhase } from "@g14o/ratelimit/config" |