Skip to content

Idempotency

Networks fail. If a write times out you cannot know whether it happened. Send an Idempotency-Key header and retry with the same key: the API performs the action at most once and returns the original response to every retry.

Sending a key

Add the header to any POST, PUT, PATCH or DELETE request made with an API key. The value is any string up to 255 characters; a UUID v4 is a good choice.

curl -X POST https://semanticson.com/api/v1/sites/SITE_ID/changes \
  -H "Authorization: Bearer $SEMANTICSON_API_KEY" \
  -H "Idempotency-Key: 4f1c2b9e-7d0a-4a51-9a63-0c2f5e8d1b77" \
  -H "Content-Type: application/json" \
  -d '{
  "changes": [
    {
      "kind": "meta.update",
      "target": {
        "externalId": "42"
      },
      "payload": {
        "metaDescription": "Dial in espresso with a simple grind size chart."
      }
    }
  ]
}'

A retry that is answered from the stored result carries the response header idempotent-replayed: true.

How it behaves

Keys are scoped to the API key that sent them, so two integrations cannot collide. The request is identified by its method, path and exact body.

SituationResult
First request with a keyProcessed normally. The status and body are stored with the key.
Same key, same method, path and bodyThe stored response is returned with the same status and idempotent-replayed: true. Nothing is done again.
First attempt failed with a 4xx errorThe error is stored too: retrying with the same key returns the same error. Fix the request and use a new key.
First attempt failed with a 5xx errorThe key is released. Retrying with the same key performs the request again.
Same key while the first request is still running409 idempotency_in_progress. Wait briefly and retry.
Same key, different method, path or body409 idempotency_key_reused
GET requests and requests without a key (/connect/exchange)The header is ignored; these are safe to repeat anyway.

The body must be byte-for-byte identical

Serialise the body once and reuse the same string for retries. Re-encoding an object can change key order or whitespace and turn a retry into idempotency_key_reused.

Conflicts

Both conflict errors use status 409. idempotency_in_progress is temporary: retry after a short delay with the same key. idempotency_key_reused means a bug in key generation: each distinct request needs its own key.

Retention

Stored responses are kept for at least 24 hours, which covers any reasonable retry window. Do not reuse a key after that for a different purpose; generate a fresh one per request.

Responses with secrets

Responses that contain a secret shown only once are never stored, so the header is ignored on POST /sites, POST /sites/{siteId}/rotate-key and POST /webhooks. Retrying POST /sites is still safe: the same address in the same project reconnects the existing website instead of adding a second one.

Choosing keys

  • Generate the key when you first decide to make a request, store it with your job, and reuse it for every retry of that job.
  • Derive it from your own record when that is natural, for example publish-post-123-v4.
  • Endpoints that create things (POST /sites/…/changes, POST /tools/…/run, audits) benefit most.