TL;DR
These prompts help you implement rate limiting to prevent API abuse. They cover token bucket and sliding window algorithms, per-user and global limits, and proper response headers. Rate limiting protects against DDoS, prevents abuse, and ensures fair usage.
Basic Rate Limiting
Use this prompt to add basic rate limiting to your API:
Add rate limiting to my API endpoints.
Requirements:
- Limit requests per IP address (for unauthenticated requests)
- Limit requests per user/API key (for authenticated requests)
- Return proper 429 status when limit exceeded
- Include rate limit headers in responses
- Store rate limit state (Redis or in-memory)
Default limits:
- Unauthenticated: 60 requests per minute
- Authenticated: 1000 requests per minute
- Specific endpoints may have custom limits
Response headers to include:
- X-RateLimit-Limit: total allowed
- X-RateLimit-Remaining: remaining requests
- X-RateLimit-Reset: when limit resets (Unix timestamp)
- Retry-After: seconds to wait (when limited)
Create reusable middleware that can be configured per route.
Framework-Specific Implementation
Next.js Rate Limiting
Add rate limiting to my Next.js API routes.
For both App Router and Pages Router:
- Create a rate limiting utility using Upstash Redis or in-memory
- Support edge runtime (for middleware) and Node runtime
- Create higher-order function for Pages Router
- Create helper for App Router route handlers
- Support Vercel's x-real-ip header for IP detection
Usage should be simple:
- Pages Router: export default withRateLimit(handler, { limit: 10 })
- App Router: await rateLimit(request, { limit: 10 })
Handle serverless cold starts (in-memory won't persist between invocations).
Express.js Rate Limiting
Add rate limiting to my Express.js API.
Options:
- Use express-rate-limit with Redis store
- Implement custom rate limiter with sliding window
Requirements:
- Global rate limit for all routes
- Stricter limits for auth endpoints (login, register)
- Skip rate limiting for health checks
- Whitelist certain IPs (internal services)
- Different limits by user tier (free vs paid)
Configuration:
- Development: relaxed limits for testing
- Production: strict limits
Also add request throttling for expensive operations.
Advanced Rate Limiting
Implement tiered rate limiting based on user subscription.
Tiers:
- Free: 100 requests/hour
- Pro: 1000 requests/hour
- Enterprise: 10000 requests/hour
Requirements:
- Look up user tier from database/cache
- Apply appropriate limit based on tier
- Return tier info in response headers
- Track usage for billing/analytics
- Grace period for temporary overages
Also implement:
- Burst allowance (temporary spike above limit)
- Separate limits for different endpoint types
- Usage dashboard data collection
Add different rate limits for different types of endpoints.
Categories:
- Auth endpoints (login, register): Very strict (5/minute per IP)
- Read endpoints: Moderate (100/minute)
- Write endpoints: Stricter (30/minute)
- Search/expensive: Very strict (10/minute)
- Public/cached: Lenient (1000/minute)
Implement:
- Rate limit decorator/middleware that accepts config
- Configuration file for all endpoints
- Easy override for specific routes
- Logging of rate limit violations
- Alerting for sustained abuse
Don't forget distributed systems: If you have multiple server instances, use Redis or another shared store for rate limit state. In-memory rate limiting won't work correctly when requests hit different servers.
Handling Rate Limits
Improve how my API handles and communicates rate limits.
Server-side:
- Return 429 status code when limited
- Include helpful error message with limit details
- Add Retry-After header with wait time
- Log rate limit events for monitoring
Client-side guidance:
- Explain exponential backoff strategy
- Document rate limit headers
- Provide code examples for handling 429
Response format: { "error": "rate_limit_exceeded", "message": "Too many requests. Please wait 30 seconds.", "retryAfter": 30, "limit": 100, "remaining": 0, "reset": 1706745600 }
Pro tip: Consider using a sliding window algorithm instead of fixed windows. Fixed windows can allow burst traffic right before and after the window resets. Sliding windows provide smoother rate limiting.
What rate limits should I start with?
Start conservative and increase based on legitimate usage. A good starting point is 60/minute for unauthenticated and 1000/minute for authenticated users. Monitor and adjust based on actual usage patterns.
Should I rate limit by IP or by user?
Both. Use IP limiting for unauthenticated requests and user/API key limiting for authenticated requests. This protects against both anonymous abuse and authenticated abuse.
What's the difference between rate limiting and throttling?
Rate limiting rejects requests over the limit with 429. Throttling slows down requests (adds delay) instead of rejecting. Rate limiting is more common for APIs, throttling for preventing system overload.
Test Your Rate Limiting
Check if your API has proper rate limiting and abuse protection.
Start Free Scan