Overview
Network failures happen. When a request fails and you're not sure whether it was processed, you need a safe way to retry. Idempotency keys let you do exactly that.
Send an Idempotency-Key header with a POST, PATCH, PUT or DELETE request. If you send the same request again with the same key, Stable returns the original response instead of performing the change twice. Some endpoints require the header; the API reference marks them.
Using Idempotency Keys
Include the Idempotency-Key header with a unique value:
curl -X POST https://api.stable.se/v1/horses \
-H "Authorization: Bearer $STABLE_TOKEN" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000" \
-d '{"name":"Starlight","sex":"Mare","primary_owner_entity_type":"Organization","primary_owner_entity_public_id":"org_example"}'
Use real public IDs and the enum values from the API reference. When you retry with the same key, the original response comes back with an Idempotent-Replayed: true header:
HTTP/1.1 201 Created
Idempotent-Replayed: true
Content-Type: application/json
How It Works
- You send a request with an
Idempotency-Keyheader. - First time: the request is processed and its response is stored.
- Same key again: the stored response is returned without processing the request again.
- While the first request is still running: the repeat gets
409 Conflict. Wait briefly and retry with the same key. - Same key, different request: some endpoints reject this with
409and the codeidempotency_key_reused. Always use a new key for a new request.
Key Format and Rules
| Property | Value |
|---|---|
| Header | Idempotency-Key |
| Format | 1–128 characters: letters, digits, -, _ and . |
| Retention | Stored responses are kept for at least 24 hours |
| Scope | Keys are scoped to your credentials |
UUIDs (with or without hyphens), ULIDs and nanoids all work as idempotency keys.
Responses that contain a secret, such as a newly created API token or webhook signing secret, are not stored, so retrying those requests repeats them.
A malformed key gets 400 Bad Request with the code bad_request.
When to Use Idempotency Keys
- Creating resources -- prevent duplicate horses, bookings, invoices
- Recording payments -- ensure a payment isn't registered twice
- Sending notifications -- avoid duplicate messages
- Any mutation over an unreliable network -- background jobs, webhook handlers, mobile connections
When They're Not Needed
- GET requests -- reads are naturally idempotent
- DELETE requests -- usually safe to retry (deleting twice gives 404 on the second attempt)
- Operations you can easily check -- if you can verify the result before retrying (e.g., by reading the resource), you may not need idempotency keys
Best Practices
- Generate a new key for each logical operation -- never reuse a key for a different request
- Use UUIDs (v4) to avoid collisions
- Store the key with the request in your system so retries use the same key
- Retry with the same route and body -- change either and it is a new request that needs a new key
- Handle
409responses: retry later with the same key when the first request is still running; use a new key afteridempotency_key_reused