# Webhooks

A webhook is an address of yours that gets a signed request when something happens to an email.

## Add an endpoint

Give a public `https://` address that takes a POST, and the events you want. Without `events` the endpoint gets every event. An IP address, `localhost` and private names are refused with `webhook_url`.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/webhooks" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "url": "https://example.com/hooks/mail",
  "events": [
    "email.delivered",
    "email.bounced"
  ]
}'
```

TypeScript:

```ts
await opensend.webhooks.create({
  url: 'https://example.com/hooks/mail',
  events: ['email.delivered', 'email.bounced'],
})
```

CLI:

```bash
opensend webhooks create \
  --url https://example.com/hooks/mail \
  --events email.delivered,email.bounced
```

MCP:

```json
{
  "name": "webhooks_create",
  "arguments": {
    "url": "https://example.com/hooks/mail",
    "events": [
      "email.delivered",
      "email.bounced"
    ]
  }
}
```

The answer holds the `secret` that signs every request to this endpoint. It is shown this once, so store it now.

webhooks.create:

```json
{
  "object": "webhook",
  "id": "wh_02",
  "url": "https://example.com/hooks/mail",
  "events": [
    "email.delivered",
    "email.bounced"
  ],
  "status": "enabled",
  "format": "native",
  "lastDeliveryAt": null,
  "lastStatus": null,
  "createdAt": "2026-11-27T09:30:00.000Z",
  "secret": "whsec_08d12dfda708a7ae4540215fe3779b10"
}
```

## What your endpoint gets

Each event is one POST with a JSON body and these headers.

```http
content-type: application/json
user-agent: OpenSend-Webhooks/1
opensend-signature: t=1795771800,v1=80db0d952157c00a6643299af13e738f4e2943f1f747cab20abb93272c91ef4f
opensend-event: email.delivered
opensend-delivery: evt_07:wh_01
```

- `opensend-signature`: the time and the signature of the request.
- `opensend-event`: the type of the event.
- `opensend-delivery`: the id of this delivery. It is the same on every retry, so use it to ignore a repeat.

The body has the id of the event, its `type`, when it happened, and `data`. For an email event `data` holds the id of the email, its sender, recipients, subject and tags, and a `detail`.

```json
{
  "id": "evt_07",
  "type": "email.delivered",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "emailId": "em_12",
    "from": "Acme <hello@mail.example.com>",
    "to": [
      "ada@example.com"
    ],
    "subject": "Your receipt for November",
    "tags": {
      "category": "receipt"
    },
    "detail": "250 2.0.0 OK",
    "createdAt": "2026-11-27T09:30:00.000Z",
    "templateId": null,
    "broadcastId": null,
    "mode": "live"
  }
}
```

## Check the signature

Anyone can post to a public address, so check that a request is yours before you trust it. The SDK does it in one call, on the raw body exactly as it arrived.

```ts
import { OPENSEND_SIGNATURE_HEADER, OpenSendError, verifyWebhook } from '@open-send/opensend'

export default {
  async fetch(request: Request): Promise<Response> {
    const payload = await request.text()
    try {
      const event = await verifyWebhook({
        payload,
        signature: request.headers.get(OPENSEND_SIGNATURE_HEADER),
        secret: process.env.WEBHOOK_SECRET ?? '',
      })
      if (event.type === 'email.bounced') {
        console.log(event.data.emailId, event.data.detail)
      }
      return new Response('ok')
    } catch (error) {
      if (error instanceof OpenSendError) return new Response(error.message, { status: 400 })
      throw error
    }
  },
}
```

`verifyWebhook` returns the event, or throws with the code `invalid_signature`. It also refuses a request older than five minutes, so a captured request cannot be replayed later. `toleranceSeconds` changes that window.

Without the SDK, check it by hand.

1. Read `t` and `v1` from the header, which looks like `t=1795771800,v1=1ba91f...`.
2. Join `t`, a dot and the raw body: `1795771800.{"id":"evt_07",...}`.
3. Compute the HMAC-SHA256 of that text with your secret as the key, as lowercase hex.
4. Compare it with `v1` in constant time, and refuse the request when `t` is more than five minutes away from now.

> The example request above is real. Signed with the secret `whsec_8ff347392e2ac0eacc623a9b6a99b44c`, its body gives exactly the signature shown, so you can test your own check against it.

## Answers and retries

Answer with any `2xx` status within 5 seconds. Anything else counts as a failure, and so does a redirect: it is not followed.

A failed delivery is tried again after 1 min, 5 min, 30 min, 2 h, 8 h. After 6 tries it is marked `failed` and left alone.

Every delivery is kept for 30 days with its state, the status your endpoint answered with, and what went wrong.

cURL:

```bash
curl "$OPENSEND_API_URL/v1/webhooks/wh_01/deliveries" \
  -H "Authorization: Bearer $OPENSEND_API_KEY"
