Prompt
I'm building an API platform and need routing, auth, and rate limiting
Latest observation
Oct 5, 2026
OpenAI APIWeb search: off
Here’s a solid, practical way to think about building an API platform with routing, auth, and rate limiting.
1) Routing
What you need
- Path routing:
/v1/users,/v1/orders/:id - Method routing:
GET,POST,PUT,DELETE - Versioning:
/v1/...or header-based versioning - Middleware chain: auth, logging, rate limiting, validation
Recommended structure
- API Gateway / Edge Router
- Handles public entry point
- Terminates TLS
- Applies coarse rate limits
- Authenticates requests
- Routes to internal services
- Service-level router
- Handles business endpoints
- Applies resource-specific checks
Best practices
- Use consistent resource naming:
/users/users/{userId}/users/{userId}/sessions
- Keep verbs out of paths unless it’s an action:
- Prefer
POST /payments - Avoid
/createPayment
- Prefer
- Centralize route registration to avoid drift
2) Authentication
Common options
API Keys
- Simple and good for machine-to-machine integrations
- Usually sent via header:
X-API-Key: ...
- Pros: easy to implement
- Cons: weak if leaked; no user identity granularity unless paired with scopes
OAuth 2.0 / OpenID Connect
- Best for third-party access and user-based authorization
- Use:
- Authorization Code flow for user login
- Client Credentials flow for service-to-service
- Supports scopes and token expiration
JWT
- Useful as an access token format
- Self-contained, stateless verification
- Use short-lived access tokens plus refresh tokens
- Validate:
- signature
- issuer
- audience
- expiry
- scope/roles
Auth design suggestions
- For public APIs: API keys + HMAC signing or OAuth2
- For internal service-to-service: mTLS or OAuth2 client credentials
- For user-facing APIs: OIDC + JWT access tokens
Authorization vs authentication
- Authentication: who are you?
- Authorization: what can you do?
- Don’t mix them.
Security practices
- Store secrets hashed where possible
- Rotate keys regularly
- Support key revocation
- Log auth failures without leaking secrets
- Use HTTPS everywhere
3) Rate limiting
Why it matters
- Prevent abuse
- Protect backend systems
- Enforce plan tiers
- Ensure fair usage
Common algorithms
Token bucket
- Best general-purpose choice
- Allows bursts while enforcing long-term rate
- Example:
- 100 tokens/minute
- burst up to 20
- Good for APIs where short bursts are acceptable
Leaky bucket
- Smooths traffic
- More rigid than token bucket
Fixed window
- Easy to implement
- Can allow burst at window boundaries
- Less precise
Sliding window
- More accurate than fixed window
- More expensive to implement
What to rate limit by
- API key
- User ID
- IP address
- Endpoint
- Tenant/org
- Combination of the above
Recommended policy layering
- Global edge limit per IP/API key
- Per-tenant limit
- Per-endpoint limit
- Sensitive endpoint stricter limits
- login
- password reset
- OTP verification
Responses
- Return
429 Too Many Requests - Include:
Retry-After- rate limit headers if useful:
X-RateLimit-LimitX-RateLimit-RemainingX-RateLimit-Reset
Implementation note
- Use a shared store like:
- Redis
- Memcached
- distributed rate-limit service
- Avoid in-memory limits if you run multiple instances unless you only need best-effort local limits
4) Suggested request flow
A strong default flow:
- Request hits gateway
- TLS terminated
- Basic request validation
- Rate limit check
- Authenticate request
- Authorize scope/role
- Route to backend service
- Service handles business logic
- Return response with audit/logging
5) Example architecture
- Client
- API Gateway
- TLS
- routing
- rate limiting
- auth validation
- Auth service / IdP
- issues tokens
- manages users/clients
- Microservices
- users
- billing
- orders
- Redis
- rate limiting counters
- sessions / token revocation lists if needed
- Observability
- logs
- metrics
- tracing
6) Practical recommendation by platform type
If you’re early-stage
Use:
- API Gateway or reverse proxy like Nginx, Envoy, Kong, or Traefik
- JWT auth
- Redis-backed token bucket rate limiting
If you need developer-friendly public APIs
Use:
- API keys for app identification
- OAuth2 for user auth
- Per-plan quotas
- Good dashboards and key rotation
If you need enterprise-grade controls
Use:
- OIDC/OAuth2
- mTLS for internal traffic
- fine-grained scopes
- tenant-aware rate limiting
- audit logs
7) Minimal endpoint policy example
GET /v1/items- auth required
- read scope
- 300 req/min per API key
POST /v1/items- auth required
- write scope
- 60 req/min per API key
POST /v1/auth/login- no token required
- 10 req/min per IP
POST /v1/auth/otp/verify- 5 req/min per user + IP
8) My recommendation
If you want a sane default stack:
- Routing: API Gateway with versioned REST routes
- Auth: OAuth2/OIDC + JWT access tokens
- Rate limiting: Redis-backed token bucket
- Authorization: scopes/roles/tenant checks
- Observability: structured logs + request IDs + tracing
If you want, I can also give you:
- a reference architecture diagram,
- a database schema for API keys/tokens/quotas, or
- implementation examples in Node.js, Go, Python, or Rust.