Idempotency-Key Header for API v2 Writes
A network timeout does not prove that a write failed. The server may already have applied it. The Idempotency-Key header lets you retry a write without creating a second leave, label, or other record.
The Idempotency-Key header
Send Idempotency-Key on any write request: POST, PUT, PATCH, or DELETE. The header is optional, but it is recommended for every write.
If a request fails or times out, resend it with the same header value and the same body. The example below shows a leave-creation request with a key attached.
Key format rules:
- 1 to 255 printable ASCII characters, with no spaces or control characters.
- Use a random UUID (version 4) for each logical write, such as 5f1c7a52-3d0e-4b8e-9d6a-2c4f0b9e1a77.
- Create a new key for each new write. Reuse a key only to retry the same write.
A header that breaks these rules is rejected with 400 INVALID_IDEMPOTENCY_KEY, and nothing is written.
Scope and retention
Key storage is tied to the API key that sent the request, not to the account as a whole. That means two separate API keys can each use the identical Idempotency-Key string, and neither one will interfere with the other's replay history.
Retention runs on a 24-hour clock that starts when a key is first used. Once that window closes, the same string is treated as brand new, so a later write carrying it will execute rather than replay.
What counts as the same request
On first use, the service records a fingerprint of the request so it can recognise a genuine retry later. A retry is treated as the same request only when every one of the following matches the original:
- the HTTP method;
- the endpoint and its path parameters (for example the leave ID in /v2/leaves/{id}/approve);
- the JSON body (key order and whitespace do not matter);
- the user in the X-On-Behalf-Of header, if you send one.
Headers outside that list are ignored when the fingerprint is compared.
Behaviour on retry
The table below summarizes what happens when you send a request with a key that has already been used. Read it as a decision table: the first column tells you the situation, and the second column tells you the response and what to do next.
When the first request finished, the original response is returned again: same status code, same body, plus an extra Idempotent-Replayed: true header. The write does not run again.
When the first request is still running, the retry returns 202 Accepted with the operation of the first request. Poll it as described in Synchronous writes and operations. The write does not run again.
When the same key is used with a different request, the response is 422 IDEMPOTENCY_KEY_REUSED. Nothing is written.
The original response is replayed as is
A replay hands back the first response unchanged, so treat it as a snapshot rather than a live read:
- For a 201 or 200, you receive the resource exactly as it stood when the original call completed. Fetch the resource again if you need its present state.
- For a 202, you receive the same operation, but the status inside the replayed body can be stale. Poll the operation URL to learn where it stands now.
- Client errors in the 4xx range are replayed as well — a leave turned down by a blackout period, for instance. Correct the request and resend it under a fresh key.
Two cases are not stored, so a retry with the same key runs the write:
- Requests rejected before the key is checked, for example INSUFFICIENT_SCOPE or ON_BEHALF_OF_NOT_ALLOWED.
- Requests that failed with 500 INTERNAL_ERROR before the write was submitted.
When a write has already been submitted and an unexpected failure follows, the key stays recorded and the reply is a 202 carrying the operation, which guarantees a retry cannot submit the write a second time.
Reused key with a different payload
If you reuse a key with a different request, the response is 422 IDEMPOTENCY_KEY_REUSED. This check comes first, so you get it even while the first request is still running.
The error body has the shape shown below.
Concurrent retries
A retry that arrives while the original is still in flight is answered immediately rather than queued behind it. The write is not executed a second time; instead you receive 202 Accepted carrying the operation from the first attempt.
Occasionally the first request has not yet attached its operation when the retry arrives; in that case you get 409 IDEMPOTENCY_REQUEST_IN_PROGRESS. Pause for a few seconds, then send the retry again with the same key.
Errors
The table below lists the error responses related to idempotency. Use the status and code together to decide whether to fix the header, wait, or use a new key.
| Situation | Response |
|---|---|
| First request with this key | The write runs normally. |
| Same key, same request, first request finished | The original response is returned again: same status code, same body. It carries an extra Idempotent-Replayed: true header. The write does not run again. |
| Same key, same request, first request still running | 202 Accepted with the operation of the first request. Poll it as described in Synchronous writes and operations. The write does not run again. |
| Same key, different request | 422 IDEMPOTENCY_KEY_REUSED. Nothing is written. |
The table is a reference for the captured API v2 behavior. Check current API documentation for any changes.
Frequently asked questions
What happens if I retry a write with the same Idempotency-Key?
If the first request finished, the original response is returned again with the same status code and body, plus an Idempotent-Replayed: true header. The write does not run again. If the first request is still running, the retry returns 202 Accepted with the operation of the first request. If the key was used with a different request, the response is 422 IDEMPOTENCY_KEY_REUSED and nothing is written.
How long is an Idempotency-Key kept?
The retention clock starts the moment a key is first used and runs for 24 hours. Once that period ends, the string is no longer recognised as a replay key, so a later write carrying it executes as a fresh request.
What makes two requests count as the same request?
Two requests match when the HTTP method, the endpoint and its path parameters, the JSON body, and the user in the X-On-Behalf-Of header (if sent) are all equal. Other headers do not count.
What should I do after a 409 IDEMPOTENCY_REQUEST_IN_PROGRESS?
Wait a few seconds and retry with the same key. The first request has not linked its operation yet.
curl -X POST $API_BASE_URL/v2/leaves \
-H "Authorization: Bearer vt_live_your-secret" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 5f1c7a52-3d0e-4b8e-9d6a-2c4f0b9e1a77" \
-d '{ "userId": "...", "leaveTypeId": "...", "startDate": "2026-10-05", "endDate": "2026-10-09" }'{
"error": {
"code": "IDEMPOTENCY_KEY_REUSED",
"name": "IdempotencyKeyReused",
"description": "Example post details.",
"resolution": "Use a new Idempotency-Key for a different request, or resend the original payload.",
"requestId": "..."
}
}{
"operation": {
"id": "pa_...",
"status": "PENDING",
"url": "/v2/operations/pa_..."
}
}| Status | Code | When |
|---|---|---|
| 400 | INVALID_IDEMPOTENCY_KEY | The header is not 1 to 255 printable ASCII characters. |
| 409 | IDEMPOTENCY_REQUEST_IN_PROGRESS | A request with this key is still being processed. |
| 422 | IDEMPOTENCY_KEY_REUSED | The key was already used with a different request. |
The table is a reference for the captured API v2 behavior. Check current API documentation for any changes.