# Webhook delivery

> Register an endpoint, verify signatures, inspect delivery attempts and replay missed events.


Each integration (a CRM that learns when a contract ends, a dunning system that learns
when a bill is sealed) registers its own webhook with its own key, tests it, sees what
was delivered and replays what it missed, without an operator or a restart.

Each service delivers its own events: register on **contractd** (`:9401`) for customers,
contracts and subscriptions, on **billd** (`:9402`) for bills. The calls below are the
same on both. To replace the caller’s bearer secret, reuse its name. The receiver keeps its signing secret, delivery cursor and history. [Rotate an API credential](../docs/services/#rotate-an-api-credential)

## 1. Register

```sh
curl -sS https://contractd.example/webhooks \
  -H "Authorization: Bearer $CRM_KEY" -H "Content-Type: application/json" \
  -H "Idempotency-Key: crm-webhook-1" \
  -d '{"name":"crm","url":"https://crm.example/telco","types":["contract","customer.moved"]}'
```

```json
{
  "name": "crm",
  "url": "https://crm.example/telco",
  "types": ["de.telco.contract.*", "de.telco.customer.moved"],
  "owner": "crm",
  "secret": "whsec_…",
  "note": "the secret is shown once; telco_events::webhook::verify checks deliveries with it"
}
```

- **The secret is shown once.** Store it where your receiver reads it. It is derived
  from the service's `TELCO_WEBHOOK_KEY`, the service, the name and the moment of
  creation — nothing secret is stored, and deleting and re-creating the webhook gives a
  new one.
- **It starts at registration.** The webhook receives what happens from then on; to fetch the
  history, [replay](#4-replay) from `0`.
- **Types** are patterns: a bare aggregate (`contract`) means all its types. Empty means
  every type — but never more than the key's own patterns allow: a key limited to
  `contract,customer` cannot receive a bill through a webhook it owns.
- **Only public `https` URLs.** Outside development, a URL that is not `https`, carries
  credentials, or names `localhost`, a private, loopback, link-local or CGNAT address or
  an internal single-label host is refused (`422 webhook.url-forbidden`). The address is
  checked again at every delivery, after DNS, so a name that later resolves inward is
  never connected to.
- A key owns at most 20 webhooks. A `read` key may not create one.

## 2. Test

```sh
curl -sS -X POST https://contractd.example/webhooks/crm/test -H "Authorization: Bearer $CRM_KEY"
```

```json
{ "delivered": true, "status": 204, "error": null, "ms": 41 }
```

The ping is a real, signed delivery of type `de.telco.test.ping` with
`data: {"webhook": "crm"}` — verify it as you will verify every event. It is never on
the feed and does not move the cursor.

## 3. Watch

`GET /webhooks/crm` shows `pending`, `attempts`, `last_error` and `paused`;
`GET /webhooks/crm/deliveries?limit=50` the last attempts, newest first:

```json
[
  { "seq": 812, "type": "de.telco.contract.ended", "at": "2026-11-20T06:00:03Z", "status": 204, "error": null, "ms": 38 },
  { "seq": 811, "type": "de.telco.contract.renewed", "at": "2026-11-20T06:00:02Z", "status": 503, "error": null, "ms": 12 }
]
```

An attempt that got no answer names why — `timeout`, `connect`, `address-not-public`
(the host resolves to a non-public address, so nothing was sent) — never more. Attempts are kept 30
days. A failing receiver is retried after 1 s, doubling to 5 min,
in order: the next event waits for the one before. `410 Gone` pauses the webhook until
a replay.

## 4. Replay

```sh
curl -sS -X POST https://contractd.example/webhooks/crm/replay \
  -H "Authorization: Bearer $CRM_KEY" -H "Content-Type: application/json" -d '{"after": 800}'
```

Everything after sequence `800` is delivered again, in order; `{"after": 0}` sends the
whole history. A replay only moves the cursor back, never forward, and un-pauses the
webhook. Your receiver de-duplicates on `id` — telco delivers at least once.

## 5. Receive

Every delivery is one CloudEvents envelope ([Events](../docs/events/)), signed per
[Standard Webhooks](https://www.standardwebhooks.com): `webhook-id`, `webhook-timestamp`,
`webhook-signature`. In Rust:

```rust,ignore
let secrets = [telco_events::webhook::Secret::parse(&std::env::var("CRM_WEBHOOK_SECRET")?)?];
telco_events::webhook::verify(&secrets, |h| headers.get(h).map(str::to_owned), &body, now_unix)?;
```

Any Standard Webhooks library verifies it too. Answer `2xx` quickly and do the work
after. To rehearse before your endpoint exists:

```sh
telco listen --webhook 127.0.0.1:9199 --secret whsec_…
```

Or let `telco listen --subscribe` register a webhook pointing at itself, print every
signed delivery, and delete the webhook when it ends — see [the CLI](../docs/cli/).

## 6. Delete

`DELETE /webhooks/crm` stops deliveries and forgets the cursor and the log.

## Who sees what

| Key role | Webhooks |
|---|---|
| `operator` | All of them, including the subscribers configured on the service (`TELCO_SUBSCRIBERS`, read-only here) |
| `integration` | Only its own; another key's webhook is `404` |
| `read` | Lists and reads its own; creates none |

Every call takes the key's name into the log; no secret and no event data is logged. The
full contract of every event is the [AsyncAPI document](../schemas/asyncapi.json).
