# CRM inbound webhook

With the CRM inbound webhook your CRM, or the automation tool in front of it, tells HeyMySocial when a contact, a lead, an opportunity or a sale changes. The records are the same ones the [batch operation of the REST API](https://heymysocial.com/docs/api/operations/send-crm-records) takes; the webhook is the push version of it, signed instead of opened with an API key.

Today this webhook, the REST API and a [file](https://heymysocial.com/docs/files/crm) are the ways a CRM is kept in step. A direct connector to a CRM product is built but switched off.

## Address and secret

Add a CRM connection of the webhook kind under **Settings**, **Connections**. Its page then shows, under **Receive from the CRM**:

```text
POST https://heymysocial.com/crm/{connectionKey}
```

- `{connectionKey}` is 40 lower-case letters and digits. It only finds the connection and is not a secret. The whole address stands under **Send webhooks to**.
- The **signing secret** starts with `hmscrm_`. It is shown once when the connection is created, and again each time you press **Rotate the secret**. The old one stops at once.

A connection that was disconnected answers `404`.

## The request

Send `Content-Type: application/json` and `X-HMS-Signature: t=<unix seconds>,v1=<hex>`: the HMAC-SHA256 of `<t>.<raw body>` with the connection's secret, as lower-case hex. See [the signature](https://heymysocial.com/docs/webhooks#the-signature).

- The timestamp may be at most 5 minutes from the server's clock.
- The header may carry several `v1` values, separated by commas. The request is accepted when one of them matches.
- The body may be at most 512 KB.

The body is one record with an `event_id`:

```json
{
  "event_id": "crm-evt-20261125-0001",
  "type": "lead.updated",
  "external_id": "L-1042",
  "updated_at": "2026-11-25T08:30:00Z",
  "data": { "name": "Mette Jensen", "email": "mette@example.com", "status": "Qualified" }
}
```

or several records under `events`, applied in order:

```json
{
  "event_id": "crm-evt-20261125-0002",
  "events": [
    { "type": "opportunity.created", "external_id": "O-311", "updated_at": "2026-11-25T09:00:00Z", "data": { "lead_external_id": "L-1042", "name": "Roof installation", "value": 84000, "currency": "DKK" } },
    { "type": "sale.created", "external_id": "S-88", "updated_at": "2026-11-25T09:30:00Z", "data": { "lead_external_id": "L-1042", "amount": 84000, "currency": "DKK", "revenue_basis": "invoiced" } }
  ]
}
```

One request holds at most 500 records, the same as the batch operation.

## The record

A record is the `CrmRecord` of the REST API. The [batch operation](https://heymysocial.com/docs/api/operations/send-crm-records) lists every field; in short:

| Field | What it is |
|---|---|
| `type` | `contact`, `lead`, `opportunity` or `sale`, a full stop, then `created`, `updated`, `deleted` or `merged`. Or give `object` and `action` (`upsert`, `delete`, `merge`) instead. |
| `external_id` | The CRM's own id of the record. Needed. |
| `updated_at` | When it changed in the CRM. For lead status the newest change wins. |
| `merged_into` | For a merge: the id of the record that stays. |
| `data` | The fields: `status`, `name`, `email`, `phone` and `company` for a lead; `lead_external_id`, `value` and `currency` for an opportunity; `lead_external_id`, `amount` and `currency` for a sale. |

Fields in `data` pass through the connection's field mapping first, so a CRM that calls the status `Lead Status` can keep doing so. Anything that is not a known field is ignored.

## Duplicates

`event_id` is what makes a delivery safe to repeat. It is taken once for each connection: the first request with it is processed, and every later one is answered `200` with the status `duplicate` and changes nothing.

```json
{ "status": "duplicate", "event_id": "crm-evt-20261125-0001", "request_id": "01M88R37HG2A5Q6629YMT5Q7YA" }
```

- Use the id your CRM gives the change, or build one that is the same on every retry of the same change. Up to 150 characters are used.
- Without an `event_id`, a fingerprint of the raw body takes its place, so only a byte-identical body counts as the same delivery.
- If a request was answered `500`, its `event_id` is free again: send it once more. Records in it that were applied before the failure change nothing the second time.

Behind that, each record is idempotent on its own. A record that says what is already stored is answered `unchanged`, and one older than what is stored is answered `stale`, or `superseded` the first time a lead status arrives that is older than the one set here.

## Status names

A lead's `status` arrives in the CRM's own words and is translated in two steps: first by the mapping you set on the connection's page under **Mapping**, then by a built-in list. Before it is looked up, the name is put in lower case and its spaces and hyphens become underscores, so `Closed Won`, `closed-won` and `closed_won` are the same.

| Status here | Names understood without a mapping |
|---|---|
| `new` | `new`, `open`, `lead` |
| `contacted` | `contacted`, `attempted_to_contact`, `connected`, `in_progress` |
| `qualified` | `qualified`, `sales_qualified` |
| `disqualified` | `disqualified`, `unqualified`, `bad_fit` |
| `opportunity` | `opportunity`, `quote_sent`, `negotiation` |
| `won` | `won`, `closed_won`, `customer` |
| `lost` | `lost`, `closed_lost` |

A status found in neither place is not guessed. The rest of the record is applied, the lead keeps the status it has, the result carries `detail: "unmapped_status"`, and the name is listed under **Reconciliation errors** until you map it. [Lead statuses](https://heymysocial.com/docs/reference/lead-statuses) describes the seven statuses.

## Answers

| Status | Code or status | When |
|---|---|---|
| `200` | `processed` | The records were looked at. `results` has one entry for each, in order. |
| `200` | `duplicate` | This `event_id` was received before. |
| `401` | `signature_invalid` | The header is missing or malformed, or no `v1` matches. |
| `401` | `timestamp_stale` | The signature matches, but the timestamp is more than 5 minutes off. |
| `404` | `not_found` | No CRM connection has this key. |
| `413` | `validation_failed` | The body is larger than 512 KB. |
| `422` | `validation_failed` | The body is not a JSON object or list, or holds more than 500 records. |
| `429` | none | More than 600 requests in a minute for this connection from one sender address. |
| `500` | `server_error` | Something failed while the records were applied. `retryable` is `true`: send it again. |

Every answer except `429` carries `request_id`. A processed request looks like this:

```json
{
  "status": "processed",
  "event_id": "crm-evt-20261125-0001",
  "results": [
    { "object": "lead", "external_id": "L-1042", "result": "created", "id": "01m88r37h3gbbawm1q0f4h8csh" }
  ],
  "request_id": "01M88R37GZE6Q87JNFE2Z5WKF5"
}
```

A record that cannot be applied does not fail the request. It gets `result: "error"` with a `detail` such as `unknown_lead` or `invalid_record`, and waits under **Reconciliation errors** for a person to resolve or ignore it. The same list is available over the API: [List CRM reconciliation errors](https://heymysocial.com/docs/api/operations/list-crm-reconciliation-errors).

## A signed request from the command line

Set `HMS_CRM_WEBHOOK_URL` to the address and `HMS_CRM_SECRET` to the secret, then:

```bash
BODY='{"event_id":"crm-evt-20261125-0001","type":"lead.updated","external_id":"L-1042","updated_at":"2026-11-25T08:30:00Z","data":{"name":"Mette Jensen","email":"mette@example.com","status":"Qualified"}}'
T=$(date +%s)
SIG=$(printf '%s.%s' "$T" "$BODY" | openssl dgst -sha256 -hmac "$HMS_CRM_SECRET" | sed 's/^.* //')

curl -X POST "$HMS_CRM_WEBHOOK_URL" \
  -H "Content-Type: application/json" \
  -H "X-HMS-Signature: t=$T,v1=$SIG" \
  --data-binary "$BODY"
```

## What the CRM decides and what it does not

- **Sales and the value of an opportunity** are the CRM's. Its numbers replace the ones here, and a sale deleted in the CRM is removed here.
- **Lead status** goes to the newest change. When the CRM sends a status older than the one set here, the status here stays, the CRM's value is kept in the lead's timeline, and the status here is sent back if the [outbound webhook](https://heymysocial.com/docs/webhooks/crm-outbound) is on and no delivery of that status was made already.
- **Where a lead came from** is never written from a CRM: capture time, visit and campaign stay as they were recorded here. A first-contact time from the CRM only fills an empty one.
- **A lead deleted in the CRM is kept here.** It is no longer synced, and the deletion is listed for a person to look at.
