Retries and idempotency
Networks fail. If your request times out, you can't tell whether Axis received it, so you can't tell whether retrying would do the thing twice: order twice, buy two top-ups, or charge twice.
Send an Idempotency-Key header on a POST and it becomes safe to retry: Axis does it once, and every repeat with the
same key gets the first answer back instead of doing it again.
How to use it
- Make a new unique key for each operation you mean to do once, such as a UUID. Keep it with the job, so a retry reuses it.
- Up to 255 characters.
- Keys are remembered for 24 hours.
- Every
POSTaccepts one. Use it on anything that charges or orders:POST /orders,POST /pools,POST /sims/{id}/top-ups,POST /pools/{id}/top-ups. It's just as useful on the SIM actions.
- cURL
- Node.js
- Python
- C#
curl -X POST https://app.zentarinetworks.com/api/v1/orders \
-H "Authorization: Bearer $AXIS_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 7c9e6679-7425-40de-944b-e07fc1f90ae7" \
-d '{ "type": "digital", "quantity": 10 }'
import { randomUUID } from 'node:crypto';
const key = randomUUID(); // store it with the job, so a retry reuses it
const res = await fetch('https://app.zentarinetworks.com/api/v1/orders', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.AXIS_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': key,
},
body: JSON.stringify({ type: 'digital', quantity: 10 }),
});
import uuid
key = str(uuid.uuid4()) # store it with the job, so a retry reuses it
res = requests.post(
"https://app.zentarinetworks.com/api/v1/orders",
json={"type": "digital", "quantity": 10},
headers={"Authorization": f"Bearer {os.environ['AXIS_API_KEY']}", "Idempotency-Key": key},
)
var key = Guid.NewGuid().ToString(); // store it with the job, so a retry reuses it
using var request = new HttpRequestMessage(HttpMethod.Post, "orders")
{
Content = JsonContent.Create(new { type = "digital", quantity = 10 }),
};
request.Headers.Add("Idempotency-Key", key);
var res = await http.SendAsync(request);
What you get back
| Situation | Answer |
|---|---|
| First time with this key | Done as normal |
| Same key, same request, first one finished | The first answer again, with Idempotent-Replayed: true. Nothing is done twice |
| Same key, same request, first one still running | 409 idempotency_in_progress: wait a moment and retry |
| Same key, different request (method, path or body) | 422 idempotency_key_reused |
When to retry
| Answer | Retry? |
|---|---|
| No answer, a timeout, a dropped connection | Yes, with the same key |
429 | Yes, after Retry-After seconds |
500, 502, 503 | Yes, after a short pause (say 1s, then 5s, then 30s), with the same key |
409 idempotency_in_progress | Yes, after a moment |
Any other 4xx | No: change the request first (Errors) |
GET, PUT, PATCH and DELETE are naturally safe to repeat: doing them twice leaves things the same as doing them
once.