# Idempotency and safe retries

> Retry a create safely with the Idempotency-Key header in the Techvia TMS API: what each case returns.

Source: https://techvia.software/docs/tms/concepts/idempotency/

Every create (`POST`) takes an optional `Idempotency-Key` header: any text of 1 to 255 characters, a fresh UUID per
new record is best. Send the same key again when you retry the same request (a timeout, a dropped connection), and a
new key for a new record. Keys are per workspace and kept 7 days; after that a key counts as unused.

| You send | You get |
|---|---|
| A key we have not seen | The request runs as usual |
| The same key and the same request (method, path, query and body, byte for byte) | The first answer again, with `Idempotent-Replayed: true`. No second record, no quota unit |
| The same key with a different request | `422` `idempotency-key-reused`. Use a new key |
| The same key while the first request is still running | `409` `idempotency-key-in-progress`, `Retry-After: 1`. A request still "running" after 5 minutes is treated as dead and the same request runs again |
| The same key after the first request failed on our side once it had started | `409` `idempotency-key-failed`: the record may exist. Look it up before you retry with a new key |
| A body over 256 KB (or the endpoint's own limit) | `413` `payload-too-large`, nothing kept |
| The first answer was over 256 KB | You get it in full, but it is not kept: a retry with the same key is `409` `idempotency-answer-too-large`: the request finished, only its answer was too large to replay, so look the record up |
| An empty key, more than 255 characters, control characters or two `Idempotency-Key` headers | `400` `idempotency-key-invalid` |

The first answer is kept when it is a success or an answer about your data (a `404`, a duplicate `409`, an unknown
reference `422`). So after a duplicate `409`, a retry with the same key gets the same `409`: to override it with
`allowDuplicate`, use a new key. When the request was refused before it ran (a `400`, a validation `422`, a rate limit) nothing is
kept: fix the request and send it again with the same key. Once a request has started its work the key stays bound
to it: a failure on our side is kept as `idempotency-key-failed`. If your connection dropped before the answer arrived,
a create that finished keeps its answer as usual, so the retry gets the record it made; while it is still running the
key answers `409` in progress, and after 5 minutes the same request runs again. Only when our server died after saving
the record but before saving the answer is a second record possible after the 5 minutes. The same is true of a create that really runs for more than 5 minutes: a retry after that time counts as
a new attempt and runs while the first is still going (no create takes that long in normal use). In both cases the
duplicate checks on customers, carriers and loads with a reference still apply. Without the header nothing is kept and a retry
runs again. Over MCP the create tools take the same thing as a `client_request_id` argument (same rules; a replayed
result carries `_meta.idempotentReplayed: true`); a REST key and an MCP id share your workspace's keys.
