# Database webhooks

When a row of a chosen table is added, changed or deleted, Rezin Core can send a signed message to an address you choose.

> **Important.** A webhook sends your project's data outside Rezin Core, to the address you give it. Only an owner of the workspace can set one up, and a message carries the row's key only unless you choose "include the row".

## Set one up

In the console open the project's **Webhooks** page.

- Choose the table, which events (added, changed, deleted) and the address.
- The table needs a primary key.
- The address must be public `https`, given by name. Private, local and link-local addresses are refused when it is saved and again at every send.
- The webhook's **secret** is shown once, when it is made. It starts with `whsec_`. Copy it then.
- A project can have up to 10 webhooks.
- **Send a test** posts a message of type `test`.

Only an owner can make, change or remove a webhook. Each can be switched off without removing it.

## The message

```
POST <your address>
content-type: application/json
user-agent: Rezin-Core-Webhooks/1
x-rezin-signature: t=1795700000,v1=<hex>
x-rezin-webhook-id: <webhook id>
x-rezin-delivery-id: <delivery id>
```

```json
{
  "id": "<delivery id>",
  "type": "row.insert",
  "project": "abcdefghijklmnopqrst",
  "table": "orders",
  "at": "2026-11-26T15:04:05.000Z",
  "key": { "id": "9c1f…" }
}
```

| Field | Meaning |
|---|---|
| `id` | The delivery. The same one may arrive twice; use it to ignore repeats. |
| `type` | `row.insert`, `row.update`, `row.delete`, or `test` |
| `project` | The project ID |
| `table` | The table the row is in |
| `at` | When the change happened |
| `key` | The row's primary key |
| `row` | The row itself. Only present when "include the row" is chosen. |

## Check the signature

Always check it before trusting a message.

1. Read `t` and `v1` from `x-rezin-signature`.
2. Compute HMAC-SHA256 of the text `<t>.<raw body>` with the webhook's secret, as hex.
3. Compare it with `v1` in constant time.
4. Refuse a `t` more than a few minutes old.

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

function verify(rawBody, header, secret) {
  const parts = Object.fromEntries(header.split(',').map((p) => p.split('=')));
  if (Math.abs(Date.now() / 1000 - Number(parts.t)) > 300) return false;
  const want = createHmac('sha256', secret).update(`${parts.t}.${rawBody}`).digest('hex');
  const a = Buffer.from(want), b = Buffer.from(parts.v1 ?? '');
  return a.length === b.length && timingSafeEqual(a, b);
}
```

Use the body exactly as it arrived, before any JSON parsing.

## Answering, and retries

Answer `2xx` within 10 seconds. Anything else, or no answer in time, is tried again after 1, 5, 30, 120 and 360 minutes and then given up on.

- Redirects are not followed.
- The body of your answer is not read.
- Messages for one webhook go out in the order the changes happened.
- The same delivery may arrive more than once. Make your handler safe to run twice.

## The delivery log

The **Webhooks** page shows what was sent and how your server answered: the time, the event, the status and any error. The messages themselves are not kept.

## Send to one of your functions instead

On the Webhooks page you can choose one of the project's [functions](https://rezincore.com/docs/functions.md) in place of an outside address. The function receives the same signed message, and the message never leaves Rezin Core.

## Good practice

- Leave "include the row" off and fetch the row from the API with the key, unless you have a reason. Then the message carries nothing private.
- Never point a webhook at an address you do not control.
- Do the work after you have answered, so a slow job does not cause a retry.
