SMS engineering

Handling SMS Webhooks Reliably: Idempotency, Retries, and Security

How to receive SMS and WhatsApp status webhooks without losing or double-counting events: fast replies, queues, idempotency, ordering, and signatures.

Updated · 3 min read

Delivery reports usually reach you as webhooks: your provider sends an HTTP request to your server every time a message changes state. Webhooks look simple, but production traffic exposes every shortcut. Messages get counted twice, statuses go backwards, and a slow endpoint causes retries that pile up.

Respond fast, process later

Your webhook endpoint should do as little as possible before replying:

  1. Verify the request is genuine.
  2. Store the raw event or put it on a queue.
  3. Return 200 OK immediately.

Do the real work (updating message status, triggering fallback, notifying users) in a background worker. If your endpoint takes several seconds, providers time out and retry, and you receive the same event again.

Expect duplicates: make processing idempotent

Providers retry when they do not get a fast success response, and sometimes send the same event twice anyway. Design for it:

  • Each event has a message ID and a status, and often an event ID or timestamp.
  • Store processed event IDs, or use a unique constraint on (message_id, status).
  • Make every handler safe to run twice: "set status to delivered" is safe; "increment delivered count" is not, unless guarded.

Expect events out of order

A "delivered" event can arrive before "sent", especially across retries. Never assume order:

  • Give each status a rank: queued < sent < delivered, with failed, undelivered, and expired as final states.
  • Only move a message forward in rank, never backwards.
  • Once a message reaches a final state, ignore later non-final events.
const rank = { queued: 1, sent: 2, delivered: 3, undelivered: 3, failed: 3, expired: 3 }

function applyStatus(message, event) {
  if (rank[event.status] <= rank[message.status]) return message // stale or duplicate
  return { ...message, status: event.status, updatedAt: event.timestamp }
}

Verify the sender

Anyone who learns your webhook URL can send fake events. Protect it:

  • Verify the provider's signature header, usually an HMAC of the body with a shared secret, and compare in constant time.
  • Reject requests with old timestamps to prevent replays.
  • Use HTTPS only.
  • Avoid relying on IP allow-lists alone; provider IPs change.

Handle your own downtime

If your endpoint is down, most providers retry for a period, then give up. To avoid gaps:

  • Monitor webhook error rates and latency.
  • Use a provider API or dashboard to re-fetch statuses for messages that never reached a final state.
  • Treat "no final status after N hours" as unknown, not delivered.

What to do with the data

  • Update message status for support agents.
  • Trigger failover to WhatsApp when an OTP is not delivered in time.
  • Feed dashboards by country and network.
  • Alert on sudden drops in delivery rate.

See SMS delivery reports explained for what each status means.