Update to [next.js Api Route Limiting Example](https://github.com/vercel/next.js/tree/canary/examples/api-routes-rate-limit)
npm install next-rate-limitUpdate to next.js Api Route Limiting Example
``sh`Using npm
> npm install next-rate-limitUsing yarn
> yarn add next-rate-limitUsing pnpm
> pnpm add next-rate-limit
`ts`
import rateLimit from 'next-rate-limit';
`ts
import rateLimit from 'next-rate-limit';
import { NextRequest, NextResponse } from 'next/server';
const limiter = rateLimit({
interval: 60 * 1000, // 1 minute
uniqueTokenPerInterval: 500 // Max 500 users per minute
});
export function GET(req: NextRequest) {
try {
const headers = limiter.checkNext(req, 10);
return NextResponse.json(
{
limit: headers.get('X-RateLimit-Limit'),
remaining: headers.get('X-RateLimit-Remaining')
},
{ headers }
);
} catch {
return NextResponse.json({ error: 'Rate limit exceeded' }, { status: 429 });
}
}
`
See original repo linked for full example.
> number
See lru-cache ttl
Defaults to 60000 ms (= 1 minute).
> number
See lru-cache max
Defaults to 500`.