# The change feed and webhooks

> Keep another system in step with your CRM's records, by reading every change in order from the change feed or having each one sent to a webhook.

Every change to a client, a person, a deal (its lines and invoices too) or an agreement is an event: added, changed, archived, brought back, merged or removed. It does not matter who made it or where: a page of the CRM, Sam, a connected assistant, this API, a workflow, an email read from somebody's Gmail, an import or a stream. The event is the record's own history, written in the same step as the change, so none is ever lost.

There are two ways to hear about them:

- **The change feed** (`GET /events`): your system asks for what changed since it last asked. Nothing to set up, and it works behind a firewall.
- **Webhooks**: the CRM sends each event to an https address of yours, signed so you can tell it is real.

Many integrations use both: a webhook to hear soon, and the feed to catch up after any time the receiver was down.

## Events


| Event | Sent when |
| --- | --- |
| `company.created` | A company was added. |
| `company.updated` | A company changed: the event lists each field, before and after. |
| `company.archived` | A company was archived. |
| `company.restored` | A company was brought back from the archive. |
| `company.merged` | A company was merged into another, which the event names. |
| `company.deleted` | A company was removed. |
| `person.created` | A person was added. |
| `person.updated` | A person changed: the event lists each field, before and after. |
| `person.archived` | A person was archived. |
| `person.restored` | A person was brought back from the archive. |
| `person.merged` | A person was merged into another, which the event names. |
| `person.deleted` | A person was removed. |
| `deal.created` | A deal was added. |
| `deal.updated` | A deal changed: the event lists each field, before and after (a move to another stage, a line or an invoice too). |
| `deal.archived` | A deal was archived. |
| `deal.restored` | A deal was brought back from the archive. |
| `deal.merged` | A deal was merged into another, which the event names. |
| `deal.deleted` | A deal was removed. |
| `agreement.created` | An agreement was added. |
| `agreement.updated` | An agreement changed: the event lists each field, before and after. |
| `agreement.archived` | An agreement was archived. |
| `agreement.restored` | An agreement was brought back from the archive. |
| `agreement.merged` | An agreement was merged into another, which the event names. |
| `agreement.deleted` | An agreement was removed. |


Each event looks the same on the feed and in a webhook:

```json
{
  "id": "48213",
  "event": "deal.updated",
  "occurred_at": "2026-12-03T04:15:00.000Z",
  "record": {
    "object": "deal",
    "id": "9b2e...",
    "url": "https://crm.sanda-os.com.au/crm/deals/9b2e..."
  },
  "by": { "id": "7a90...", "name": "Priya Shah" },
  "via": "api",
  "through": "API key “Xero sync”",
  "changes": [
    { "field": "stage", "action": "changed", "from": "c4d1...", "to": "e2a8..." },
    { "field": "custom_fields.po_number", "action": "changed", "from": null, "to": "PO-7" }
  ]
}
```

