# Webhooks

> Get signed HTTP notifications when emails are delivered, bounce or fail.

Source: https://sndr.sh/docs/webhooks

Add an endpoint in **Webhooks** (or with `POST /v1/webhooks`) and choose its events. SNDR sends each event as a signed `POST` with a JSON body.

## Events

| Event | Sent when |
| --- | --- |
| email.delivered | The recipient’s mail server accepted the email. |
| email.bounced | The address doesn’t exist (permanent failure). It’s added to your suppression list. |
| email.failed | The email couldn’t be sent, for example a rejection or signing problem. |
| email.unsubscribed | A recipient used the unsubscribe link. The address is added to your suppression list. |

Payload:

```json
{
  "id": "evt_3f9a1c2b7d4e",
  "type": "email.delivered",
  "created_at": "2026-01-15T10:30:04Z",
  "data": {
    "email_id": "eml_01HXKQ8B3N",
    "to": ["customer@example.com"]
  }
}
```

Deliveries are retried on failure, so the same event can arrive twice. Use `id` to process each event once.

## Verify the signature

Each request has an `X-Sndr-Signature` header: `t=<unix time>,v1=<hex HMAC-SHA256>`. The HMAC is computed over `t`, a dot, and the raw body, with your endpoint’s secret. Always check it before trusting the payload.

SDK:

```js
import { verifyWebhookSignature } from '@rkiza/sndr';

// rawBody: the exact request body, before JSON.parse
const valid = verifyWebhookSignature(
  rawBody,
  request.headers.get('x-sndr-signature') ?? '',
  process.env.SNDR_WEBHOOK_SECRET,
);
if (!valid) return new Response('Invalid signature', { status: 401 });
```

Node.js:

```js
import { createHmac, timingSafeEqual } from 'node:crypto';

// header: "t=1736937004,v1=5f2b..." (v1s appears while a new secret is staged)
function verify(rawBody, header, secret) {
  const parts = header.split(',').map((p) => p.trim().split('='));
  const t = parts.find(([k]) => k === 't')?.[1];
  if (!t || Math.abs(Date.now() / 1000 - Number(t)) > 300) return false;
  const expected = createHmac('sha256', secret).update(t + '.' + rawBody).digest();
  return parts.some(([k, sig]) => k.startsWith('v1') && /^[0-9a-f]{64}$/i.test(sig ?? '') &&
    timingSafeEqual(expected, Buffer.from(sig, 'hex')));
}
```

> **Note:** Reject requests whose `t` is more than 5 minutes old to stop replays.

## The secret

The full secret is shown once, when you create the endpoint (`secret` in the API response). Lists only show `secret_prefix`, which can’t be used to verify.

## Rotate the secret

### 1. Stage a new secret

Open the endpoint’s **Signing keys** and choose **Generate replacement secret**. Requests are now signed with both secrets (`v1` and `v1s`).

### 2. Accept both

Update your receiver to accept either secret.

### 3. Promote

Choose **Make primary**. The old secret stops signing new deliveries.
