# Alerts webhooks

Source: https://help.ruuster.com/idx/alerts-webhooks

> The alerts webhook events (delivery.ready, buyer and saved-search changes, sent alerts and email events), how to choose them, verify their signature and handle retries.

Your alerts webhook is one `https` URL on your server that gets signed events about your alerts.
Set it, and choose its events, in your alert settings:

```js
await fetch("https://idx.ruuster.com/v1/alerts/settings", {
  method: "PATCH",
  headers: { Authorization: `Bearer ${process.env.IDX_SECRET_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({
    webhookUrl: "https://www.example-homes.com/api/alerts-webhook",
    webhookEvents: ["delivery.ready", "buyer.unsubscribed", "email.clicked"],
  }),
});
```

The answer carries `webhookSecret` the first time you set the URL. It is shown once: store it as
`RUUSTER_ALERTS_WEBHOOK_SECRET`. `rotateWebhookSecret: true` makes a new one; the old one keeps
working for 24 hours so you can deploy the new one first.

## The events

| Event | When | `data` |
|---|---|---|
| `delivery.ready` | An alert is ready to send | `delivery`: the whole delivery object |
| `delivery.expired` | An alert had no result for 72 hours | `deliveryId`, `kind`, `buyerId`, `searchId` |
| `buyer.created` | You added a buyer | `buyer` |
| `buyer.unsubscribed` | A buyer unsubscribed (link, one-click, bounce, complaint or your call) | `buyer`, `reason` |
| `buyer.resubscribed` | A buyer was turned back on | `buyer` |
| `saved_search.created` | A saved search was added | `buyer`, `search` |
| `saved_search.updated` | A saved search changed or was unsubscribed | `buyer`, `search` |
| `saved_search.deleted` | A saved search was deleted | `buyer`, `search` |
| `alert.sent` | You reported an alert as sent | `deliveryId`, `kind`, `buyer`, `searchId`, `providerMessageId` |
| `email.delivered`, `email.opened`, `email.clicked`, `email.bounced`, `email.complained`, `email.unsubscribed` | You reported that event | `deliveryId`, `buyer`, `searchId`, `machine`, `url` |

`buyer` is `{ id, externalId, email }`; `search` is `{ id, externalId, name, frequency, status }`.
The default is `delivery.ready` only.

## The request

Each event is a `POST` with a JSON body `{ id, kind, occurredAt, data }` and these headers:

| Header | What it holds |
|---|---|
| `Ruuster-Event-Id` | The event's id: the same event always has the same id |
| `Ruuster-Event-Kind` | The event's kind |
| `Ruuster-Delivery-Id` | This webhook delivery's id |
| `Ruuster-Signature` | `t=<unix seconds>,v1=<signature>` (a second `v1` during a rotation) |

## Verify the signature

The signature is HMAC-SHA256 of `<t>.<raw body>` with your secret, in hex. Compare it in constant
time and refuse a `t` more than five minutes old:

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

export function verifySignature(raw, header, secret) {
  if (!header) return false;
  const parts = Object.fromEntries(header.split(",").map((p) => p.split("=")).filter((p) => p[0] === "t"));
  const t = Number(parts.t);
  if (!t || Math.abs(Date.now() / 1000 - t) > 300) return false;
  const expected = crypto.createHmac("sha256", secret).update(`${t}.${raw}`).digest("hex");
  return header.split(",").filter((p) => p.startsWith("v1=")).some((p) => {
    const got = Buffer.from(p.slice(3));
    const want = Buffer.from(expected);
    return got.length === want.length && crypto.timingSafeEqual(got, want);
  });
}
```

## Retries

Answer with any 2xx within ten seconds. Any other answer is retried after 1 minute, 5 minutes, 30
minutes, 2 hours, 6 hours and 12 hours. Events can arrive more than once and out of order, so use
`Ruuster-Event-Id` to skip one you already handled. After 20 failed attempts in a row we turn the
webhook off; set `webhookUrl` again to turn it back on. Alerts keep waiting in
`GET /v1/deliveries` while it is off.
