Skip to content
Docs menu
Guides

Idempotency

Send POST requests of the simple API again after a timeout without creating anything twice, with the Idempotency-Key header.

A request can time out or lose its connection after switchfile has already started the conversion. Sent again, it would convert the file twice. With an Idempotency-Key, a repeated request returns the first result instead.

Send a key#

Every POST request of the simple API takes the header Idempotency-Key: 1 to 255 visible ASCII characters. A UUID works well. Make a new key for every new request and keep it until the request has succeeded.

KEY=$(uuidgen)
curl https://api.switchfile.io/v1/convert \
  -H "Authorization: Bearer $SWITCHFILE_API_KEY" \
  -H "Idempotency-Key: $KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com/report.docx", "to": "pdf"}'

What you get back#

CaseResponse
Same key, same requestThe same conversion, batch, upload or archive, with Idempotent-Replayed: true. Nothing is created twice and no credits are used twice
Same key, different request422 IDEMPOTENCY_KEY_REUSED
Same key while the first request is still running409 IDEMPOTENCY_KEY_IN_USE with Retry-After: 1

A repeated request shows the current state, so it can already return the finished result. Keys are kept for 24 hours per account. A request that failed does not use up its key, so you can send it again with the same key.

Jobs and tasks (v2)#

/v2 requests do not take an Idempotency-Key. To avoid duplicate jobs after a timeout, set a tag on the job and look for it with GET /v2/jobs and filter[tag] before you create the job again.