Guides › Concepts
Idempotent requests
3 min · 3 steps · Languages: curl, node, python
Networks fail. A request can time out after the server did the work, and retrying it blindly could deploy twice or create two services. Every write (POST, PUT, PATCH, DELETE) therefore carries an Idempotency-Key.
1. Send a key with every write
Use a new random value, 8 to 128 characters, for each new operation; a UUID is ideal. A write without one is refused with 400 idempotency_key_required.
cURL
curl https://devapi.dispralabs.com/v1/cloud/services/svc_01jdk3n2f4g6h8j0k2l4z6x8ca/restart \
-X POST \
-H "Authorization: Bearer dsp_live_YOUR_KEY" \
-H "Idempotency-Key: 9f1c2b7e-4a63-4d1e-8f0a-2c5b7e9d1a34"
2. Retry with the same key
Sending the same key again with the same request returns the first answer, status and body, without doing the work again. Keys are kept for 24 hours.
- The first request is still running:
409 request_in_progresswithRetry-After: 1. Wait and retry. - The same key with a different request:
422 idempotency_key_reused. Use a new key.
3. Retry the right errors
Retry 429, 502, 503 and 504 with backoff, honouring Retry-After, and always with the same Idempotency-Key. Don't retry other 4xx errors unchanged: they will fail the same way.