- `id` is the event's own, a string of digits. Two deliveries of the same event have the same `id`, so skip one you have handled.
- `record` is what changed. Read it with `GET /deals/{id}` for how it stands now.
- `by` is the person in the CRM who made the change, or `null` when nobody did, such as a [stream](https://docs.sanda-os.com.au/crm/streams) or a workflow running on its own.
- `via` says how, as a record's [history](https://docs.sanda-os.com.au/crm-api/records#history) does, and `through` names the key, the assistant or the workflow.
- `changes` lists each field the change touched, as the record's history has it: named as the record writes it (`stage`, `owner`, `custom_fields.po_number`), with what it was and what it is now. A link's `from` and `to` are ids. On `created`, `archived` and `restored`, `changes` is usually empty. On a deal, a line or an invoice is a change too: see [lines and invoices on the change feed](https://docs.sanda-os.com.au/crm-api/deals#lines-and-invoices-on-the-change-feed).
- `merged_into` is on a `merged` event: the record it was merged into, which carries on, with what it took from this one as its own `updated` event.

One change to a record is one event, however many fields it changed. A change that changes nothing is none.

### Do not echo your own changes

A sync that writes to the CRM hears its own changes back. Give the sync a key of its own and skip events whose `via` is `api` and whose `through` names that key, such as `API key “Xero sync”`, or compare the record with what you last wrote.

## The change feed

`GET /events` lists events in the order their changes were made, 100 at a time unless `limit` says otherwise (up to 500).

```bash
curl "https://crm.sanda-os.com.au/api/crm/v1/events?cursor=eyJmIjoiMTIz..." -H "Authorization: Bearer bck_..."
```

```json
{ "events": [ ... ], "next_cursor": "eyJmIjoiMTI5...", "more": true }
```

- **Keep `next_cursor`** and send it as `cursor` next time. It is there even when `events` is empty: it says the feed has been read up to now.
- **`more`** says there is already more to read. Ask again at once while it is `true`, and when it is `false`, wait a while (a minute, say) before asking again.
- **No cursor** starts from the first change the feed holds. `start=now` gives a cursor for this moment and no events: start a new sync there, after reading what you need with the lists.
- **`types`** narrows the feed to some records, with commas: `types=deal,agreement`.

A change is on the feed once it is saved, in the order changes were saved, and never twice: a change saved while you read, even one begun before what you last read, is there on your next read. A cursor is your place in the feed, so keep the last one you were given for as long as you need, and nothing between it and now is missed.

The feed holds every change the CRM's history holds. To start a sync, take a cursor with `start=now`, read the records you need with the lists, then keep up with the feed from that cursor.

The [timeline](https://docs.sanda-os.com.au/crm-api/timeline) is not on the feed: it is what happened, not a change to a record. Read new entries with `GET /activities?order=updated`.

## Webhooks

A webhook is an https address the CRM sends each event to. An owner or admin adds them under **Settings · API keys · Webhooks**, or with `POST /webhooks` and a key with `crm:manage`. A CRM keeps up to 10.

:::steps
1. **Press Add a webhook.** Under **Webhooks** on **Settings · API keys**.
2. **Name it and give its address.** Under **Name**, say where it goes, such as "Xero". Under **Address**, give the https address the other system gave you. It must be https, on the standard port, at a public address.
3. **Choose its events.** Under **Events**, tick the records it wants, or none for every change to every record.
4. **Press Add the webhook, and copy its signing secret.** The secret begins `whsec_` and is shown once.
5. **Press Send a test.** The CRM sends it a `webhook.test` event and says how it answered.
:::

Through the API, `events` takes single events too:

```bash
curl https://crm.sanda-os.com.au/api/crm/v1/webhooks \
  -H "Authorization: Bearer bck_..." \
  -H "Content-Type: application/json" \
  -d '{"name": "Xero", "url": "https://hooks.example.com/sanda", "events": ["company.*", "deal.updated"]}'
# → {"webhook": {...}, "secret": "whsec_..."}
```

`company.*` is every event of a company. A new webhook is sent what happens from then on, never the CRM's history: read that from the change feed.

### What is sent

Each event is its own `POST`, with the event as JSON and the workspace it is from:

```http
POST /sanda HTTP/1.1
Host: hooks.example.com
Content-Type: application/json; charset=utf-8
User-Agent: sanda-crm-webhooks/1
Sanda-Event: deal.updated
Sanda-Delivery: 48213
Sanda-Webhook: 3e9a...
Sanda-Signature: t=1764735300,v1=5d1c0f...

{"id":"48213","event":"deal.updated","occurred_at":"2026-12-03T04:15:00.000Z","record":{...},"by":{...},"via":"api","through":"API key “Xero sync”","changes":[...],"workspace":{"slug":"acme-services"}}
```

`Sanda-Delivery` is the event's `id`.

### Answer quickly

Answer with any 2xx within 10 seconds, then do the work. Anything else is a failed delivery: another status, no answer in time, or a redirect, which is never followed.

### Order, retries and switching off

- **In order, at least once.** Each webhook is sent its events in the order the changes were made, one at a time, up to 50 at a go. When one fails, nothing after it is sent until it is delivered, so you never see an event before the one ahead of it. An event can arrive twice (your answer was lost on the way back, say), so skip one whose `id` you have handled.
- **When.** A change made on a page of the CRM or through this API is sent within moments. Every webhook is also brought up to date every 15 minutes, which is when changes from elsewhere (a workflow, an email, an import, a stream) go out, and a failed event is tried again.
- **Switched off after 50 failures in a row.** **Settings · API keys** then says why. Fix the receiver and press **Switch on**, or send `PATCH /webhooks/{id}` with `"active": true`, and it carries on from where it stopped. Add `"skip_missed": true` to start from now instead.

**Settings · API keys** shows each webhook's last delivery and, while one is failing, what the receiver answered. `GET /webhooks/{id}` gives the same.

### Check the signature

Every request carries `Sanda-Signature: t=<unix seconds>,v1=<hex>`. `v1` is the HMAC-SHA256, under the webhook's whole signing secret (`whsec_` and all), of `t`, a full stop, and the body exactly as it arrived. Check it before you trust the body, and refuse a `t` more than a few minutes old so a recorded request cannot be replayed.

::::tabs
:::tab JavaScript
```javascript
import crypto from 'node:crypto';

// raw is the body exactly as it arrived, as a string. Parse it only after this.
export function fromSanda(raw, header, secret, toleranceSeconds = 300) {
  const parts = Object.fromEntries(String(header).split(',').map((p) => p.trim().split('=')));
  const t = Number(parts.t);
  if (!t || Math.abs(Date.now() / 1000 - t) > toleranceSeconds) return false;
  const want = crypto.createHmac('sha256', secret).update(`${t}.${raw}`).digest();
  const got = Buffer.from(parts.v1 || '', 'hex');
  return got.length === want.length && crypto.timingSafeEqual(got, want);
}
```
:::
:::tab Python
```python
import hashlib
import hmac
import time

# raw is the body exactly as it arrived, as bytes. Parse it only after this.
def from_sanda(raw: bytes, header: str, secret: str, tolerance_seconds: int = 300) -> bool:
    parts = dict(p.strip().split("=", 1) for p in header.split(",") if "=" in p)
    t = int(parts.get("t", "0") or 0)
    if not t or abs(time.time() - t) > tolerance_seconds:
        return False
    want = hmac.new(secret.encode(), f"{t}.".encode() + raw, hashlib.sha256).hexdigest()
    return hmac.compare_digest(want, parts.get("v1", ""))
```
:::
::::

A body re-serialised by a framework before you check it will not match: read it raw.

:::links
- [Records](https://docs.sanda-os.com.au/crm-api/records): Read a record an event names.
- [Reference](https://docs.sanda-os.com.au/crm-api/reference): Every endpoint, event and error.
:::
