Idempotency
Send an Idempotency-Key on every write so a retried request can never create a duplicate.
Network calls fail. Clients retry. Without idempotency, a retried POST /v1/documents
may create two invoices for the same logical event.
The Acreum API solves this with the Idempotency-Key header. Send a stable key on every
mutation that creates or transitions state, and the server guarantees that at most one
logical operation occurs no matter how many times the request is delivered.
When to send a key
Send Idempotency-Key on:
- Every
POSTthat creates a resource (POST /v1/documents,POST /v1/parties,POST /v1/banking/accounts, …). - Every
POSTthat transitions state (POST /v1/documents/{id}/post,POST /v1/loans/{id}/activate, …). PATCHandPUTmutations where the endpoint declares the header.
You do not need it on GET and HEAD requests, or on the preview endpoints, which
write nothing.
Whether a route demands the header is machine-readable, and it is the only place you
should look: the required flag on the operation's Idempotency-Key parameter in the
OpenAPI document, shown on every API Reference page.
required: true— calling without a key returns400 IDEMPOTENCY_KEY_REQUIRED.required: false— calling without a key succeeds with no replay protection. Send one anyway; the endpoint honours it in full.
An operation with no Idempotency-Key parameter at all does not participate, and sending
the header there does nothing.
The header
Idempotency-Key: <client-chosen string>- Must match
^[A-Za-z0-9_-]{1,255}$: letters, digits, underscores and hyphens, at most 255 characters. It does not have to be a UUID, but a v4 UUID is the simplest correct choice. - Must be unique per logical request.
If the format is wrong, the server returns 400 INVALID_IDEMPOTENCY_KEY before doing any
work.
Replay semantics
When the server receives a request with Idempotency-Key: K:
- Lookup. It looks up
(org_id, key, method, route), whererouteis the request path (see Scoping). - First time? It executes the handler and returns the response. A successful (
2xx) response is stored, status and body, against the SHA-256 of the request body. An error response is not stored, so a retry after an error runs the handler again. - Seen before, same body? It returns the stored response without re-executing
the handler. The new request gets its own
X-Request-Id, but the side effect (one document created) is preserved. - Seen before, different body? It returns
409 IDEMPOTENCY_CONFLICT. The server refuses to replay a stored response when the caller's intent has changed.
The replay window is 24 hours. A cleanup job runs every 15 minutes and deletes keys first seen more than 24 hours earlier, so a replay can still succeed for a few minutes past the window; after the key is deleted, a fresh request with the same key is treated as new.
Choosing a key
The single rule: the key must be derivable from your client's logical operation, not from a server response.
- Generate a v4 UUID at the moment your business logic decides to create a resource. Store it alongside your local record. Reuse it across every retry of that operation.
- For an integration that pushes n invoices nightly, each invoice gets its own UUID, generated when the request is constructed.
- Bad: hashing the response body. The response is what you are deduplicating against.
- Bad: hashing only the request body. Two genuinely different but byte-identical requests would collide. Combine with an identifier from your own system if you want a deterministic key.
const key = `${serviceName}-${myRecordId}-${operation}`;
// or
const key = crypto.randomUUID(); // store and reuse on retryConflict response
{
"type": "https://docs.acreum.io/errors/IDEMPOTENCY_CONFLICT",
"title": "Idempotency conflict",
"status": 409,
"detail": "An Idempotency-Key was reused with a different request body. Reuse the original body or supply a new key.",
"code": "IDEMPOTENCY_CONFLICT",
"request_id": "…",
"instance": "/v1/documents"
}This is almost always a client bug: two code paths competing for the same key, or a key generated from data that changes between attempts. Investigate before retrying with a new key; the duplicate intent it surfaced may be the real bug.
Concurrent requests with the same key
If two requests arrive in flight with the same key on the same path before the first has completed, exactly one executes. The other waits for the in-flight transaction to commit and then receives the stored response. You will not get duplicate side effects.
Scoping
The lookup key is (org_id, key, method, route), where route is the concrete request
path without its query string: /v1/items/0b6e…/archive, not /v1/items/{id}/archive.
So reusing the same key value never interferes across:
- different routes, methods or organizations;
- different resources on one route. Archiving item A and then item B with the same key runs both; B is never answered with A's stored response.
In practice: use UUIDs and do not coordinate.
What gets stored
Per key: the method and request path, the SHA-256 of the request body, the full response status
and body, and first/last-seen timestamps. Request headers (other than the key), full
request bodies and caller identity beyond org_id are not stored. Records are reaped
after the replay window.
Pitfalls
- Keys from timestamps. Two retries in the same second collide; two on different days never match. Use a UUID.
- Keys generated by the server and echoed back. Backwards; the client controls the key.
- Sharing a key across services. A scheduler and a backfill job should each carry their own key, or share one deterministic key derived from the business event.
- A different request on retry. The hash is taken over the parsed JSON body with object
keys sorted, so key order and number formatting (
1vs1.0) do not matter, but any changed value does and yieldsIDEMPOTENCY_CONFLICT. Rebuild retries from the same data.