```

TypeScript:

```ts
await opensend.webhooks.deliveries('wh_01')
```

CLI:

```bash
opensend webhooks deliveries wh_01
```

MCP:

```json
{
  "name": "webhooks_deliveries",
  "arguments": {
    "id": "wh_01"
  }
}
```

## Try it out

Post one signed sample event to an endpoint, and get back what the endpoint answered.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/webhooks/wh_01/test" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
```

TypeScript:

```ts
await opensend.webhooks.test('wh_01')
```

CLI:

```bash
opensend webhooks test wh_01
```

MCP:

```json
{
  "name": "webhooks_test",
  "arguments": {
    "id": "wh_01"
  }
}
```

To stop an endpoint without losing it, set its `status` to `disabled`.

cURL:

```bash
curl -X PATCH "$OPENSEND_API_URL/v1/webhooks/wh_01" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "status": "disabled"
}'
```

TypeScript:

```ts
await opensend.webhooks.update('wh_01', {
  status: 'disabled',
})
```

CLI:

```bash
opensend webhooks update wh_01 --status disabled
```

MCP:

```json
{
  "name": "webhooks_update",
  "arguments": {
    "id": "wh_01",
    "status": "disabled"
  }
}
```

## Every event

`email.sent`: The email left for the receiving mail server.

```json
{
  "id": "evt_06",
  "type": "email.sent",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "emailId": "em_11",
    "from": "Acme <hello@mail.example.com>",
    "to": [
      "ada@example.com"
    ],
    "subject": "Your receipt for November",
    "tags": {
      "category": "receipt"
    },
    "detail": "",
    "createdAt": "2026-11-27T09:30:00.000Z",
    "templateId": null,
    "broadcastId": null,
    "mode": "live"
  }
}
```

`email.delivered`: The receiving mail server took the email. `detail` is what it answered.

```json
{
  "id": "evt_07",
  "type": "email.delivered",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "emailId": "em_12",
    "from": "Acme <hello@mail.example.com>",
    "to": [
      "ada@example.com"
    ],
    "subject": "Your receipt for November",
    "tags": {
      "category": "receipt"
    },
    "detail": "250 2.0.0 OK",
    "createdAt": "2026-11-27T09:30:00.000Z",
    "templateId": null,
    "broadcastId": null,
    "mode": "live"
  }
}
```

`email.delayed`: The receiving mail server asked to try again later. `detail` is what it answered.

```json
{
  "id": "evt_08",
  "type": "email.delayed",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "emailId": "em_13",
    "from": "Acme <hello@mail.example.com>",
    "to": [
      "ada@example.com"
    ],
    "subject": "Your receipt for November",
    "tags": {
      "category": "receipt"
    },
    "detail": "451 4.7.1 Try again later.",
    "createdAt": "2026-11-27T09:30:00.000Z",
    "templateId": null,
    "broadcastId": null,
    "mode": "live"
  }
}
```

`email.bounced`: The receiving mail server refused the email for good. `detail` is its reason.

```json
{
  "id": "evt_09",
  "type": "email.bounced",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "emailId": "em_14",
    "from": "Acme <hello@mail.example.com>",
    "to": [
      "ada@example.com"
    ],
    "subject": "Your receipt for November",
    "tags": {
      "category": "receipt"
    },
    "detail": "550 5.1.1 The email account does not exist.",
    "createdAt": "2026-11-27T09:30:00.000Z",
    "templateId": null,
    "broadcastId": null,
    "mode": "live"
  }
}
```

`email.opened`: The recipient opened the email. Sent when open tracking is on for the domain.

```json
{
  "id": "evt_10",
  "type": "email.opened",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "emailId": "em_15",
    "from": "Acme <hello@mail.example.com>",
    "to": [
      "ada@example.com"
    ],
    "subject": "Your receipt for November",
    "tags": {
      "category": "receipt"
    },
    "detail": "",
    "createdAt": "2026-11-27T09:30:00.000Z",
    "templateId": null,
    "broadcastId": null,
    "mode": "live"
  }
}
```

`email.clicked`: The recipient followed a link. `detail` is the link. Sent when click tracking is on for the domain.

