How It Works
Include anIdempotency-Key header with a unique value (e.g., a UUID) on any supported request. The API will:
- First request — process normally and cache the response for 24 hours
- Duplicate request (same key, same parameters) — return the cached response without re-executing the operation
- Key reuse with different parameters — return a
422error to prevent misuse
Idempotent-Replayed: true header — without adding the item again.
Supported Endpoints
| Endpoint | Actions |
|---|---|
POST /cart | Cart creation |
PATCH /orders/:id | Order updates |
PATCH /orders/:id/next | Checkout state transition |
PATCH /orders/:id/advance | Checkout advancement |
PATCH /orders/:id/complete | Order completion |
POST /orders/:id/line_items | Adding items to cart |
POST /orders/:id/payment_sessions | Payment session creation |
PATCH /orders/:id/payment_sessions/:id/complete | Payment completion |
POST /orders/:id/coupon_codes | Applying coupon codes |
POST /orders/:id/store_credits | Applying store credits |
GET, DELETE, and other non-supported actions.
Key Requirements
- Must be a string of 255 characters or less
- Should be unique per distinct operation (UUIDs are recommended)
- Keys are scoped per API key — different API keys can use the same idempotency key without conflict
- Cached responses expire after 24 hours
Response Headers
When a cached response is replayed, the API sets:Error Handling
Key Reuse with Different Parameters
If you send a request with an idempotency key that was previously used with different request parameters (different body, different path), the API returns a422 error:
Key Too Long
Keys longer than 255 characters return a400 error:
Server Errors
5xx responses are not cached. If the server fails to process your request, you can safely retry with the same idempotency key and the request will be re-executed.
SDK Usage
The Spree SDK supports idempotency keys via theidempotencyKey option:
Best Practices
- Generate a new key for each distinct operation. Use a UUID (v4) or another random identifier. Never reuse keys across different operations.
- Store the key before sending the request. If the request fails due to a network error, retry with the same key to ensure the operation is only performed once.
- Don’t use idempotency keys for GET requests. GET requests are naturally idempotent and the API ignores the header on read-only endpoints.
- Let keys expire naturally. Cached responses expire after 24 hours. Don’t rely on idempotency keys for longer-term deduplication.
Relationship with Optimistic Locking
The Store API also uses optimistic locking via thestate_lock_version field on orders. These mechanisms complement each other:
| Mechanism | Prevents | Scope |
|---|---|---|
| Idempotency keys | Duplicate operations from retries | Per-request deduplication |
Optimistic locking (state_lock_version) | Concurrent modifications | Conflict detection between clients |
state_lock_version detects when another client has modified the order.
