Idempotent API
Ensures identical requests produce the same result, enabling safe retries.Overview
Usage
Client Side
IncludeIdempotency-Key header with unique key per logical operation:
Key Format
- Max 256 characters
- Must be URL-safe
- Client generates and manages
How It Works
1. Middleware Intercepts
2. Response Caching
Cached data:- HTTP status code
- Response body
- Headers (except sensitive ones)
3. TTL Expiration
Supported Operations
| Method | Safe to Retry? | Notes |
|---|---|---|
POST | ✅ Yes | Create operations |
PUT | ✅ Yes | Update operations |
PATCH | ✅ Yes | Partial updates |
DELETE | ⚠️ Caution | May cause duplicate deletes |
GET | ❌ No | Not cached (idempotent by nature) |
Error Handling
Missing Key (400)
Invalid Key Format (400)
Conflict (409)
If request is in progress with same key:Configuration
When to Use
Use Cases
- ✅ User registration
- ✅ Payment processing
- ✅ Order creation
- ✅ Any state-changing operation
Don’t Use For
- ❌
GETrequests (already idempotent) - ❌ Search queries
- ❌ Real-time data fetching
Testing
Best Practices
- Generate keys client-side — Never rely on server for key generation
- Use ULID for keys — Sortable + unique
- Set reasonable TTL — 24h is usually sufficient
- Include key in error responses — Helps with debugging
- Don’t use sequential keys — Use UUID/ULID to prevent collisions