New: every plan now includes free incoming texts and rollover credits. See pricing →
Search Help Center Documentation

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-Key header. 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 keyYou get
with the same method, path and body, after the first request finishedThe first answer again, same status and body, with the header Idempotent-Replayed: true. Nothing is done twice.
while the first request is still running409 idempotency_in_progress. Wait a moment and retry.
with a different path or body422 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-Keystring

Any 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.

Limits: up to 200 characters.

Example "2b5c4f8e-order-1042".

All 28 endpoints that take an Idempotency-Key