Rate Limiting Guide
Rate limiting protects your API from abuse. Add it when your app grows.
When to Add
- 1000+ daily API requests
- Public API endpoints
- User-generated content
- Before Product Hunt launch
Quick Setup with Upstash
- Sign up: https://upstash.com
- Create Redis database
- Install:
npm install @upstash/ratelimit @upstash/redis - Add to
.env.local:
UPSTASH_REDIS_REST_URL=
UPSTASH_REDIS_REST_TOKEN=
- Create
src/lib/rate-limit.ts:
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
export const ratelimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.slidingWindow(10, "10 s"),
});- Use in API routes:
const { success } = await ratelimit.limit(userId);
if (!success) return Response.json({ error: "Too many requests" }, { status: 429 });Other Options
- Vercel Edge Config
- Cloudflare Workers KV
- Custom middleware
Need help? Contact support (hipacgie@gmail.com).