# Webhooks

Instead of polling, register an HTTPS endpoint and Mail for Bots will `POST` a signed JSON event to it when something happens. Manage webhooks in the webmail under **Settings, Webhooks** (add an endpoint, pick events, send a test, see the delivery log), or over REST with the endpoints below. Webhooks need the `webhooks:manage` scope and a key that is not restricted to some inboxes; in a team, the admin role.

Create a webhook

```
curl -X POST "https://mailforbots.com/api/v1/webhooks" \
  -H "Authorization: Bearer $BOTMAIL_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "url": "https://example.com/mailforbots", "events": ["message.received"] }'
```

The response includes the signing `secret` (`whsec_...`) once; store it. `POST /webhooks/:id/rotate` issues a new one. The optional `inbox_id` (id or address) limits a webhook to the events of one inbox, and an account can have up to 20 webhooks. URLs must use `https://`, can't contain credentials and must resolve to a public address (checked when you save and again on every attempt).

## Events

| Event | Sent when | data |
| --- | --- | --- |
| `message.received` | A message arrives in an inbox. | `inbox` { id, address }, `message` (the summary, as in message lists; fetch the body with `GET /messages/:id`) |
| `message.sent` | A message is sent from an inbox (send, reply, forward or a sent draft). | `inbox`, `message`, `actor_id` (the person, or the creator of the API key, who sent it) |
| `message.bounced` | A message you sent bounced (only with external sending through SES). | `message_id`, `thread_id`, `subject`, `provider_message_id`, `bounce_type` (`permanent` or `transient`), `recipients` |
| `thread.labeled` | Labels were added to or removed from a conversation, one event per conversation. | `thread_id`, `inbox_id`, `added` and `removed` (lists of `{ id, name }`), `source` (`user`, `agent` or `filter`) |
| `draft.created` | An agent (an API key) created a draft for a human to review. | `draft`, `actor_id` |
| `ping` | You called `POST /webhooks/:id/test`. Not a subscribable event. | `webhook_id`, `message` |

## Payload

Every delivery is a `POST` with a JSON body of the form `{ id, type, created_at, data }` and these headers:

| Header | Value |
| --- | --- |
| `botmail-signature` | `t=<unix seconds>,v1=<hex HMAC>`, see below |
| `botmail-event` | The event type. |
| `botmail-delivery` | The delivery id. It stays the same across retries; the body's id identifies the event. |
| `content-type` | `application/json`; the user agent is `botmail-webhooks/1.0`. |

Example: message.received

```
{
  "id": "evt_Qh3kVx9yTq2LmN8a",
  "type": "message.received",
  "created_at": "2026-01-05T10:15:00.000Z",
  "data": {
    "inbox": { "id": "INBOX_ID", "address": "agent@mailforbots.com" },
    "message": {
      "id": "MESSAGE_ID",
      "inbox_id": "INBOX_ID",
      "thread_id": "THREAD_ID",
      "direction": "inbound",
      "from_address": "no-reply@example.com",
      "to": ["agent+github@mailforbots.com"],
      "subject": "Your verification code",
      "snippet": "Your code is 123456",
      "has_attachments": false,
      "read": false,
      "created_at": "2026-01-05T10:15:00.000Z"
    }
  }
}
```

## Verify the signature

`v1` is the hex HMAC-SHA256, keyed with your webhook secret, of the string `<t>.<raw request body>`. Compute it over the exact bytes you received (parse JSON only afterwards), compare in constant time, and reject timestamps that are far from now to stop replays.

Verify a delivery (Node, Express)

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

const app = express();

// Keep the raw body: the signature covers the exact bytes that were sent.
app.post("/mailforbots", express.raw({ type: "application/json" }), (req, res) => {
  const header = req.get("botmail-signature") ?? "";
  const { t, v1 } = Object.fromEntries(header.split(",").map((kv) => kv.split("=")));
  const body = req.body.toString("utf8");

  const expected = crypto
    .createHmac("sha256", process.env.BOTMAIL_WEBHOOK_SECRET)
    .update(t + "." + body)
    .digest("hex");

  const valid = v1?.length === expected.length && crypto.timingSafeEqual(Buffer.from(v1), Buffer.from(expected));
  const fresh = Math.abs(Date.now() / 1000 - Number(t)) < 300; // reject replays older than 5 minutes
  if (!valid || !fresh) return res.sendStatus(400);

  const event = JSON.parse(body);
  if (event.type === "message.received") console.log("New mail:", event.data.message.subject);

  res.sendStatus(200); // answer fast; deduplicate on event.id
});

app.listen(3000);
```

## Delivery, retries and disabling

- Any `2xx` response counts as delivered. Non-2xx responses, redirects (not followed), connection errors and no answer within 10 seconds are failures. Respond quickly and do the work afterwards.
- A failed delivery is retried with backoff, after 1 minute, 5 minutes, 30 minutes, 2 hours and 6 hours: six attempts in total. After that it stays `failed`. Delivery is at least once, so deduplicate on the event `id`.
- After 20 consecutive failed attempts the webhook is switched off (`enabled: false`, with a `disabled_reason`) and its queued deliveries are marked failed. Turn it back on in Settings or with `PATCH /webhooks/:id` `{ "enabled": true }`, which resets the failure count. A success resets the count too.
- Test pings are sent once and never count towards the 20.
- `GET /webhooks/:id/deliveries` shows recent attempts with the response status and the first 1,000 characters of the response body. Delivery logs are kept for 30 days.

## Endpoints

All under `https://mailforbots.com/api/v1`; request and response details are in the [REST API](https://mailforbots.com/docs/rest) reference.

| Endpoint | Purpose |
| --- | --- |
| `GET /webhooks` | List webhooks. |
| `POST /webhooks` | Create one; returns the secret once. |
| `GET /webhooks/:id` | Get one, with its failure count and last error. |
| `PATCH /webhooks/:id` | Change url, events, inbox_id or enabled. |
| `DELETE /webhooks/:id` | Delete it and its delivery log. |
| `POST /webhooks/:id/rotate` | New signing secret, returned once. |
| `POST /webhooks/:id/test` | Send a ping now and return the delivery. |
| `GET /webhooks/:id/deliveries` | Recent deliveries (limit 1 to 100). |

If you can't expose an endpoint, MCP `wait_for_message` and REST `GET /api/v1/inboxes/:inbox/wait` block until a matching message arrives (up to 300 seconds; the REST call returns `408` on timeout).

## Guides

- [How to test password-reset emails in CI](https://mailforbots.com/blog/test-password-reset-emails-in-ci)
- [Disposable inboxes for testing sign-up and password-reset emails](https://mailforbots.com/email-testing-for-developers)
- [How to QA email flows in 2026 with AI](https://mailforbots.com/blog/how-to-qa-email-flows-with-ai-2026)
