Webhooks

Get a signed event whenever mail arrives, and check its signature before you trust it.

Set one up

  1. On a paid plan, an admin opens Settings → Webhooks and chooses Add webhook.
  2. Give an https:// URL on a public host, on port 443, 80 or 1024 and above. Redirects aren't followed.
  3. To get only some mail, give a domain, an address pattern such as signup-*@qa.acme.test, or both.
  4. Copy the signing secret, whsec_ and 43 more characters. It's shown once.
  5. Send test event from the webhook's deliveries, and see how it went there.

The event

Mayfly posts a message.received event whenever a mail finishes being read, whether that worked or failed. It carries the mail's summary, never its body or attachments: read those with the API, by message.id. A test event is webhook.test, with message null.

The event's body
{
  "id": "6f1c2a0e-7b3d-4e59-a8c1-2d9f4b6e8a03",
  "type": "message.received",
  "createdAt": "2026-11-14T09:30:12.902Z",
  "message": {
    "id": "0b6d7e52-3c1f-4a8e-9f2d-5c4b3a291e10",
    "to": "signup-1@<your-name>.qa.aravaltech.info",
    "envelopeFrom": "bounce@mail.example.test",
    "headerFrom": "Example App <hello@example.test>",
    "subject": "Your verification code",
    "receivedAt": "2026-11-14T09:30:12.418Z",
    "expiresAt": "2026-11-15T09:30:12.418Z",
    "sizeBytes": 4182,
    "parseStatus": "parsed",
    "otp": { "value": "482910", "confidence": "high" }
  }
}
Its headers
POST /your/endpoint HTTP/1.1
Content-Type: application/json
User-Agent: Mayfly-Webhooks/1
Mayfly-Event-Id: 6f1c2a0e-7b3d-4e59-a8c1-2d9f4b6e8a03
Mayfly-Event-Type: message.received
Mayfly-Signature: t=1794650400,v1=71c2ded449a1e3d70d7aa70adda7145a8cef84b84ba622833bf537b89e628d66

Check the signature

v1 is the HMAC-SHA256 of t, a dot and the raw body, keyed with your whole secret, whsec_ included, in lowercase hex. Compute it over the body before parsing it, compare in constant time, and refuse a t more than 5 minutes from your clock, so a captured event can't be sent again.

verify.mjs
import { createHmac, timingSafeEqual } from "node:crypto";

// True when Mayfly signed this body with your secret in the last 5 minutes.
export function verifyMayfly(rawBody, header, secret) {
  const parts = header.split(",").map((part) => part.trim());
  const t = parts.find((part) => part.startsWith("t="))?.slice(2) ?? "";
  const signatures = parts
    .filter((part) => part.startsWith("v1="))
    .map((part) => part.slice(3));
  if (!/^\d+$/.test(t) || signatures.length === 0) return false;
  if (Math.abs(Date.now() / 1000 - Number(t)) > 300) return false;

  const expected = createHmac("sha256", secret).update(`${t}.`).update(rawBody).digest();
  return signatures.some((hex) => {
    const given = Buffer.from(hex, "hex");
    return given.length === expected.length && timingSafeEqual(given, expected);
  });
}
server.mjs
import { createServer } from "node:http";
import { verifyMayfly } from "./verify.mjs";

const seen = new Set(); // keep these somewhere lasting in a real receiver

createServer(async (request, response) => {
  const chunks = [];
  for await (const chunk of request) chunks.push(chunk);
  const body = Buffer.concat(chunks); // the raw body, before any JSON parsing

  const signature = request.headers["mayfly-signature"] ?? "";
  if (!verifyMayfly(body, signature, process.env.MAYFLY_WEBHOOK_SECRET)) {
    response.writeHead(400).end();
    return;
  }
  const event = JSON.parse(body);
  if (!seen.has(event.id)) {
    seen.add(event.id);
    // event.message is the mail's summary; read the rest with the API, by event.message.id.
  }
  response.writeHead(204).end();
}).listen(8080);
verify.py
import hashlib
import hmac
import time


def verify_mayfly(raw_body: bytes, header: str, secret: str) -> bool:
    parts = [part.strip() for part in header.split(",")]
    t = next((part[2:] for part in parts if part.startswith("t=")), "")
    signatures = [part[3:] for part in parts if part.startswith("v1=")]
    if not t.isdigit() or not signatures or abs(time.time() - int(t)) > 300:
        return False
    signed = t.encode() + b"." + raw_body
    expected = hmac.new(secret.encode(), signed, hashlib.sha256).hexdigest()
    return any(hmac.compare_digest(expected, given) for given in signatures)

Retries and rotation

  • Answer with any 2xx within 10 seconds. Anything else is tried again: 8 attempts over about half an hour.
  • An event can arrive more than once, or out of order. Skip a Mayfly-Event-Id you've handled.
  • After 20 failures in a row, the first over an hour ago, the webhook is turned off. Turn it on again from Edit.
  • Rotate secret makes a new one. For 24 hours each event carries two v1= entries, the new secret's first, and either verifies. Then the old secret stops working.
NextTroubleshooting