# Events and webhooks

Instead of asking again and again whether something happened, a client can be told: by a live **event stream** it keeps open, or by **webhooks** the server posts to.

Both carry the same events: logins, file transfers, shares, forms, jobs and their steps, admin changes, security events (bans, refusals), the server starting and stopping, AS2 and SLA alerts. An event is cut to the users the caller may see, and secrets in it are masked.

```json
{
  "id": "48213",
  "category": "file",
  "time": "2026-11-18T13:44:43.120Z",
  "data": { "...": "the event's own fields" }
}
```

The categories: `auth`, `file`, `form`, `job`, `jobstep`, `share`, `admin`, `security`, `server`, `as2`, `sla`.

## The stream

`GET /events/stream` holds the connection open and sends each event as it happens, as Server-Sent Events (`text/event-stream`):

```bash
curl -N -H "Authorization: Bearer $TOKEN" "$API/events/stream?categories=file,security"
```

- `categories`: the ones you want, comma separated; all of them when left out.
- `max_seconds` (default 3600, at most a day) and `max_events` end the stream; reconnect to go on.
- **Missed nothing:** a client that reconnects with the `Last-Event-ID` header (or `last_event_id`) gets the events after that one, from the last 1000.

Reading the stream is a sensitive permission: events name users, paths and addresses.

## Webhooks

A webhook is a stored subscription: a URL, the categories, and a secret. The server posts each event of those categories to the URL as JSON:

```bash
curl -X PUT -u admin -H "Content-Type: application/json" \
  -d '{"url": "https://hooks.example.com/crushmft", "categories": ["file", "security"]}' \
  $API/webhooks/siem-bridge
```

Without a `secret` the server makes one and shows it once, in that answer. `POST /webhooks/{id}/test` sends a test event; `GET /webhooks` shows each one's last delivery, status and counts.

Each post carries these headers:

| Header | Value |
|---|---|
| `X-CrushMFT-Event` | The category. |
| `X-CrushMFT-Delivery` | The event's id: the same id again means a retry of the same event. |
| `X-CrushMFT-Webhook` | The webhook's id. |
| `X-CrushMFT-Signature` | `sha256=` and the HMAC-SHA256 of the body with the webhook's secret, in hex. |

**Check the signature** before trusting a post, over the raw body as it came:

```python
import hmac, hashlib

def genuine(body: bytes, header: str, secret: str) -> bool:
    want = "sha256=" + hmac.new(secret.encode(), body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(want, header)
```

A post that does not get a 2xx answer is tried again after 2 seconds, 10 seconds, 1 minute, 5 minutes and 30 minutes, then given up and logged. Answer quickly and do the work afterwards.

> [!NOTE]
> A webhook to a loopback, link-local or cloud metadata address can be set only by an administrator. For log collection the server's log targets (syslog, SQL, files) already push structured events; webhooks are for everything else.

[[openapi API/openapi.json tag=events]]
