stable.se

API-tokens och webhooks ingår inte i de planer vi säljer idag. Hör av dig innan du bygger på API:et.

Utvecklardokumentationen är för närvarande bara tillgänglig på engelska.

Idempotency

Safely retry failed requests without creating duplicate resources using idempotency keys.

3 min read

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

  1. You send a request with an Idempotency-Key header.
  2. First time: the request is processed and its response is stored.
  3. Same key again: the stored response is returned without processing the request again.
  4. While the first request is still running: the repeat gets 409 Conflict. Wait briefly and retry with the same key.
  5. Same key, different request: some endpoints reject this with 409 and the code idempotency_key_reused. Always use a new key for a new request.

Key Format and Rules

PropertyValue
HeaderIdempotency-Key
Format1–128 characters: letters, digits, -, _ and .
RetentionStored responses are kept for at least 24 hours
ScopeKeys 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 409 responses: retry later with the same key when the first request is still running; use a new key after idempotency_key_reused