# Events

> What YOU(th) tells your system, and what each delivery carries.

When something happens in your organization, to one of its members or to one of its access codes,
YOU(th) tells your system with an event: a signed `POST` to the HTTPS address you registered, for
each event you subscribed to. A delivery says what happened and where to read it.

## Events

| `event_type`          | Sent when                                         | Resource         |
| --------------------- | ------------------------------------------------- | ---------------- |
| `AccessCode.redeemed` | A person redeemed one of your access codes        | The access code  |
| `Access.ended`        | The access a code started has ended               | The access code  |
| `Checkup.completed`   | Preview: a member of yours completed a checkup    | `Bundle` (FHIR)  |
| `Patient.registered`  | Preview: a person registered as a member of yours | `Patient` (FHIR) |

> **Preview:** `Checkup.completed` and `Patient.registered` are not available yet. What this page
> says of them, and of reading a checkup or a member from a `resource_url`, is proposed and may
> change. `AccessCode.redeemed` and `Access.ended` are final, and are sent to the endpoints that
> take them.

Each event type documents its own resource. New event types can appear: answer `2xx` to one you
do not handle.

A webhook endpoint is one address of yours that takes deliveries: its HTTPS address, and the events
it receives. Your backend creates it with the API: see
[Manage your endpoints](/docs/webhooks/endpoints). Each endpoint has its own signing secret, shown
once when it is created. An organization can have several endpoints, and your
[sandbox](/docs/sandbox) has its own.

## The body

Every event shares one envelope. A checkup's, a preview, looks like this:

```json
{
  "event_id": "evt_e560fc965cf89b3d5a8c3ed27406806f",
  "event_type": "Checkup.completed",
  "occurred_at": "2026-10-01T09:30:00.375000Z",
  "patient_id": "cfefb422-ac79-4cb9-a849-e80f8a6f21cd",
  "checkup_id": "64d866f3-0c88-485c-bfe6-3339df1d224a",
  "resource_url": "https://api.youth-platform.io/v1/checkups/64d866f3-0c88-485c-bfe6-3339df1d224a/fhir"
}
```

| Field            | Means                                                                                                                                                                                                                            |
| ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `event_id`       | The event, `evt_` and 32 hex digits. The same on every retry: deduplicate on it                                                                                                                                                  |
| `event_type`     | From the table above                                                                                                                                                                                                             |
| `occurred_at`    | When what the event reports happened, ISO 8601 in UTC: see below                                                                                                                                                                 |
| `patient_id`     | The member; in `Checkup.completed` and `Patient.registered`                                                                                                                                                                      |
| `checkup_id`     | The checkup; only in `Checkup.completed`                                                                                                                                                                                         |
| `access_code_id` | The access code's id; in `AccessCode.redeemed` and `Access.ended`                                                                                                                                                                |
| `batch_id`       | The code's batch; in `AccessCode.redeemed` and `Access.ended`                                                                                                                                                                    |
| `reason`         | Why the access ended; only in `Access.ended`                                                                                                                                                                                     |
| `resource_url`   | Where to read the resource: the checkup's bundle, the member's `Patient` or the access code, in the read-API version your integration is set to, today `/v1`. You can also build it from the ids: see [Versions](/docs/versions) |

