Rate Limiting

Protect your API with Upstash Rate Limiting.

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

  1. Sign up: https://upstash.com
  2. Create Redis database
  3. Install: npm install @upstash/ratelimit @upstash/redis
  4. Add to .env.local:
   UPSTASH_REDIS_REST_URL=
   UPSTASH_REDIS_REST_TOKEN=
  1. 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"),
   });
  1. 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).