Idempotency
Every POST, PATCH and DELETE made with a Dio API key needs an Idempotency-Key header. Here is what a retry gets back, and how to retry safely.
Networks drop answers. Without protection, a retried request to create an interview link makes a second link for the same candidate, and your system, having seen success both times, has no way to know. So every request that changes something carries an Idempotency-Key header, and a retry with the same key gets the first answer back instead of doing the work twice.
Sending the header
Send Idempotency-Key on every POST, PATCH and DELETE made with an API key. The value is yours to choose; a UUID is a good one.
IDEMPOTENCY_KEY=$(uuidgen) # one per interview link: keep it, and send it again on a retry
curl -X POST https://api.getdio.ai/v1/jobs/job_126f809ebbe2/invites \
-H "Authorization: Bearer $DIO_API_KEY" \
-H "Idempotency-Key: $IDEMPOTENCY_KEY" \
-H "Content-Type: application/json" \
-d '{"label": "Application 4471"}'
A request that needs the header and doesn't have it answers 400, naming the header:
{"detail": "Idempotency-Key header is required on this request. A retry without one creates a second invite for the same candidate."}
GET requests never need it.
What a retry gets
Dio claims the key before it does the work and keeps the answer afterwards. "The same request" means the same method, the same path and the same body, byte for byte. Headers are not part of it.
| What happens | What you get |
|---|---|
| The first request with a key | The real answer. If it is a 2xx or 4xx JSON answer, Dio keeps it for 24 hours. |
| The same request again with the same key, within 24 hours | The kept status and body, with the header Idempotent-Replay: true. The body is replayed; the first answer's other headers are not. |
| The same request while the first is still running | 409, with the detail "a request with this Idempotency-Key is still in flight". Retry shortly. A first request that hasn't answered within 10 minutes is treated as stopped, and the retry runs. |
| The same key with a different method, path or body | 422. Reusing a key for a different request is a bug in the client, and replaying the first answer would hide it. |
The first request ended in a 5xx |
Nothing is kept and the key is free. Retry with the same key after a pause, and the request runs again. |
| The same key after 24 hours | The key is forgotten, and the request runs as a first request. |
A refusal is replayed like a success. If a request was refused with a 4xx, for example a 409 because the workspace could not start interviews, sending it again with the same key returns the same refusal for 24 hours. Once the cause is fixed, send the request with a new key.
Choosing keys
Use one key per logical operation: one per interview link you mean to create, not one per process or per batch. Create the key before the first attempt, store it with the record it belongs to (the application, say), and send the same key on every retry of that operation, even after a restart.
Keys are kept per API key, so the same value sent with two different API keys is two separate claims.
Retry with the same body bytes. Re-serialising your JSON with a different key order or spacing is a different body, and the retry answers 422.
After a server error
A 5xx frees the key so that your retry runs. Retry with the same key, after a pause.
Be aware that a server error can arrive after the work was done: the link made, then the failure on the way back. In that case the retry can make a second link. If that matters to you, the early-access on_duplicate option answers a second request for the same person with the link they already have; see Early access.
A retry loop
This Python function creates one interview link and retries the failures worth retrying, always with the same key and the same body.
import json
import time
import requests
API = "https://api.getdio.ai/v1"
def create_link(api_key: str, job_id: str, label: str, idempotency_key: str) -> dict:
"""Create one interview link. `idempotency_key` belongs to this link: store it first."""
body = json.dumps({"label": label}) # serialised once, so every attempt sends the same bytes
headers = {
"Authorization": f"Bearer {api_key}",
"Idempotency-Key": idempotency_key,
"Content-Type": "application/json",
}
for attempt in range(6):
try:
response = requests.post(
f"{API}/jobs/{job_id}/invites", headers=headers, data=body, timeout=30
)
except requests.RequestException:
time.sleep(2**attempt) # no answer: the same key makes the retry safe
continue
if response.status_code == 429:
time.sleep(int(response.headers.get("Retry-After", "1")))
continue
if response.status_code == 409 and "still in flight" in response.json().get("detail", ""):
time.sleep(2**attempt)
continue
if response.status_code >= 500:
time.sleep(2**attempt)
continue
response.raise_for_status() # any other 4xx needs a person or a code change
return response.json()
raise RuntimeError(f"gave up creating a link for {job_id}")
Any other 4xx is not worth retrying unchanged: read its detail. Errors and limits lists what each status means.