1. Set up rate limiting in your project by installing the @upstash/ratelimit and @vercel/kv packages:
npm install @upstash/ratelimit @vercel/kv
  1. Create a rate limiter instance using the following pattern:
import { Ratelimit } from "@upstash/ratelimit";
import { kv } from "@vercel/kv";

// Create a new ratelimiter instance
const ratelimit = new Ratelimit({
	redis: kv,
	// Choose the appropriate limiter algorithm
	limiter: Ratelimit.slidingWindow(10, "10 s"), // Example: 10 requests per 10 seconds
});

...
// Inside your route handler
// call ratelimit with request ip
const { success, remaining } = await ratelimit.limit(req.ip ?? 'ip');

// block the request if unsuccessfull
if (!success) return new Response('Rate limit exceeded', { status: 429 });