Track email delivery with webhooks: know right away when mail is delivered, bounces or gets reported

· 2 min read

Use webhooks to receive email.delivered, email.bounced and email.complained events in real time, with signature verification code, deduplication and ideas for using the status in your app.

Track email delivery with webhooks

When the send API returns an id, the email was accepted, but you don't yet know whether it reached the inbox. A webhook is how your provider pushes the outcome to your own system the moment something changes, so you never have to poll.

Events worth handling

Event Happens when What to do
email.delivered The receiving server accepted the email Mark it delivered
email.delivery_delayed The receiver asked to try again later Wait, retries are automatic
email.bounced Permanent rejection, such as an unknown user Ask the user to fix the address, stop sending to it
email.complained The recipient reported it as spam Stop marketing email to that person
email.failed It couldn't be sent, for example all recipients are suppressed Check failed.reason

Set up an endpoint

  1. Create an https URL in your app, for example /webhooks/bytesms
  2. In the Dashboard, go to Webhooks, add the endpoint and choose the events
  3. Copy the signing secret (starts with whsec_) into an environment variable
  4. Click Send test event to try it

The payload is JSON with type, created_at and data. data includes the same email_id you got from the API, so you can match it to an order or user right away.

Verify the signature before trusting the data

Anyone can POST to your URL, so check the bytesms-signature header: an HMAC-SHA256 of the timestamp followed by the raw body. Compute it over the raw bytes, before parsing JSON.

import crypto from "node:crypto";
import express from "express";

const app = express();
const SECRET = process.env.BYTESMS_WEBHOOK_SECRET;

app.post("/webhooks/bytesms", express.raw({ type: "application/json" }), (req, res) => {
  const parts = Object.fromEntries(
    (req.get("bytesms-signature") ?? "").split(",").map((kv) => kv.split("=", 2)),
  );
  const t = Number(parts.t);
  if (!t || Math.abs(Date.now() / 1000 - t) > 300) return res.sendStatus(400);
  const expected = crypto.createHmac("sha256", SECRET).update(`${t}.${req.body}`).digest("hex");
  const ok = parts.v1?.length === expected.length &&
    crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(parts.v1));
  if (!ok) return res.sendStatus(400);

  const event = JSON.parse(req.body.toString("utf8"));
  // Store the status by event.type and event.data.email_id
  res.sendStatus(200);
});

Already using the svix library or Resend's webhook helpers? They verify the svix-* headers with the same secret.

Events can repeat or arrive out of order

  • Reply 2xx within 10 seconds. Otherwise the delivery is retried with growing delays, up to 8 attempts
  • Deduplicate on the svix-id header, which stays the same on every retry
  • Order by created_at, not by arrival
  • Queue heavy work, such as notifying your team, instead of doing it inside the webhook request

Putting it to work

  • An admin page that shows whether each customer's order confirmation arrived
  • A signup form that warns the user right away when the email bounces, since they probably mistyped it
  • Syncing people who complained to your CRM so marketing stops

More articles