Idempotency
Stable keys vs transport retries — never double-submit to Hacienda.
Issuing a document is a mutation with real-world consequences: a duplicate means
a duplicate comprobante filed with Hacienda. Sello makes issuance safe to retry
with an Idempotency-Key header.
How it works
Every mutating request (POST /v1/documents, ingest, respond) carries an
Idempotency-Key. Replaying the same key returns the original result
without re-issuing — no double-submit to Hacienda. Reusing a key with a
different request body is a 409.
Idempotency-Key: order-1042The key is caller-generated — a stable business key like order-1042 (see
below) or a random UUID both work — and must be 1–255 printable-ASCII characters
with no whitespace or control characters (!–~); anything else is a 400.
Two layers of retry — know which you're solving
This is the subtle part. There are two kinds of retry, and only your key can
cover both. That is why documents.create and createAndWait require one:
the SDK refuses a call with no key, or a blank one, with a TypeError and sends
nothing.
Transport retries (the SDK replays your key)
The SDK's transport layer retries transient failures — a network blip, a 429,
a 5xx. Every one of those retries resends the same key you passed, so a
flaky network never causes a double-submit within a single create call.
Your own retries (your key covers them too, if it is stable)
If your application runs create again — a retried job, a redelivered queue
message, a user double-clicking "pay" — Sello can only tell it is the same sale
if the key is the same. So derive the key from the business event and store
it with that event:
// Same order retried twice → exactly one issuance.
await sello.documents.create(invoice, `order-${order.id}:FE`);Never derive the key from the document's content (a hash of the body, the total, the buyer). Two real sales can look identical, and the second one would replay the first instead of being issued.
If you really want a random key per call, pass newIdempotencyKey() explicitly.
It dedupes the transport retries inside that call and nothing else: when your
code calls create again it gets a new key, and Sello issues a second document.
Asking whether a key was ever settled
Sometimes you hold a body you no longer want to send — the customer corrected their profile after your job froze the request — but you cannot tell whether an earlier attempt under that key already issued. Retrying blindly may sign the stale body; dropping it may lose a document Hacienda already holds.
Send the same body with Sello-Replay-Only: true (SDK: { replayOnly: true }).
Sello then does one of two things and nothing else:
- the key has a settled result → the original
201body is replayed; - the key is unknown or its reservation lapsed →
412withtype: https://sello.cr/problems/idempotency-replay-missand areasonofno-roworlapsed-reservation. No consecutivo is allocated and nothing is signed. The original key is permanently cancelled, excluding even a delayed request that has not arrived yet. Save the returnedreplacementIdempotencyKeydurably before rebuilding, then send the rebuilt body under that replacement key without the replay-only header. Never reuse the cancelled key. Repeated probes of the original key return the same replacement if the response is lost.
A key still being processed by an earlier request answers 409
idempotency-in-flight with a Retry-After, exactly as a plain retry would.
Once recovery starts, continue probing until the replacement is saved, even if the customer's profile changes back. The Odoo adapter uses its durable dispatch marker to probe every previously dispatched job. Enable this protocol only after all API replicas support it: older versions ignore the header and may issue a document. The initial rollout must pause adapter processing until both API and adapter fleets are fully upgraded; see the Odoo re-drive runbook.
try {
const doc = await sello.documents.create(frozenBody, { idempotencyKey, replayOnly: true });
// settled earlier — use doc.clave, do not resend
} catch (err) {
if (err instanceof SelloApiError && err.problem?.type?.endsWith('/idempotency-replay-miss')) {
const replacementKey = err.problem.replacementIdempotencyKey;
if (!replacementKey) throw err; // older API: retain the frozen body; do not remap
// Persist replacementKey before rebuilding; use it on all subsequent attempts.
// A lost response must not make you fall back to the cancelled original key.
} else throw err;
}Rule of thumb
- Passing no key → the SDK refuses the call; nothing is sent.
newIdempotencyKey()→ transport retries are safe; separate re-runs are not.- Pass a stable, unique-per-business-event key (
order-${id},invoice-${id}) whenever your own code path can run more than once for the same real-world event. That is the key that makes end-to-end issuance exactly-once from your side.
The idempotency guarantee is per (account, key). Choose keys that are stable
across your retries but unique across distinct business events.