# Webhooks for backup events

Send a failed or overdue backup, a finished restore and other backup events to endpoints of your own, signed so you can check they came from Brewless.


## What a webhook is for

The owners and admins of your organisation get a mail when a backup fails, misses items or is overdue. A webhook sends the same moments, and a few more, to an address of your own: a chat channel's incoming webhook, your incident tool or your own monitoring. The mail keeps going; a webhook comes on top of it.

> [!NOTE]
> Webhooks for backup events are being rolled out. Until Brewless has switched them on, the **Webhooks** tab is not there and the endpoints below answer `404`.

## Adding an endpoint

**Backups** in the sidebar, **Webhooks** tab, **Add endpoint**. Enter an HTTPS address and choose the events it hears; choose none and it hears every event. An organisation can have up to 5 endpoints. Owners and admins add, change and remove them; every member who sees applications sees them.

Brewless delivers only to an `https://` address with a host name, without a user name or password in it, and only when that name points at the public internet: a name that resolves to a private, loopback or otherwise reserved address is refused when you add it, and again at every delivery. Brewless connects to exactly the address it checked and does not follow a redirect.

After adding, the console shows the endpoint's **signing secret** once. Store it with your endpoint. **Show secret** shows it again later; that is recorded in the audit trail (`backup.webhook_secret_revealed`). For a new secret, remove the endpoint and add it again.

**Send a test** sends a `ping` at once and says how the endpoint answered. **Turn off** keeps the endpoint but sends it nothing.

## The events

| Event | When |
| --- | --- |
| `backup.succeeded` | A backup run succeeded, every item in it |
| `backup.partial` | A backup run ended with at least one item failed |
| `backup.failed` | A backup run failed |
| `backup.overdue` | No backup succeeded within the schedule and its grace period: once per incident |
| `destination.failed` | The test of a destination of your own failed |
| `restore.finished` | A restore or download finished |
| `restore.failed` | A restore or download failed |
| `recovery_key.expired` | A recovery key was not confirmed within 24 hours and is being removed again |
| `repository.rotated` | A repository was rotated to a new key |
| `restore_test.failed` | An automatic restore test did not check out in full: partly failed or failed |
| `restore_test.leftovers` | The hourly check removed temporary databases a restore test left behind |
| `disaster_recovery.finished` | A disaster recovery finished (not sent yet, see below) |
| `disaster_recovery.failed` | A disaster recovery failed (not sent yet, see below) |
| `ping` | **Send a test**; an endpoint cannot choose it |

An endpoint can already choose `disaster_recovery.finished` and `disaster_recovery.failed`, but Brewless does not send them yet: they follow with disaster recovery's own events.

## A delivery

A `POST` with a JSON body:

```json
{
  "version": 1,
  "id": "01mabc0000000000000000000",
  "event": "backup.partial",
  "at": "2026-12-03T02:41:12Z",
  "organisation": {"id": "coding-agency", "name": "Coding Agency"},
  "application": {"id": "01m4…", "name": "Shop"},
  "environment": {"id": "01m4…", "name": "Production"},
  "console_url": "https://coding-agency.brewless.eu/applications/01m4…/environments/01m4…/backups",
  "data": {
    "run_id": "59ee2b10-3c4d-4e5f-8a9b-0c1d2e3f4a5b",
    "status": "partial",
    "trigger": "schedule",
    "started_at": "2026-12-03T02:30:01+00:00",
    "finished_at": "2026-12-03T02:41:12+00:00",
    "bytes_read": 1830291002,
    "bytes_added": 20482211,
    "items": 3,
    "failed": ["shop-files: source_denied"],
    "error_code": null
  }
}
```

`data` holds names, counts, sizes, times and error codes, never a key, a variable value, a row or an object key. A `ping` has `application` and `environment` set to `null`. A field may be added to a delivery; one is never removed or changed in meaning without a new `version`.

The headers:

| Header | Value |
| --- | --- |
| `X-Brewless-Event` | The event, as in the body |
| `X-Brewless-Delivery` | The delivery's `id`; the same on every attempt of that delivery |
| `X-Brewless-Timestamp` | Unix seconds when this attempt was sent |
| `X-Brewless-Signature` | `sha256=` and the hex HMAC-SHA256 of `timestamp + "." + body`, with the signing secret |
| `User-Agent` | `Brewless-Webhooks/1` |

## Checking the signature

Compute the HMAC over the timestamp header, a dot and the raw body, compare it in constant time, and refuse a timestamp more than five minutes from your own clock:

```php
$timestamp = $request->header('X-Brewless-Timestamp');
$expected = 'sha256='.hash_hmac('sha256', $timestamp.'.'.$request->getContent(), $secret);

abort_unless(hash_equals($expected, (string) $request->header('X-Brewless-Signature')), 401);
abort_if(abs(time() - (int) $timestamp) > 300, 401);
```

Handle a delivery once per `X-Brewless-Delivery`: an attempt that timed out at your end may arrive again.

## When an endpoint does not answer

An answer in the 2xx range counts as delivered. Anything else, or no answer within 10 seconds, is tried again after 30 seconds, 2 minutes, 10 minutes, an hour and 6 hours. After the last attempt the delivery is given up and counts in the endpoint's failures, which the tab shows with the last error. A successful delivery sets the count back to 0.
