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

# Idempotency

> Retry POST, PATCH and DELETE requests safely

A network error can leave you unsure whether a request worked. Send an `Idempotency-Key` header and you can retry it without doing the work twice.

Every `POST`, `PATCH` and `DELETE` accepts the header. `GET` requests ignore it, because they change nothing.

```bash theme={null}
curl -X POST "https://api.alex.com/v2/invitations" \
  -H "X-API-Key: $ALEX_API_KEY" \
  -H "Idempotency-Key: 6f1c2b9e-0d4a-4e3b-9a87-1c5d2e3f4a5b" \
  -H "Content-Type: application/json" \
  -d '{
    "candidate": { "first_name": "Ada", "last_name": "Lovelace" },
    "job_external_id": "REQ-12345",
    "contact": { "email": "ada@example.com" }
  }'
```

Use a new random value, such as a UUID, for each distinct request. Keys are 1 to 255 characters. A key outside that range returns `400` `idempotency_key_invalid`. Keys are scoped to your company.

## Replays

When a request succeeds, Alex stores its status and body against the key. Send the same request with the same key again and you get the stored response back, with this header:

```http theme={null}
Idempotent-Replayed: true
```

The request doesn't run again. The replay has the stored status and the same JSON body, though fields within an object can come back in a different order, as JSON objects are unordered. The replay has its own `Request-Id`.

"The same request" means the same method, path, query parameters and JSON body. The order of JSON keys and query parameters doesn't matter.

## Conflicts

| Status | Code | Meaning | What to do |
| - | - | - | - |
| `409` | `idempotency_key_reused` | The key was already used for a different method, path, query or body. | Use a new key for a new request. |
| `409` | `idempotency_request_in_progress` | The first request with this key hasn't finished yet. | Wait, then retry with the same key. |

## Which outcomes are stored

Only successful responses are stored and replayed. Every error releases the key, so you can fix the problem and retry with the same key:

| Outcome | Key |
| - | - |
| `2xx` response | Stored and replayed |
| Any error response (`4xx` or `5xx`), including `429` | Released |
| The request stops without answering | Released after a few minutes |

A released key works as if it were never used.

## Retention

A stored response is kept for 24 hours from the first request. After that, the key is forgotten and a request with it runs as new. Retry within 24 hours.

## Endpoints that are safe to retry anyway

Some endpoints are idempotent without a key, because a repeat finds the existing record:

* `POST /candidates` returns the existing candidate with the same `external_id` or email.
* `POST /invitations` returns the candidate's live application on the job.
* `POST /partner-applications` returns the same `application_id` with `created: false`.
* `POST /handoffs/{id}/cancel` returns `200` for a handoff that is already cancelled.

A key still helps here: it returns the original response, such as the first `201`, rather than a `200`.

## Deletes

`DELETE /jobs/{id}`, `DELETE /candidates/{id}` and `DELETE /webhook-endpoints/{id}` return `204` when they delete the object and `404` `resource_missing` when there is nothing to delete, including when it was already deleted. A retry without a key can therefore get a `404` for a delete that worked. Send an `Idempotency-Key` and a retry gets the first `204` back.

## Responses that carry a secret

`POST /webhook-endpoints` and `POST /webhook-endpoints/{id}/rotate-secret` return the endpoint's signing secret once. A replay of either has `secret: null`, because the secret is never stored where it can be shown again. If you lose it, rotate the secret.


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