# Conversation billing

`crm.billing` — `/v1/channels/:channelId/conversations`

What each conversation on a channel cost.

One route answers in two shapes. Without `stat` it pages through individual
charges; with `stat` it returns aggregates instead — which is why this SDK
splits it into two methods.

**`channelId` may be `'all'`** to read across every channel the caller owns.
That also changes what `summary` groups by: currency across all channels,
message category on a single one.

| Action | Route | Scope |
| --- | --- | --- |
| [`list`](#list) | `GET /v1/channels/:channelId/conversations` | `channels:read` |
| [`stats`](#stats) | `GET /v1/channels/:channelId/conversations?stat=…` | `channels:read` |

**Permissions** — `CRM.CHANNEL.USAGE.READ`, shared with the channel wallet.
Read-only: charges are written by the platform as conversations happen.


## The billing log object

```ts
interface BillingLog {
  id: string;
  chatId?: string;             // the conversation the charge belongs to
  identifier: string;          // the recipient, as the channel addresses them
  deduction: {
    billable: boolean;         // false for free-tier conversations — logged, not charged
    amount: number;
    currency: string;
  };
  meta?: Record<string, unknown>;  // recipient country, category, template, campaign, message id
  createdAt: string;
  updatedAt: string;
}
```

---

## list

```ts
crm.billing.list(channelId: string, query?): Promise<Paginated<BillingLog>>
```

`GET /v1/channels/:channelId/conversations` · `channels:read` · **200**

| Field | Type | Default | Notes |
| --- | --- | --- | --- |
| `page` | number | `1` | 1-based. |
| `rows` | number | `25` | 1–100. |
| `search` | string | — | 1–120 chars. Matches the recipient `identifier` and the `meta` fields: recipient, category, country and message id. |
| `range` | `{ start, end }` | — | Unix seconds. `0` on either end means **no bound**. Serialised to JSON by the SDK. |
| `template` | string[] | — | Quick filter: templates the conversation was billed against. |
| `country` | string[] | — | Quick filter: recipient country codes. |
| `campaign` | string[] | — | Quick filter: campaigns that produced the conversation. |

The three array filters are sent as JSON; the server also accepts a
comma-separated string if you build the query yourself.

```ts
const charges = await crm.billing.list('all', {
  range: { start: 1767225600, end: 1769817600 },
  country: ['IN', 'US'],
  rows: 100,
});
```

---

## stats

```ts
crm.billing.stats(channelId: string, stat?: BillingStat, query?): Promise<BillingStats>
```

`GET /v1/channels/:channelId/conversations?stat=…` · `channels:read` · **200**

The same filters, aggregated instead of paged. `stat` defaults to `'summary'`.

```ts
interface BillingStats {
  data: { group: string; totalRecords: number; totalDeductionAmount: number }[];
  stat: 'summary' | 'day_wise';
}
```

`group` is what the totals are cut by:

| `stat` | `channelId` | `group` is |
| --- | --- | --- |
| `summary` | `'all'` | the currency |
| `summary` | one channel | the message category |
| `day_wise` | either | the calendar date |

**Pagination does not apply** — every group is returned, and `page`/`rows` are
ignored.

```ts
const monthly = await crm.billing.stats('all', 'summary', {
  range: { start: 1767225600, end: 1769817600 },
});

for (const row of monthly.data) {
  console.log(row.group, row.totalDeductionAmount, `(${row.totalRecords} conversations)`);
}

// Per day, one channel
const daily = await crm.billing.stats(channelId, 'day_wise', { range: { start: 0, end: 0 } });
```
