# Segments

`crm.segments` — `/v1/audience/segments`

A saved [contact filter](../contact-filter.md) under a name.

Nothing is materialised: a segment holds no contact ids. It covers whatever its
conditions match at the moment it is next run — by
`crm.contacts.list({ segment })`, by an export's `config.segment`, or by a
campaign. Two runs a day apart can return different people, by design.

| Action | Route | Scope |
| --- | --- | --- |
| [`list`](#list) | `GET /v1/audience/segments` | `segments:read` |
| [`get`](#get) | `GET /v1/audience/segments/:id` | `segments:read` |
| [`create`](#create) | `POST /v1/audience/segments` | `segments:write` |
| [`update`](#update) | `PATCH /v1/audience/segments/:id` | `segments:write` |
| [`delete`](#delete) | `DELETE /v1/audience/segments/:id` | `segments:write` |

**Permissions** — `CRM.CONTACT.SEGMENT`: `LIST`, `READ`, `CREATE`, `UPDATE`,
`DELETE`. Reading a segment reveals its filter, not the contacts behind it;
those still pass `CRM.CONTACT.LIST`.

## The segment object

```ts
interface Segment {
  id: string;
  name: string;
  description?: string;
  filter: ContactFilter;
  createdAt: string;
  updatedAt: string;
}
```

---

## list

```ts
crm.segments.list(query?: Partial<ListSegmentsQuery>): Promise<Paginated<Segment>>
```

`GET /v1/audience/segments` · `segments:read` · **200**

| Field | Type | Default | Notes |
| --- | --- | --- | --- |
| `page` | number | `1` | 1-based. |
| `rows` | number | `25` | 1–100. |
| `search` | string | — | 1–120 chars. Matches the **name and description**. |

---

## get

```ts
crm.segments.get(id: string): Promise<Segment>
```

`GET /v1/audience/segments/:id` · `segments:read` · **200**

---

## create

```ts
crm.segments.create(input: CreateSegmentInput): Promise<Segment>
```

`POST /v1/audience/segments` · `segments:write` · **201**

| Field | Type | Required | Notes |
| --- | --- | --- | --- |
| `name` | string | yes | Trimmed, 1–120 chars. Addressable by name as well as by id wherever a segment is referenced. |
| `description` | string | no | Trimmed, up to 500 chars. |
| `filter` | `ContactFilter` | yes | Validated on write, not when first run — a segment that stores cannot fail later for a condition that never parsed. |

```ts
const segment = await crm.segments.create({
  name: 'High budget, Pune',
  description: 'Lead score over 70 in Pune',
  filter: {
    conditions: [
      { operator: null, field_id: 'city', field_type: 'text', condition: 'equal to', value: 'Pune' },
      { operator: 'and', field_id: 'lead_score', field_type: 'number', condition: 'greater than', value: 70 },
    ],
  },
});

// Replay it
const contacts = await crm.contacts.list({ segment: segment.id });
// …or by name
const same = await crm.contacts.list({ segment: 'High budget, Pune' });
```

---

## update

```ts
crm.segments.update(id: string, input: UpdateSegmentInput): Promise<Segment>
```

`PATCH /v1/audience/segments/:id` · `segments:write` · **200**

Same fields as `create`, all optional. Sending `filter` **replaces** the whole
condition list — there is no per-condition edit. Read the segment, change the
array, send it back.

---

## delete

```ts
crm.segments.delete(id: string): Promise<void>
```

`DELETE /v1/audience/segments/:id` · `segments:write` · **204**, no body.

Contacts are untouched: a segment never owned them. Anything still referencing
the segment by id or name — a scheduled export, a campaign — will start failing
with `404`, since listing everyone because the segment went missing would be the
wrong answer.
