Safe retries with idempotency keys
developers-idempotencyOn this page
Send an Idempotency-Key header on writes, so retrying after a dropped connection never creates a second contact, job or payment.
A network can drop the answer to a write that actually worked. If you retry it, you could create a second contact or job. Send an Idempotency-Key header, and a retry with the same key does nothing twice.
It works on POST /contacts, PATCH /contacts/{id}, POST /jobs, PATCH /jobs/{id}, PATCH /jobs/{id}/status, POST /visits, PATCH /visits/{id}, POST /visits/{id}/cancel, POST /estimates, POST /invoices, POST /invoices/{id}/payments, POST /webhooks/subscriptions and DELETE /webhooks/subscriptions/{id}.
On POST /invoices/{id}/payments the header is required, because money is recorded there.
curl -s -X POST "$SKEDULER_API/api/v1/contacts" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Idempotency-Key: 6f1c0b5e-2c1a-4f3e-9a52-0d3c9f1d7a10" \
-H "Content-Type: application/json" \
-d '{"first_name":"Sam","last_name":"Rivera","email":"sam@example.com"}'
How idempotency keys work
- The key is yours: 1 to 255 printable ASCII characters. A UUID is a good choice.
- Use a new key for each write, and the same key when you retry that write.
- Keys belong to your app, so two apps never collide.
- A repeat with the same key and the same request (method, URL and body) returns the stored answer, with
Idempotent-Replayed: true. Nothing is created again. - Answers are kept for 24 hours.
Which answers are kept for a retry
Successful answers (2xx), and 422s for a malformed request (a field that fails validation), are kept and replayed.
A 422 that the record's state caused, such as a payment on a draft invoice or above the amount due, isn't kept, and nor is a 422 from a check that reads your data or today's date, such as an id that doesn't exist yet: fix the cause, then send the same request with the same key and it runs for real.
Anything else that can change (a 500, a 429, an auth failure, a 404) isn't kept either, so a retry with the same key really tries again.
If a payment was refused (say the invoice was still a draft), fix the cause and send the payment with a new key.
Idempotency key errors
| Status | error.code | Meaning |
|---|---|---|
| 422 | invalid_idempotency_key | The key isn't 1 to 255 printable ASCII characters, or it's missing on a payment. |
| 422 | idempotency_key_reused | The key was already used for a different request. Use a new key. |
| 409 | idempotency_in_progress | The first request with this key hasn't finished. Wait a moment and retry. |
| 409 | idempotency_replay_unavailable | The first answer for this key can no longer be replayed. Retry with a new Idempotency-Key. |