> ## Documentation Index
> Fetch the complete documentation index at: https://docs.vectorshift.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Idempotency

> Retry any write safely after a timeout or a lost response

Every write needs an `Idempotency-Key` header. Send one key per logical write, and send the same key again, unchanged, on every retry of that write. If a response is lost, a retry gets the first answer back instead of making the change twice.

```http theme={"languages":{}}
POST /v1/object-model/records/company
Authorization: Bearer sk_…
Idempotency-Key: create-acme-2026-11-16
Content-Type: application/json

{"values": {"name": "Acme"}}
```

## Which requests need a key

* **Writes:** every `POST`, `PATCH`, `PUT` and `DELETE`. Without a key the request is refused with `400 idempotency_key_required`.
* **No key needed:** reads; the read-only `POST`s (`/record-search`, `/records/{type_id}/query`, `/facts/query`, `/figures/query`, `/figures/explain`); dry runs (`"dry_run": true`); and creating the ontology (`POST /v1/object-model`, which an organization can do only once). These are never recorded.

A key is 1 to 255 printable ASCII characters. Make it unique per write, for example a UUID, or an id from your own system plus the change it carries.

## What a retry gets

A key belongs to your organization and to the person the API key acts for. Two of one person's API keys retrying the same write agree. Another person using the same key string makes a separate write of their own.

The API records each write's fingerprint: the method, the route, the path, the request body as parsed (key order and spacing do not matter) and any `If-Match`. A retry then gets one of three answers:

| The retry is | Answer |
| - | - |
| The same request, already finished | The recorded answer again: the same status, body, `Object-Model-Version` and `X-Request-Id`, plus `Idempotent-Replayed: true`. Nothing runs again. |
| The same request, still running | `409 request_in_progress` with `Retry-After`. Retry after that many seconds to get the first request's answer. |
| A different request under the same key | `409 idempotency_key_reused`. Use a new key for a new request. |

A replay is authorized like any request: the API key must still be valid and carry the route's scope, and the replay counts toward the rate limit. An `If-Match` the original carried is not checked again, because the original's own success moved the version it named.

### A lost response

The connection drops before the answer arrives, so the client sends the same request with the same key:

```http theme={"languages":{}}
HTTP/1.1 200 OK
Idempotent-Replayed: true
X-Request-Id: 7f3a…   (the original request's)

{"written_id": "6650…", "disposition": "created", "records": […]}
```

### A concurrent duplicate

A second copy arrives while the first is still running:

```http theme={"languages":{}}
HTTP/1.1 409 Conflict
Retry-After: 2

{"status": "failed", "code": "request_in_progress", "error": "a request with this Idempotency-Key is still running; retry after it finishes to get its answer"}
```

Retried after the first finishes, it gets the first copy's answer with `Idempotent-Replayed: true`.

## What is recorded, and for how long

* **Every answer the ontology gave is recorded, refusals included.** For example, a `422` for an action that stopped part way replays with what it committed, so a retry never quietly writes more.
* **Some failures are not recorded:** a permission refusal (`403`), a `5xx`, a `503` and a `504`. None of them says what was committed, so a retry with the same key runs again. The ontology recognizes the key for the writes listed below, so the change is still made once.
* **After a `504`** the request may still be running. A retry within the time the API waits for that route plus 30 seconds gets `request_in_progress`. After that, the retry runs again.
* **Answers replay for 24 hours.** After that the key is free, and a request under it runs again.
* **If the record cannot be written or read**, the write does not run, and you get `503 idempotency_unavailable` with `Retry-After`.

## After the replay window

Past 24 hours, or after a `504` whose hold has lapsed, a retry reaches the ontology again. Most writes are still recognized by their key there. Each endpoint page describes its own behavior under **Re-sent under the same Idempotency-Key**. In summary:

| Write | Re-sent past the replay window |
| - | - |
| Create a record | Answers the record the first request made, and creates none. |
| Update a record | Field changes and link edits are not applied again. A link someone removed since stays removed. |
| Delete a record | `404 not_found`, because the record is gone. |
| Record, withdraw and scenario writes | Answers the original result and changes nothing again. A request that stopped part way makes only what is missing. |
| Run an action | Answers the finished run again (`replayed`) without writing. See [Actions](#actions) for a run that stopped part way. |
| Run an import | Rows that saved are not written twice. Rows the first run did not reach are saved. |
| Write jobs | The same job, part or state is answered again. A part with the same `part_id` is accepted once. |
| Configure the ontology with `If-Match` | Refused with `412`, because the version has moved. |
| Publish or discard a draft, upgrade the template | Refused, because the change already took effect. |
| Configure **without** `If-Match`, revert, open a draft | **Not recognized.** It applies again: a new version, or another draft. Send `If-Match` on configure to make every retry safe. |

## Actions

An action run's key also names the run. Its `instance_id` is derived from the person and the key, so two people who choose the same key run two separate runs, and the answer carries the `instance_id`.

A run that stopped part way answers `422` with what it committed under `result`. Re-sending it under the same key answers that partial result again and writes nothing. To continue the run, send the same action, subject and params with a **new** `Idempotency-Key`, the `instance_id` from the partial answer, and `"resume": true`:

```bash theme={"languages":{}}
curl -X POST https://api.vectorshift.ai/v1/object-model/actions/advance_stage/run \
  -H "Authorization: Bearer $VECTORSHIFT_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: advance-acme-resume-1" \
  -d '{"instance_id": "key:3f9c…", "resume": true, "subject_id": "6650…", "params": {"stage": "diligence"}}'
```

An `instance_id` belongs to your organization. Another person's run under it, or the same id with a different body, is refused with `instance_mismatch`.

## The generic endpoint

The older generic endpoint (`POST /v1/object-model/{object_model_id}/request`) takes `Idempotency-Key` optionally. Without one, a request runs every time it is sent. With one, it is recorded and replayed as described above.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.