Idempotency

Retry creates and money movement safely with the Idempotency-Key header.

Every POST that changes something accepts an Idempotency-Key header, and the endpoints that create something or move value require it. Sending the same key with the same body again returns the stored original response instead of acting twice.

Endpoints that require a key

Endpoint
POST /api/v1/persons
POST /api/v1/persons/{person_id}/review/submit
POST /api/v1/persons/{person_id}/associations
POST /api/v1/documents
POST /api/v1/accounts
POST /api/v1/counterparties
POST /api/v1/transfers
POST /api/v1/reserves/move
POST /api/v1/billing/move-from-revenue

A request to one of these endpoints without the header is a 422 with code validation. POST /api/v1/sandbox/deposits and POST /api/v1/sandbox/billing/deposits accept a key but do not require one.

Send a key

The key is 1–255 printable ASCII characters. A UUID works.

curl -X POST https://api.sandbox.roots.finance/api/v1/transfers \
  -H "Authorization: Bearer $ROOTS_API_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 5b8f2c1e-7d4a-4f0e-9a63-2e1c8b7d9f40" \
  -d '{
    "source": {"type": "account", "id": "acct_1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d"},
    "destination": {"type": "counterparty", "id": "cpty_9f8e7d6c5b4a39281706f5e4d3c2b1a0"},
    "rail": "ach",
    "amount_minor": "125000",
    "purpose": "supplier_payment"
  }'

How keys behave

SituationResult
First request with a key succeeds (2xx)The response is stored against the key for 24 hours.
First request fails with a 5xxThe response is stored and replayed like a success, because a 5xx can follow work already done. Read the resource to see whether the first attempt took effect.
Same key, identical body, within 24 hoursThe stored response is returned, with its original status code. Nothing runs again. The endpoint's own preconditions still apply first: retrying a document upload after the person's review has frozen the file returns 409.
Same key, different body409 with code idempotency_key_conflict.
Same key while the first request is still running409 with code idempotency_key_in_progress. Wait briefly and resend the same key and body to receive the first response.
Same key after the first request's work was saved but its response was not409 with code idempotency_key_response_lost. Read the resource; do not resend with a new key.
Same key after 24 hoursThe stored response has expired. Most endpoints run the request again. POST /api/v1/transfers does not: the transfer itself keeps the key, so a retry returns that transfer, repeats its refusal, or 409s on a different body.
First request is refused with a 4xx before anything changedNo response is stored. Resending the same key runs the request again, so a corrected retry goes through. A refused withdrawal is the exception; see below.
First request to POST /api/v1/accounts, POST /api/v1/transfers, or POST /api/v1/persons/{person_id}/review/submit fails with any errorNo response is stored. These endpoints resume: resending the same key and body runs the request again from where it stopped.
Same key for a different operationIndependent. A key is scoped to your client and one operation, and to the person named in the path where there is one.
Same key and body from another user or API token of your client409 with code idempotency_key_conflict. A stored response is replayed only to the user or API token that sent the first request.

For JSON requests the body comparison is byte-exact. Serialize the body once, store it, and resend those exact bytes on every retry. Re-serializing can reorder keys or change whitespace, which reads as a different body.

For POST /api/v1/documents (multipart), the comparison covers the form fields (subject_type, subject_id, type, document_number, issued_country), the file name, its content type, and the file content. The multipart boundary is ignored.

A replayed response carries the header Idempotent-Replayed: true.

A 5xx, or an idempotency 409, to a request that sent a key also carries X-Should-Retry. true means resending the same key can succeed: the first attempt is still running, or the endpoint resumes after an error. false means the same key can only return the same answer. Without the header, decide from the status code.

Refused transfers

A withdrawal or internal transfer is stored as a transfer before the payments backend is called. If the backend refuses it with insufficient_funds or not_found, the transfer is kept with status failed and both transfer.created and transfer.status.changed are sent. Resending the same key repeats the same refusal. To try again, create a new transfer with a new key. A timeout or 5xx leaves the transfer waiting for the backend; resending the same key resumes that call.

Concurrent duplicates

A second request with the same key that arrives while the first is still running gets 409 with code idempotency_key_in_progress. Wait and resend; once the first request has finished, the same key returns its stored response.

POST /transfers also locks every account and counterparty it touches while it runs. A request with a different key that touches one of them during that window gets 429 with code rate_limited and Retry-After: 1. Resend it with its own key and the same body. See Rate limits.

Recover from a timeout

  1. Generate one key per intended operation and store it with the serialized body before the first send.
  2. On a timeout, a 429, a 409 idempotency_key_in_progress, or a 5xx with X-Should-Retry: true, resend the same key and the same bytes.
  3. On a replayed 5xx (Idempotent-Replayed: true), a 5xx with X-Should-Retry: false, or a 409 idempotency_key_response_lost, read the resource first. Use a new key only once you know the first attempt did not take effect. A new key is a new instruction and can move money twice.
  4. A transfer that reached a terminal state, including failed, is never retried by resending. Create a new transfer with a new key.

Next steps


Did this page help you?