Guide
Idempotency
Send an Idempotency-Key with a POST, and retrying it can never do the work twice.
Why it matters#
When a request times out you cannot tell whether it reached us. Retrying a plain POST might send the same text twice, or add a contact twice. With an Idempotency-Key, the retry gets the first answer back instead of doing the work again.
How to use it#
- Make a unique key for each new piece of work, such as one text or one contact. A UUID works well; up to 200 characters.
- Send it in the
Idempotency-Keyheader. If you retry that same request, send the same key and the same body. - Keys last 24 hours and belong to your account, so every API key on the account shares them.
curl -X POST https://app.smsblast.net/v1/messages \
-H "Authorization: Bearer $SMSBLAST_API_KEY" \
-H "Idempotency-Key: $(openssl rand -hex 16)" \
-H "Content-Type: application/json" \
-d '{
"to": "+14155550101",
"body": "Your table for 2 is booked for 7pm."
}'const res = await fetch('https://app.smsblast.net/v1/messages', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.SMSBLAST_API_KEY}`,
'Idempotency-Key': crypto.randomUUID(),
'Content-Type': 'application/json',
},
body: JSON.stringify({
to: '+14155550101',
body: 'Your table for 2 is booked for 7pm.',
}),
});
console.log(res.status, await res.json());import os
import uuid
import requests
res = requests.post(
"https://app.smsblast.net/v1/messages",
headers={
"Authorization": f"Bearer {os.environ['SMSBLAST_API_KEY']}",
"Idempotency-Key": str(uuid.uuid4()),
},
json={
"to": "+14155550101",
"body": "Your table for 2 is booked for 7pm.",
},
)
print(res.status_code, res.json())Retrying a timed-out request with the same key:
// Make the key once per text, and reuse it on every retry of that text.
const key = crypto.randomUUID();
async function send() {
return fetch('https://app.smsblast.net/v1/messages', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.SMSBLAST_API_KEY}`,
'Idempotency-Key': key,
'Content-Type': 'application/json',
},
body: JSON.stringify({ to: '+14155550101', body: 'Your table for 2 is booked for 7pm.' }),
});
}
let res;
try {
res = await send();
} catch {
res = await send(); // timed out: safe to retry, the text cannot go out twice
}
console.log(res.status, res.headers.get('Idempotent-Replayed'), await res.json());import os
import uuid
import requests
# Make the key once per text, and reuse it on every retry of that text.
key = str(uuid.uuid4())
def send():
return requests.post(
"https://app.smsblast.net/v1/messages",
headers={
"Authorization": f"Bearer {os.environ['SMSBLAST_API_KEY']}",
"Idempotency-Key": key,
},
json={"to": "+14155550101", "body": "Your table for 2 is booked for 7pm."},
timeout=30,
)
try:
res = send()
except requests.RequestException:
res = send() # timed out: safe to retry, the text cannot go out twice
print(res.status_code, res.headers.get("Idempotent-Replayed"), res.json())What a retry gets#
| When you send the same key | You get |
|---|---|
| with the same method, path and body, after the first request finished | The first answer again, same status and body, with the header Idempotent-Replayed: true. Nothing is done twice. |
| while the first request is still running | 409 idempotency_in_progress. Wait a moment and retry. |
| with a different path or body | 422 idempotency_key_reused. Use a new key for new work. |
after the first request failed with a server error (5xx) | The request runs again. Server errors are not remembered, so they can be retried. |
after the first request was refused (4xx) | The same refusal. If you change the request to fix it, use a new key. |
“The same body” means the same bytes: resend the JSON string you sent the first time rather than building it again with fields in a different order.
Which requests take a key#
Every POST that changes something accepts one: 28 of the 29 POST endpoints. PATCH and DELETE requests ignore the header. The header itself:
Idempotency-KeystringAny unique string up to 200 characters (a UUID works well). If a POST times out or you are not sure it went through, send it again with the same key and the same body: you get the first answer back (with Idempotent-Replayed: true) instead of the work being done twice. Keys are kept for 24 hours per account.
All 28 endpoints that take an Idempotency-Key
- POST
/v1/messagesSend a text - POST
/v1/messages/previewPreview a message - POST
/v1/contactsAdd a contact - POST
/v1/contacts/importImport contacts - POST
/v1/contacts/bulkAct on many contacts - POST
/v1/importsStart an import job - POST
/v1/imports/{id}/rowsSend a batch of rows - POST
/v1/imports/{id}/finishFinish an import job - POST
/v1/listsCreate a list - POST
/v1/segmentsCreate a segment - POST
/v1/segments/previewCount a segment before saving - POST
/v1/campaignsCreate a campaign draft - POST
/v1/campaigns/estimateEstimate a campaign - POST
/v1/campaigns/testSend a test text - POST
/v1/campaigns/{id}/duplicateDuplicate a campaign - POST
/v1/campaigns/{id}/cancelStop a campaign - POST
/v1/campaigns/{id}/recurringMake a campaign recurring - POST
/v1/campaigns/{id}/sendSend or schedule a campaign - POST
/v1/workflowsCreate a workflow - POST
/v1/workflows/{id}/enrollEnroll contacts in a workflow - POST
/v1/formsCreate a signup form - POST
/v1/conversationsText someone from the inbox - POST
/v1/conversations/{id}/messagesReply in a conversation - POST
/v1/keywordsSet up a keyword - POST
/v1/webhooksCreate a webhook - POST
/v1/webhooks/{id}/rotate-secretRotate the signing secret - POST
/v1/webhooks/{id}/testSend a test event - POST
/v1/webhooks/{id}/deliveries/{deliveryId}/retrySend a delivery again