Documentation
Ratelimiting endpoints with Next.js
ratelimit.*.create_namespace
ratelimit.*.limit
npx create-next-app@latest
@unkey/ratelimit
npm install @unkey/ratelimit
.env
UNKEY_ROOT_KEY="YOUR_KEY"
import { NextResponse } from 'next/server'; import { Ratelimit } from "@unkey/ratelimit"; const limiter = new Ratelimit({ namespace: "next-example", limit: 2, duration: "30s", rootKey: process.env.UNKEY_ROOT_KEY }); export const POST = (async (req) => { const identifier = getUserId(); // or ip or anything else you want const ratelimit = await limiter.limit(identifier) if (!ratelimit.success){ return new NextResponse("Please try again later", {status: 429}); } return new NextResponse('Hello!'); });
npm run dev
curl -XPOST 'http://localhost:3000/protected'
Was this page helpful?