Cant setup rate limiting on Vercel
Unanswered
Transvaal lion posted this in #help-forum
Transvaal lionOP
Hi, Im having issues with rate limiting and can't seem to get it to work on my API routes. this is my code in src/middleware.ts (same level as the app folder):
I followed this guide : https://vercel.com/guides/rate-limiting-edge-middleware-vercel-kv
Thank you
import { NextRequest, NextResponse } from 'next/server';
import { Ratelimit } from '@upstash/ratelimit';
import { kv } from '@vercel/kv';
const ratelimit = new Ratelimit({
redis: kv,
// 5 requests from the same IP in 10 seconds
limiter: Ratelimit.slidingWindow(1, '30 s'),
});
// Define which routes you want to rate limit
export const config = {
matcher: '/',
};
export default async function middleware(request: NextRequest) {
// You could alternatively limit based on user ID or similar
const ip = request.ip ?? '127.0.0.1';
const { success, pending, limit, reset, remaining } = await ratelimit.limit(ip);
return success
? NextResponse.next()
: NextResponse.redirect(new URL('/blocked', request.url));
}I followed this guide : https://vercel.com/guides/rate-limiting-edge-middleware-vercel-kv
Thank you