# Change Webhooks

Verzi POSTs the archive change feed to your HTTPS endpoint. Every event
carries its receipt: the document id, the version, and the sha256 of the
archived bytes. You can audit any event against the public archive.

## Register

Webhook subscriptions require an API key. One request registers the URL
and returns the signing secret. The secret is shown once and never again.

```bash
curl -X POST https://api.verzi.health/v1/subscriptions \
  -H "X-API-Key: $VERZI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "channel": "webhook",
    "webhook_url": "https://example.com/hooks/verzi",
    "codes": ["K0606", "A4239"]
  }'
```

Scope options:
- `codes`: HCPCS codes. You get changes to documents that govern these codes.
- `source_ids`: archive sources (see `/v1/archive/sources`).
- `change_types`, `payers`, `states`, `specialties`: category filters. Each
  is a list of values from the change tags a feed row carries
  (`tags.change_type`, `tags.payer`, `tags.state`, `tags.specialty`). A
  change must match every list that is set. Omit a list for no filter on
  that tag. `change_types` takes `edits`, `fees`, `coverage_policy` and
  `contractor_page`: the types that ride a notification. `specialties`
  takes `ed` and `dme`. `ed` does not match for a webhook yet.
- Neither: every public source.

URL rules: `https` only, no credentials in the URL, no private address.
The resolved address is checked again at delivery time.

## Delivery

One POST per run (daily, 14:00 UTC) when at least one change matches.
No matching changes, no request.

Headers:

| Header | Value |
|---|---|
| `Content-Type` | `application/json` |
| `X-Verzi-Event` | `changes.observed` |
| `X-Verzi-Subscription` | your subscription id |
| `X-Verzi-Signature` | `hex(hmac_sha256(secret, raw_body))` |

Body:

```json
{
  "event": "changes.observed",
  "sent_at": "2026-10-06T14:00:12+00:00",
  "subscription_id": 42,
  "count": 2,
  "changes": [
    {
      "doc_id": "lcd:33718",
      "source_id": "mcd_archive",
      "title": "Oxygen and Oxygen Equipment",
      "title_withheld": null,
      "publisher_version": "R15",
      "version_id": 8123,
      "publisher_effective_from": "2026-11-01",
      "publisher_effective_to": null,
      "observed_from": "2026-10-06T08:14:02+00:00",
      "origin": "live",
      "sha256": "9f2c...64 hex...",
      "affected_codes": ["E0601", "K0606"],
      "changed_codes": [],
      "revision_note": "Revision Effective Date: 11/01/2026 ...",
      "revision_note_withheld": null,
      "rev_number": "R49",
      "rev_date": "2026-11-01",
      "sections_changed": ["Coverage Indications, Limitations and/or Medical Necessity", "Revision History"],
      "sections_changed_vs": "48",
      "versions_url": "https://api.verzi.health/v1/archive/docs/lcd:33718/versions"
    }
  ]
}
```

Field notes:
- `sha256` is the hash of the archived original bytes. Verify it against
  `GET /v1/archive/files/{sha256}`.
- `publisher_effective_from` in the future means the version takes effect
  on that date. You hear about it when the archive observes it, not on
  the effective day.
- `origin` other than `live` (`backfill`, `wayback`) means the version is
  new to the archive, not newly published.
- **License holds are off until go-live** (Adrian's RECORD on #249
  7068559507; the switch is `entry_notes.ALERT_LICENSE_HOLDS_ON`). Until
  then `affected_codes` and `changed_codes` hold every code, CPT included,
  `title_withheld` and `revision_note_withheld` are always null, and no
  section heading is left out. At go-live the rules below return.
- `affected_codes` comes from Verzi's coverage mapping. It is empty when
  no mapping exists. At go-live it holds public HCPCS Level II codes only.
- `changed_codes` lists the codes whose row changed in this
  version of a row file (the NCCI MUE file, the DMEPOS fee schedule),
  against the version before it. It names the codes only, never a value.
  It is empty for any other document, and until a version is compared. A
  yearly fee update can change the row of most codes, so the list can be
  long. A subscription with `codes` matches on either list.
- `revision_note` is the publisher's own revision-history text for the
  version, verbatim, with `rev_number` and `rev_date` where the
  publisher stated them. All three are null when the renderer extracted
  no note. Verzi never writes or rewords this text. For a CMS transmittal
  (`source_id` = `cms_transmittals`) the field carries the cover page's
  SUBJECT line instead, verbatim; `rev_number` and `rev_date` stay null.
- `title_withheld` is `"licensed_text"` when the document title quotes
  licensed code text under the same rule as the note (a transmittal's title
  is its SUBJECT line). Then `title` is null. It is null otherwise.
- `revision_note_withheld` is `"licensed_text"` when the note quotes
  licensed code text CMS cites (such as the ADA's CDT descriptors), or
  comes from a document that does. Then `revision_note` is null. It is
  null otherwise.
- `note_sha256` is the sha256 of the archived file the quoted note comes
  from. It differs from `sha256` when CMS publishes the note at a separate
  endpoint (the Coverage API revision history). Null when there is no note.
- `sections_changed` names the publisher sections whose words differ
  from the prior version of the same document, by the publisher's own
  headings, in document order. `sections_changed_vs` is that prior
  version's number. An empty list means the two versions were compared
  and every section's words are identical. Both are null when the
  version was not compared: it is the document's first version, the
  version immediately before it was captured in a different source
  shape, or either version has a section that failed the fidelity gate.
  No diff text is ever authored; only the headings are named.

## Verify the signature

Compute the HMAC over the raw request body and compare in constant time.

```python
import hashlib, hmac

def verify(secret: str, raw_body: bytes, signature_header: str) -> bool:
    expected = hmac.new(secret.encode(), raw_body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, signature_header)
```

Reject the request when the signature does not match. The body is signed
exactly as sent; parse it only after the check passes.

## Retries and ordering

- A delivery succeeds on any 2xx within 10 seconds. Redirects are not
  followed and count as failure.
- On failure, the cursor does not advance. The next daily run redelivers
  the same changes plus any new ones. Delivery is at-least-once:
  **dedupe on `version_id`**.
- The redelivery window is capped at 14 days. After that, read the feed:
  `GET /v1/archive/changes`.

## Unregister

`POST /v1/subscriptions/unsubscribe?token=<unsubscribe_token>` with the
token from the registration response.
