# Billing, contract and usage events

> Subscribe to signed CloudEvents and decode each payload with the event schemas or Rust types.


Use contract events to update a CRM, bill events to start reconciliation, and usage events to retrieve completed switch receipts.

## What an event looks like

A [CloudEvents 1.0](https://github.com/cloudevents/spec) envelope, the same on the pull
feed and on a contractd or billd webhook:

```json
{
  "specversion": "1.0",
  "id": "01a1e9c2-2e01-7423-a0a3-722983ab028c",
  "source": "urn:telco:billd",
  "type": "de.telco.bill.sealed",
  "subject": "bill/01a1e9c2-2e01-7423-a0a3-722983ab028c",
  "time": "2026-10-05T06:00:01Z",
  "datacontenttype": "application/json",
  "dataschema": "https://hupe1980.github.io/telco/schemas/events/de.telco.bill.sealed.json",
  "sequence": "42",
  "businessdate": "2026-10-05",
  "data": { "id": "…", "number": "RE-2026-000001", "kind": "invoice", "billing_account": "…" }
}
```

De-duplicate on `id`, order by `sequence` within one `source`. `businessdate` is the day
the fact belongs to; on a test clock it is not the day of `time`. `orderref`, when
present, is the seller's order reference: every event of a contract that came through
`POST /signups` carries it, so a shop matches events to its orders without a lookup
([Selling](../docs/selling/)).

## Every type's data has a schema

Each event's payload is a Rust struct in the `telco-events` crate, with a generated JSON
Schema (draft 2020-12). Find it

- at the event's `dataschema` URL,
- in `GET /event-types` on contractd and billd, with the meaning and the source (a
  shared profile a service consumes under any type, such as contractd's
  [`appointment@1`](../docs/appointments/#connect-a-field-system), is listed there by
  `profile`, `dataschema` and `route`),
- as a downloadable schema, such as [bill held](../schemas/events/de.telco.bill.held.json),
- in the terminal: `telco events schema bill.held`,
- all at once as an [AsyncAPI 3.0](https://www.asyncapi.com/docs/reference/specification/v3.0.0)
  document at [`/schemas/asyncapi.json`](../schemas/asyncapi.json)
  (`telco events asyncapi`): one message per type, the envelope, the signature headers —
  for code generators and event catalogues.

Every schema is strict (`additionalProperties: false`). Every event the services' test
suites emit is read back strictly into its type when the test ends, so a producer cannot
send a field the schema does not declare. `data` carries ids, states, dates and rule ids —
**never** a name, an address, a contact or an amount: those stay behind the API, read
under the reader's own rights, and a test refuses a payload field named like one.

**The promise.** A type is never renamed or removed; `data` only gains optional fields; a
fact that changes meaning gets a new type, emitted beside the old one until a release note
retires it.

## The types

| Type | Emitted by | Data |
|---|---|---|
| `de.telco.usage.batch-reconciled` | usaged | `id`, the completed voice batch. Operators and read-only integrations poll `GET /usage/events` and fetch the redacted receipt by UUID. |
| `de.telco.customer.created` | contractd | `id` |
| `de.telco.customer.moved` | contractd | `id`, `from` — read the new address from contractd |
| `de.telco.billing-account.created` | contractd | `id`, `customer` |
| `de.telco.billing-account.changed` | contractd | `id`, `field` (`payment_method`, `invoice_email`), `from` |
| `de.telco.contract.created` | contractd | `id`, `state` |
| `de.telco.contract.approved` | contractd | `id`, `state` — § 54 Abs. 3 TKG |
| `de.telco.contract.voided` | contractd | `id`, `state` |
| `de.telco.contract.termination-received` | contractd | `id`, `effective_on`, `basis`, `rule` |
| `de.telco.contract.renewal-notice-due` | contractd | `id`, `last_day_to_cancel`, `initial_term_end` — § 56 Abs. 3 TKG, once |
| `de.telco.contract.renewed` | contractd | `id`, `renewed_on`, `rule` |
| `de.telco.contract.term-extended` | contractd | `id`, `initial_months`, `rule` — § 66 Abs. 3 TKG |
| `de.telco.contract.ended` | contractd | `id`, `ended_on` |
| `de.telco.subscription.outage-credit-approved` | contractd | `id` (subscription), `case_id` identify the frozen outage award for billing |
| `de.telco.subscription.appointment-credit-approved` | contractd | `id` (subscription), `case_id` — billd reads the immutable award |
| `de.telco.subscription.created` | contractd | `id`, `contract`, `state` |
| `de.telco.subscription.activated` | contractd | `id`, `state` — billing starts |
| `de.telco.subscription.restricted` | contractd | `id`, `state` |
| `de.telco.subscription.restriction-lifted` | contractd | `id`, `state` |
| `de.telco.subscription.plan-changed` | contractd | `id`, `plan`, `book_version` |
| `de.telco.subscription.identifier-assigned` | contractd | `id`, `kind` — the value stays in contractd |
| `de.telco.subscription.termination-scheduled` | contractd | `id`, `state` |
| `de.telco.subscription.ended` | contractd | `id`, `state` |
| `de.telco.subscription.voided` | contractd | `id`, `state` |
| `de.telco.bill.sealed` | billd | `id`, `number`, `kind`, `billing_account` |
| `de.telco.bill.held` | billd | `billing_account`, `period`, `code`, `rules`, `fix` (`field`, `operation`) |
| `de.telco.bill.credited` | billd | `id`, `number`, `cites` |
| `de.telco.test.ping` | either | `webhook` — sent only by `POST /webhooks/{name}/test`, never on the feed |

## In Rust

Add the events crate and a JSON decoder to your Rust project:

```sh
cargo add telco-events --features webhook
cargo add serde_json
```

```rust,ignore
use telco_events::{CloudEvent, bill, contract, webhook};

// Standard Webhooks: HMAC-SHA256 over id.timestamp.body, the code telco signs with.
let secrets = [webhook::Secret::parse(&std::env::var("TELCO_WEBHOOK_SECRET")?)?];
webhook::verify(&secrets, |h| headers.get(h).map(str::to_owned), &body, now_unix)?;

let event: CloudEvent = serde_json::from_slice(&body)?;
match event.kind.as_str() {
    bill::SEALED => {
        let sealed: bill::Sealed = event.data()?; // refused if the type or the data differ
        println!("{} sealed for {}", sealed.number, sealed.billing_account);
    }
    contract::TERMINATION_RECEIVED => {
        let t: contract::TerminationReceived = event.data()?;
        println!("ends on {}", t.effective_on);
    }
    _ => {}
}
```

The crate does no I/O and reads no clock: `verify` takes your `now`. With the `schema`
feature, `telco_events::schema(kind)` returns any type's schema.

## Receiving them

**Pull.** Read `GET /events` on contractd and billd or `GET /usage/events` on usaged. Store the returned `next` cursor per service and send it as `after` on the next request. To filter the billing feed, add `types=de.telco.bill.*`.

**Push.** Register a webhook on contractd or billd
(`POST /webhooks`, [the webhooks guide](../docs/webhooks/)): it gets its signing secret
once, a test ping, a delivery log and replay, and sees only the types its key may see. An
operator can also configure subscribers on the emitting service
(`TELCO_SUBSCRIBERS=crm=https://crm.example/telco`). Deliveries are signed, in order per
subscriber, retried with back-off, and a `410 Gone` pauses the subscriber
([the services](../docs/services/#delivery-and-the-inbox)).

## Watching them

`telco listen` follows the contract, billing and usage feeds as things happen — one line per event, each aggregate in
its colour:

```text
$ just listen bill contract
● following contractd, billd and usaged (de.telco.bill.*, de.telco.contract.*) — Ctrl-C ends it
17:04:10 02.11.2027  contract.renewed                   contract/…3fe46a0c            renewed_on=2027-11-01 rule=DE-TKG-56-3-renewed
17:04:10 02.11.2027  bill.sealed                        bill/…e5fefc50                billing_account=…1bb2981b kind=invoice number=RE-2027-000021
17:04:10 02.11.2027  bill.held                          billing-account/…4670fc2a     code=bill.einvoice-invalid fix=invoice_email period=2027-11 rules=PEPPOL-EN16931-R010
```

- `--types bill,contract.ended` filters (a bare aggregate means all its types);
  `--json` prints the envelopes, one per line, for `jq`; `--all` starts from the
  beginning.
- `--webhook 127.0.0.1:9199 --secret whsec_…` receives deliveries instead, so you can point
  a subscriber at it and rehearse your endpoint: each signature is checked, a forged
  delivery is answered `401` and shown as refused, and control characters in event text
  are shown escaped.
- `telco events list` prints every type with its source and meaning.
