
# Events (webhooks)

Horizon can call your system when a record changes. You give an address, pick
the events, and Horizon sends a signed message to it each time.

An event says **what happened, never the values**. You fetch the record
yourself, with your own API key, and see only what that key may see.

## 1. Set it up

You need the right to configure Horizon.

1. Open **Configuration → Access → Outbound events** and choose **Add subscription**.
2. Enter a name and an address. The address must start with `https://` and be
   reachable from the internet.
3. Pick the events. Object types are optional; without any, you get all.
4. With exactly one object type you can add a condition under **Only if**, for
   example `priority > 2`. See the
   [HSL Expression Reference](hsl-expressions.html).
5. **Send test call** checks the address before you save.
6. After saving, copy the **signing key**. It is shown only once.

## 2. The events

| Event | When |
|---|---|
| `record.created` | a record was created |
| `record.changed` | fields were changed |
| `record.stateChanged` | the record moved to another state |
| `record.commented` | someone commented |
| `record.deactivated` | the record was archived |
| `record.approvalDecided` | an approval was granted or rejected |

Horizon sends a `POST` with this body:

```json
{
  "id": "evt-1",
  "type": "record.stateChanged",
  "occurredAt": "2026-11-01T08:59:00Z",
  "objectType": "ticket",
  "record": { "id": 48213, "number": "T-2026-00042" },
  "state": { "from": "new", "to": "assigned" },
  "changedFields": ["status", "assignee"],
  "href": "https://yourcompany.example/api/record?type=ticket&id=48213"
}
```

`number`, `state` and `changedFields` are only there when they apply. `state`
holds codes, not labels. To get the record, call `href` with your API key; the
[REST API Guide](guide.html) explains how.

The machine-readable contract is [asyncapi.json](/asyncapi.json).

## 3. Check the signature

Anyone can call your address. The signature proves that a message comes from
Horizon. It is in the header `Horizon-Signature`:

```
t=<seconds>,v1=<HMAC-SHA256 of "<t>.<body>">
```

Check it before you use the message, on the body **exactly as received**, and
refuse messages older than five minutes. In Node.js:

```js
import { createHmac, timingSafeEqual } from 'node:crypto'

export function isFromHorizon(header, body, signingKey, now = Date.now() / 1000) {
  const match = /^t=(\d+),v1=([0-9a-f]{64})$/.exec(header ?? '')
  if (!match || Math.abs(now - Number(match[1])) > 300) return false
  const expected = createHmac('sha256', signingKey).update(`${match[1]}.${body}`).digest()
  return timingSafeEqual(expected, Buffer.from(match[2], 'hex'))
}
```

To test your own code: with the key `example-signing-key` and this body

```text
{"id":"evt-1","type":"record.stateChanged","occurredAt":"2026-11-01T08:59:00Z","objectType":"ticket","record":{"id":48213,"number":"T-2026-00042"},"state":{"from":"new","to":"assigned"},"changedFields":["status","assignee"],"href":"https://yourcompany.example/api/record?type=ticket&id=48213"}
```

the header is

```text
t=1793523540,v1=730d81efcb766eb367d93755c79071a971558c7c19baa14e902d56d0281629dc
```

## 4. Answer and retries

- Answer with any `2xx` status within ten seconds. Do the work afterwards.
- Redirects are not followed.
- **The same event can arrive twice.** Remember its `id` and ignore repeats.
- Events about the same record arrive in order.
- If you do not answer, Horizon tries again after 1, 5, 15 and 60 minutes, then
  every hour. After three days the event is given up.
- Answer `410 Gone` if you do not want this event. Horizon does not try again.
- If nothing reaches you for three days, Horizon pauses the subscription and
  tells your administrators.

Under **Deliveries** at the subscription you see each call and its answer for
30 days. There you can resend an event that was given up and renew the signing
key. The old key stops working at once.
