Blocks/Rate Limiting
IntermediateSecurity

Rate Limiting

Sliding window limiter for Next.js API routes

Production rate limiting with in-memory sliding window (works on Vercel, single Lambda) and optional Upstash Redis for multi-instance production. Per-endpoint configs, X-RateLimit headers, 429 responses with Retry-After, withRateLimit() and withAuthRateLimit() wrappers, and a peek function for dashboard display.

5h
Time saved
228
Lines of code
100%
TypeScript
#rate-limit#redis#upstash#api

Dependencies

@upstash/ratelimit@upstash/redis

Code preview

blocks/ratelimit/index.ts
1var(--text-3);font-style:italic">// Protect an API route:
2export const POST = withRateLimit('auth', async (req) => {
3 return NextResponse.json({ ok: true })
4})
5var(--text-3);font-style:italic">// Automatic 429 + Retry-After on breach. X-RateLimit-* headers on all responses.

Short excerpt. Full block includes SQL migrations, error handling, RLS policies, and complete file structure.

$9

One-time Β· Instant GitHub access

πŸ”œ Payments launching soon

We are setting up secure checkout. Email samarthofficial52@gmail.com to get early access.

You might also like