g14o
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

lib/ratelimit.ts
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 caseImport
Rate limit factoryimport { createRateLimit } from "@g14o/ratelimit-nextjs"
Custom store helpersimport { createStore, defineStore } from "@g14o/ratelimit-nextjs"
In-memory storeimport { memoryStore } from "@g14o/ratelimit-nextjs/memory"
Upstash storeimport { upstashStore } from "@g14o/ratelimit-nextjs/upstash"
Redis store (node-redis / ioredis)import { redisStore } from "@g14o/ratelimit-nextjs/redis"
Redis / env helpersimport { createRedisClient, isBuildLikePhase } from "@g14o/ratelimit/config"

On this page