Retrying safely
A request can time out after the API has already sent the message. Add an Idempotency-Key header to every request in the table below. A retry with the same key gets the first answer back instead of doing the work twice.
Send the same request twice
This is the request that sends the Quickstart's message, with its key. Send it again with the same key and the same body:
curl -X POST "https://api.sms.codelia.studio/v1/organizations/$CODELIA_ACCOUNT_ID/messages" \
-H "Authorization: Bearer $CODELIA_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: order-1042-ready" \
-d '{
"to": [
"+442079460123"
],
"from": "InfoSMS",
"body": "Your order 1042 is ready to collect."
}'const response = await fetch(
`https://api.sms.codelia.studio/v1/organizations/${process.env.CODELIA_ACCOUNT_ID}/messages`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.CODELIA_API_KEY}`,
"Content-Type": "application/json",
"Idempotency-Key": "order-1042-ready",
},
body: JSON.stringify({
"to": [
"+442079460123"
],
"from": "InfoSMS",
"body": "Your order 1042 is ready to collect."
}),
},
);
console.log(response.status);
console.log(JSON.stringify(await response.json(), null, 2));Once the first request has finished, the answer is the first one again, with the header Idempotency-Replayed: true. The message isn't sent or charged a second time.
Which requests take a key
| Endpoint | What it does |
|---|---|
POST / | Send a message |
POST / | Send a message again |
POST / | Send or schedule a campaign |
PATCH / | Reschedule a campaign |
POST / | Cancel a campaign |
POST / | Resume a stalled campaign |
POST / | Send a campaign again |
POST / | Create a recurring campaign |
PATCH / | Change a recurring campaign |
DELETE / | Delete a recurring campaign |
POST / | Pause a recurring campaign |
POST / | Resume a recurring campaign |
POST / | Import contacts |
Every other endpoint an API key can call ignores the header. A request without it runs as usual, and nothing is kept.
What happens to a repeat
Keys are kept per organization and per endpoint. A repeat is compared with the first request on its path and its exact body:
- Same key, same path and body, after the first has finished: you get the first answer again, with
Idempotency-Replayed: true. - Same key while the first request is still running:
409withidempotency_, whatever the body. Retry shortly with the same key.conflict - Same key after the first request never finished:
409withidempotency_. After 15 minutes the error'sconflict error_says the outcome is unknown. See When the outcome is unknown.description - Same key, different body, once the first has finished:
422withidempotency_.key_ reuse - Same key on another path: the path is part of the request. Using one key to send two different messages again, on
POST /, answersorganizations/ {accountId}/ messages/ {messageId}/ resend 422once the first has finished. - 24 hours after the first request: the key is forgotten, and a request with it runs again.
- A key that's empty or longer than 255 characters:
400withinvalid_. Nothing is kept.idempotency_ key
What a repeat gets back
When the request reached the endpoint, its answer is kept, errors included. A retry with the same key after a 400, a 402 or a 500 gets that same error back and doesn't try again. So once you've fixed what an error asked for, like topping up after 402 insufficient_, send again with a new key.
A large campaign sent now is answered 202 before it's accepted, and that 202 is what a repeat gets back, even if the campaign fails once it's accepted. So check the campaign's status and failureReason rather than retrying. Once you've fixed the reason, send it again.
An answer given before the key is read isn't kept: a 401, a 403 for a permission the API key doesn't hold, the 404 for an account ID that isn't yours, and a 429. A retry of one of those with the same key runs the request.
Choose a key
- Derive it from your own event, like the order the message is about:
order-1042-ready. Then a timeout, a crash and restart, or a job that runs twice within 24 hours all send the same key. - Never make a new key for each attempt. A retry with a new key is a new request, and it sends again.
- Never reuse a key for something else. On the same endpoint, a different body is refused with
422, and what you meant to send isn't sent. - Use 1 to 255 characters.
When the outcome is unknown
If no answer reached you (a timeout, a dropped connection, or a 502 or 504 before the API answered), the request may have taken effect. Send it again with the same key and the same body:
- The first answer comes back: that's the result. Nothing was done twice.
409withidempotency_: the first request is still running, or never finished. Wait, then retry with the same key. If it keeps answeringconflict 409, check whether the request took effect before you use a new key: for a message, get the message or the campaign. The key stays taken until 24 hours after the first request.500withinternal_: the API kept that answer, so the same key returns it again. Check whether the request took effect, and send again with a new key only if it didn't.error
On the receiving end
Webhook events can arrive more than once. Each has an id, like evt_, which stays the same on every retry and every replay of that event. Use it as your own idempotency key: keep the IDs you've handled, and skip an event you've already seen. See Webhooks.
With the TypeScript SDK
The SDK's methods for these endpoints take an idempotencyKey, and make one for each call when you don't pass one. See TypeScript SDK.