# Inbox — referrals (click-to-WhatsApp)

Where a lead came from. When a contact opens a conversation by tapping a
click-to-WhatsApp ad, the provider attaches the ad's referral payload to that
first inbound message, and it is stored as a row of its own — one per contact per
channel.

`inbox.referrals(...)` · see [connection.md](https://github.com/udghatan-dev/crm-sdk/blob/main/packages/sdk-js/docs/inbox/connection.md) first

| Action | Event | Scope |
|---|---|---|
| `referrals` | `ctwa.list` | `contacts:read` |

## The shape

```ts
interface CtwaReferral {
  id: string;
  contact: string;
  channel: string | null;
  /** The provider's referral object, carried through untouched. */
  payload: JsonObject;
  createdAt: string;
  updatedAt: string;
}
```

`payload` is whatever the provider sent, unmodified. For WhatsApp that typically
carries `source_id` (the ad id), `source_type`, `source_url`, `headline`, `body`,
`media_type` and a thumbnail or video url. It is **not** normalised: read it
defensively, and treat a missing key as a referral that did not include it.

`payload.source_id` is the one key the platform itself relies on — it is what the
`REFERRAL_ID` filter field matches against.

## referrals

```ts
const referrals = await inbox.referrals(chatId);

for (const { payload } of referrals) {
  console.log(payload.headline, payload.source_url);
}
```

`ctwa.list` · scope `contacts:read`

| Field | Type | Required | Default | Notes |
|---|---|---|---|---|
| `id` | `string` | yes | — | Contact whose referrals to read. |
| `limit` | `number` | no | `50` | Max 100. |

Newest first. An empty array means the contact did not arrive through an ad, which
is the normal case — not an error.

## Filtering chats by ad

`REFERRAL_ID` is a filter field available on the inbox chat list and **not** on
the REST contact list: it resolves through this collection, which only the inbox
reads.

```ts
const { contacts } = await inbox.chats.list({
  conditions: [
    { field_id: 'REFERRAL_ID', field_type: 'string', condition: 'equal to', value: '120210000000', operator: null },
  ],
});
```

The condition is membership, whatever comparison you name: a row holds exactly
one `source_id`, so there is nothing to compare but whether a contact came from
that ad.