- `occurred_at` is when what the event reports happened, not when it was sent:
  - `Checkup.completed`: the checkup's completion, which is the `issued` of its bundle's
    `DiagnosticReport` and what the [catch-up list](/docs/webhooks/catch-up#list-what-you-missed)'s
    `completed_from` filters on;
  - `Patient.registered`: the member's registration;
  - `AccessCode.redeemed`: the redemption, which is the code's `redeemed_at`;
  - `Access.ended`: the end you set with [`PUT …/access`](/docs/access-codes/end-access), or the
    end of the code's term.
- Most events are sent within seconds, but one can come later than its `occurred_at`: an
  `Access.ended` comes once the app has ended the access, within about 10 minutes of its end, later
  when YOU(th) can't reach the app; and, rarely, an `AccessCode.redeemed` comes up to a day after
  the redemption.
- `AccessCode.redeemed` and `Access.ended` name a code and never a person: they have no
  `patient_id`. See [their examples](#accesscoderedeemed).
- `Patient.registered` has no `checkup_id`; its `resource_url` points at the `Patient`.
- Ids are opaque strings. Store them as they are; do not rely on their length or format.
- New fields and new event types can appear. Ignore fields you do not know; answer `2xx` to an event
  you do not handle. A change that could break your receiver comes as a new event type, never as a
  change to one you receive: see [Versions](/docs/versions).

## What a delivery carries

Ids, never the resource: your backend reads it from `resource_url` with an
[access token](/docs/authentication). A delivery sent to a wrong address, or kept in a gateway's or
a queue's logs, then holds no personal or health data, and every read is checked against your
integration's scopes when it happens.

- An access event names a code and its batch, never a person.
- A checkup's or a member's event is sent only for a member who consented to share their data with
  your organization, and reading its `resource_url` takes the scope `checkups:read` or
  `patients:read`: see [Health data](/docs/credentials#health-data).
- A checkup's bundle is built once, when the event happens: reading `resource_url` answers the
  same bundle every time.
- Reading a member's `resource_url` answers the `Patient` as it is now.

## The request

|           |                                                                             |
| --------- | --------------------------------------------------------------------------- |
| Method    | `POST`, to your HTTPS address, on port 443                                  |
| Body      | JSON, compact, keys sorted, UTF-8. Verify the raw bytes; never re-serialize |
| Success   | Any `2xx` within ten seconds                                                |
| Redirects | Not followed. A `3xx` counts as a failure that is not retried               |
| Order     | None. Deliveries can arrive in any order and more than once                 |

Headers, as [Standard Webhooks](https://www.standardwebhooks.com/) names them (their names are
case-insensitive):

| Header              | Holds                                                                                            |
| ------------------- | ------------------------------------------------------------------------------------------------ |
| `Content-Type`      | `application/json`                                                                               |
| `webhook-id`        | The `event_id`. The same on every retry: deduplicate on it                                       |
| `webhook-timestamp` | When this attempt was signed, in Unix seconds                                                    |
| `webhook-signature` | `v1,<signature>`, or several separated by spaces: see [Verify a delivery](/docs/webhooks/verify) |

YOU(th) adds two headers of its own, to route and log a delivery before you read its body:

| Header                   | Holds                                               |
| ------------------------ | --------------------------------------------------- |
| `youth-event-type`       | The `event_type`, such as `Checkup.completed`       |
| `youth-delivery-attempt` | Which attempt this is: `1` for the first, up to `8` |

They are not signed. Act on the verified body, and trust it over them if they ever differ.

Verify every delivery before you act on it, and answer fast: see
[Retries and duplicates](/docs/webhooks/retries).

## Examples

The bodies below are pretty-printed for reading. On the wire a body is compact, with its keys
sorted, as in the first example; verify the bytes as you received them.

### A whole request

This request is really signed: the fixture secret on [Verify a delivery](/docs/webhooks/verify#try-it)
verifies it.

```http
POST /youth-webhook HTTP/1.1
Host: partner.example.com
Content-Type: application/json
webhook-id: evt_e560fc965cf89b3d5a8c3ed27406806f
webhook-timestamp: 1790847002
webhook-signature: v1,f/69pGXkwyZXFdzr/vJ44kTIC3ThgR+du6f4olitEV0=
youth-event-type: Checkup.completed
youth-delivery-attempt: 1

{"checkup_id":"64d866f3-0c88-485c-bfe6-3339df1d224a","event_id":"evt_e560fc965cf89b3d5a8c3ed27406806f","event_type":"Checkup.completed","occurred_at":"2026-10-01T09:30:00.375000Z","patient_id":"cfefb422-ac79-4cb9-a849-e80f8a6f21cd","resource_url":"https://api.youth-platform.io/v1/checkups/64d866f3-0c88-485c-bfe6-3339df1d224a/fhir"}
```

### `Patient.registered`

```json
{
  "event_id": "evt_3b9e0f6a1c2d4e5f8a7b6c5d4e3f2a1b",
  "event_type": "Patient.registered",
  "occurred_at": "2026-10-01T08:12:40.120000Z",
  "patient_id": "cfefb422-ac79-4cb9-a849-e80f8a6f21cd",
  "resource_url": "https://api.youth-platform.io/v1/patients/cfefb422-ac79-4cb9-a849-e80f8a6f21cd/fhir"
}
```

### `AccessCode.redeemed`

```json
{
  "event_id": "evt_3b9f2c1a7d4e4f6a8c5b0e1d2f3a4b5c",
  "event_type": "AccessCode.redeemed",
  "occurred_at": "2026-12-12T09:30:00.375000Z",
  "access_code_id": "0193b1c2-7a4e-7c1d-9f0a-2b6d5e8f1a34",
  "batch_id": "0193b1c0-11aa-7e02-8c55-9d3f0a7b6c21",
  "resource_url": "https://api.youth-platform.io/v1/access-codes/0193b1c2-7a4e-7c1d-9f0a-2b6d5e8f1a34"
}
```

`occurred_at` is the redemption: the time to bill from. `access_code_id` is the `id` you got with
the code when you [read the batch](/docs/access-codes/order#read-the-codes).

### `Access.ended`

```json
{
  "event_id": "evt_7c1e5a9b3d2f4a6c8e0b1d3f5a7c9e2b",
  "event_type": "Access.ended",
  "occurred_at": "2027-01-12T00:00:00.000000Z",
  "access_code_id": "0193b1c2-7a4e-7c1d-9f0a-2b6d5e8f1a34",
  "batch_id": "0193b1c0-11aa-7e02-8c55-9d3f0a7b6c21",
  "reason": "ended_by_partner",
  "resource_url": "https://api.youth-platform.io/v1/access-codes/0193b1c2-7a4e-7c1d-9f0a-2b6d5e8f1a34"
}
```

`reason` is `ended_by_partner` or `term_over`: see
[What happens then](/docs/access-codes/end-access#what-happens-then). New reasons can appear.