```json
{
  "id": "evt_11",
  "type": "email.clicked",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "emailId": "em_16",
    "from": "Acme <hello@mail.example.com>",
    "to": [
      "ada@example.com"
    ],
    "subject": "Your receipt for November",
    "tags": {
      "category": "receipt"
    },
    "detail": "https://example.com/receipts/november",
    "createdAt": "2026-11-27T09:30:00.000Z",
    "templateId": null,
    "broadcastId": null,
    "mode": "live"
  }
}
```

`email.complained`: The recipient marked the email as spam.

```json
{
  "id": "evt_12",
  "type": "email.complained",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "emailId": "em_17",
    "from": "Acme <hello@mail.example.com>",
    "to": [
      "ada@example.com"
    ],
    "subject": "Your receipt for November",
    "tags": {
      "category": "receipt"
    },
    "detail": "",
    "createdAt": "2026-11-27T09:30:00.000Z",
    "templateId": null,
    "broadcastId": null,
    "mode": "live"
  }
}
```

`email.failed`: The email could not be sent at all. `detail` says why.

```json
{
  "id": "evt_13",
  "type": "email.failed",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "emailId": "em_18",
    "from": "Acme <hello@mail.example.com>",
    "to": [
      "ada@example.com"
    ],
    "subject": "Your receipt for November",
    "tags": {
      "category": "receipt"
    },
    "detail": "The message was refused before it left.",
    "createdAt": "2026-11-27T09:30:00.000Z",
    "templateId": null,
    "broadcastId": null,
    "mode": "live"
  }
}
```

`email.received`: An email arrived on one of your domains. `data.id` is the id of the received email.

```json
{
  "id": "evt_14",
  "type": "email.received",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "id": "in_02",
    "from": "grace@customer.example",
    "to": "support@mail.example.com",
    "subject": "A question about my invoice"
  }
}
```

`email.scheduled`: An email was scheduled, or moved to a new time. `detail` is the time it is due.

```json
{
  "id": "evt_04",
  "type": "email.scheduled",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "emailId": "em_10",
    "from": "Acme <hello@mail.example.com>",
    "to": [
      "ada@example.com"
    ],
    "subject": "Your receipt for November",
    "tags": {
      "category": "receipt"
    },
    "detail": "2026-12-04T09:00:00.000Z",
    "createdAt": "2026-11-27T09:30:00.000Z",
    "templateId": null,
    "broadcastId": null,
    "mode": "live"
  }
}
```

`email.canceled`: A scheduled email was canceled before it was sent.

```json
{
  "id": "evt_05",
  "type": "email.canceled",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "emailId": "em_10",
    "from": "Acme <hello@mail.example.com>",
    "to": [
      "ada@example.com"
    ],
    "subject": "Your receipt for November",
    "tags": {
      "category": "receipt"
    },
    "detail": "Cancelled before it was sent.",
    "createdAt": "2026-11-27T09:30:00.000Z",
    "templateId": null,
    "broadcastId": null,
    "mode": "live"
  }
}
```

`automation.entered`: A person entered an automation. `data.runId` is the id of their run.

```json
{
  "id": "evt_run_04_entered",
  "type": "automation.entered",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "automationId": "auto_01",
    "automation": "Tell the CRM",
    "runId": "run_04",
    "email": "ada@example.com",
    "contactId": "con_486cf92a1e02a43d335e2ff1",
    "source": "manual",
    "status": "running",
    "reason": null
  }
}
```

`automation.finished`: A run ended. `data.status` says how, and `data.reason` why when it ended early.

```json
{
  "id": "evt_run_04_finished",
  "type": "automation.finished",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "automationId": "auto_01",
    "automation": "Tell the CRM",
    "runId": "run_04",
    "email": "ada@example.com",
    "contactId": "con_486cf92a1e02a43d335e2ff1",
    "source": "manual",
    "status": "completed",
    "reason": null
  }
}
```

`automation.step`: A webhook step of an automation ran. `data.step` names it and `data.data` is what started the run.

```json
{
  "id": "evt_run_04_tell_crm",
  "type": "automation.step",
  "createdAt": "2026-11-27T09:30:00.000Z",
  "data": {
    "automationId": "auto_01",
    "automation": "Tell the CRM",
    "runId": "run_04",
    "stepId": "tell_crm",
    "step": "Tell the CRM",
    "email": "ada@example.com",
    "contactId": "con_486cf92a1e02a43d335e2ff1",
    "source": "manual",
    "data": {
      "plan": "pro"
    }
  }
}
```
