Skip to content
Codelia SMSDocs
Guides: Retrying safely

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."
}'

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

EndpointWhat it does
POST /organizations/{accountId}/messagesSend a message
POST /organizations/{accountId}/messages/{messageId}/resendSend a message again
POST /organizations/{accountId}/messages/campaignsSend or schedule a campaign
PATCH /organizations/{accountId}/messages/campaigns/{campaignId}Reschedule a campaign
POST /organizations/{accountId}/messages/campaigns/{campaignId}/cancelCancel a campaign
POST /organizations/{accountId}/messages/campaigns/{campaignId}/resumeResume a stalled campaign
POST /organizations/{accountId}/messages/campaigns/{campaignId}/resendSend a campaign again
POST /organizations/{accountId}/messages/recurringCreate a recurring campaign
PATCH /organizations/{accountId}/messages/recurring/{id}Change a recurring campaign
DELETE /organizations/{accountId}/messages/recurring/{id}Delete a recurring campaign
POST /organizations/{accountId}/messages/recurring/{id}/pausePause a recurring campaign
POST /organizations/{accountId}/messages/recurring/{id}/resumeResume a recurring campaign
POST /organizations/{accountId}/contacts/importImport 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: 409 with idempotency_conflict, whatever the body. Retry shortly with the same key.
  • Same key after the first request never finished: 409 with idempotency_conflict. After 15 minutes the error's error_description says the outcome is unknown. See When the outcome is unknown.
  • Same key, different body, once the first has finished: 422 with idempotency_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 /organizations/{accountId}/messages/{messageId}/resend, answers 422 once 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: 400 with invalid_idempotency_key. Nothing is kept.

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_credits, 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.
  • 409 with idempotency_conflict: the first request is still running, or never finished. Wait, then retry with the same key. If it keeps answering 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.
  • 500 with internal_error: 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.

On the receiving end

Webhook events can arrive more than once. Each has an id, like evt_0a8d…, 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.