Why blind retries are dangerous
With SMS a retry is not free. When a request times out, the client knows nothing about the outcome: maybe the request never arrived, maybe the SMS already went out. Repeating the call "to be safe" in the second case means two texts to the recipient and two charges on the balance.
The classic case is not even a hand-written retry: it is an HTTP library with automatic retries on by default. Against an API that moves money, every retry must be explicit and idempotent.
The Idempotency-Key header
The fix is one header: Idempotency-Key, with a value you choose that identifies that one logical send. Nothing else in the request changes.
curl
curl -X POST https://sms-jet.com/api/v1/sms/transactional \
-H "Authorization: Bearer gsms_…" \
-H "Idempotency-Key: order-1042" \
-H "Content-Type: application/json" \
-d '{ "to": "+393331234567", "text": "…", "sender_type": "system" }'Replay semantics
On the first send the key is recorded together with a fingerprint of the request body. From then on: same key and same body → the original response is returned again, with no new send and no new charge. This includes definitive errors: they replay identically.
Same key with a different body → 409 IDEMPOTENCY_CONFLICT. That is protection, not friction: a key identifies one message, not a reusable slot.
409 Conflict
409 Conflict
{
"code": "IDEMPOTENCY_CONFLICT",
"message": "This Idempotency-Key was already used with a different request body"
}SEND_OUTCOME_UNKNOWN: the outcome is pending
There is a third case: a previous attempt with that key started but never resolved (a timeout or a crash mid-flight). The API does not know the outcome and refuses to guess: it answers 503 SEND_OUTCOME_UNKNOWN.
What to do: do not retry blindly and do not switch keys — with a fresh key the double send becomes possible again. Check the message log, or retry later with the same key: once the outcome resolves, the retry gets it as a replay.
503 Service Unavailable
503 Service Unavailable
{
"code": "SEND_OUTCOME_UNKNOWN",
"message": "A previous attempt with this Idempotency-Key has not been resolved yet. Do not retry blindly — poll the message log or retry later with the same key."
}Choosing keys
The right key usually already exists in your system:
- One key per logical message, not per attempt: order-1042 for the confirmation of order 1042, identical on every retry.
- Never random values generated per attempt: a fresh key on every retry switches idempotency off exactly when you need it.
- Derive it from an identifier you already own: the order id, the queue-row id, the event id.
- Keys are scoped per app: two different apps can use the same value without interfering.