# Contact filter

One condition language, five callers:

| Where | Shape |
| --- | --- |
| `crm.contacts.list({ filter })` | `ContactFilter` — sent as JSON on the query string |
| `crm.segments.create({ filter })` | `ContactFilter` — stored under a name |
| `crm.crmTriggers.create({ filters })` | `ContactFilterEntry[]` — flat, not wrapped |
| `crm.exports.create({ config: { filter } })` | `ContactFilter` |
| `crm.scheduledMessages.create({ conditions })` | `ContactFilter` — re-checked just before each send |

A filter that works in one of these works in all of them, and means the same
thing: a trigger's `filters` are checked by running the contact list query
pinned to that one contact.

## Shape

```ts
interface ContactFilter {
  type?: string;                    // free-form tag for provenance, default 'sheet'
  conditions?: ContactFilterEntry[]; // at most 50, default [] = match everyone
}

interface ContactFilterEntry {
  id?: number;                      // your own row id, echoed back
  operator?: 'and' | 'or' | null;   // null on the first condition
  field_id: string;                 // a custom field slug, `tags`, or a SYSTEM_* field
  field_type: ContactFilterFieldType;
  condition: ContactFilterCondition;
  value?: unknown;                  // omitted for conditions that take none
  direction?: 'LAST' | 'TODAY' | 'NEXT' | '';
  timeUnit?: 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'years' | '';
  offset?: number;                  // caller's midnight, as a ms shift from UTC midnight
  props?: { period?: 'daily' | 'weekly' | 'monthly' | 'annually' };
  requireInput?: boolean;           // UI hint, ignored by the server
}
```

## How conditions combine

`and` binds tighter than `or`. The list is split on every `or`, each run is
`and`ed, and the runs are `or`ed together — so this:

```
A  and B  or C  and D
```

means `(A and B) or (C and D)`. There is no nesting and no parentheses; when you
need them, save the inner group as a segment and narrow it with `filter`.

The first condition's `operator` is `null`. An empty `conditions` array matches
every contact.

## Fields

| `field_id` | What it addresses |
| --- | --- |
| a custom field slug | `contact.custom_field_hash.<slug>` |
| `tags` | The contact's tags. `value` is one tag id or an array of them; `equal to` is "has any of these", `not equal to` is "has none of them". |
| `SYSTEM_LAST_SEEN` | `contact.last_seen`, stored as unix **seconds**. |
| `SYSTEM_CREATED_ON` | `contact.createdAt`, a date. |
| `SYSTEM_IDENTIFIER` | The identifier of any channel identity — a phone number, handle or address. |
| `SYSTEM_PROFILE_NAME` | The name on any channel identity. |

`SYSTEM_IDENTIFIER` and `SYSTEM_PROFILE_NAME` live in a separate collection, so
they resolve through a lookup. Negated conditions on them read the way you would
say them out loud: `SYSTEM_IDENTIFIER not equal to X` means *no* identity holds
X — not *some* identity holds something else.

## Conditions by field type

Four conditions apply to every type, whatever its shape:

`is null` · `is not null` · `exist` · `does not exist`

Beyond those:

| `field_type` | Conditions |
| --- | --- |
| `string` `text` `url` `email` `phone` `country` `language` `timezone` `dial_code` `dropdown` | `equal to` `not equal to` `start with` `end with` `contains` `does not contains` `is empty` `is not empty` |
| `number` | `equal to` `not equal to` `greater than` `greater than or equal to` `less than` `less than or equal to` |
| `date` `timestamp` | `is` `after` `before` `between` `within` |
| `periodic_time` | `is` `after` `before` `between` `within` — needs `props.period` |

Anything else is rejected with `422`, naming the field and the condition that
had no meaning for it.

## Values

- **Text and number conditions** take a scalar in `value`.
- **`is` / `after` / `before` / `between`** on a date take a **JSON array
  string**: `between` reads two entries, the rest read the first.
  `value: '["2026-03-01","2026-03-31"]'`.
- **`within`** reads `direction`, `timeUnit` and a number in `value`: `LAST`
  counts that many units backwards from the caller's midnight, `NEXT` forwards.
  `direction: 'TODAY'` is the exception — it means the caller's current day and
  needs neither `timeUnit` nor `value`. A missing `timeUnit` or an unknown
  `direction` is a `422`.
- **`is empty` / `is not empty` / `is null` / `exist` / …** take no `value`.

### `offset`

Relative windows are anchored to the caller's midnight, not UTC's. `offset` is
the shift in milliseconds from UTC midnight to yours: IST (+5:30) sends
`-19800000`. Leave it at `0` to work in UTC.

Months and years shift calendar-aware; hours, days and weeks are fixed-width.

## Examples

Everyone in Pune, tagged VIP:

```ts
await crm.contacts.list({
  filter: {
    conditions: [
      { operator: null, field_id: 'city', field_type: 'text', condition: 'equal to', value: 'Pune' },
      { operator: 'and', field_id: 'tags', field_type: 'string', condition: 'equal to', value: [vipTagId] },
    ],
  },
});
```

Seen in the last 7 days, or created today:

```ts
await crm.contacts.list({
  filter: {
    conditions: [
      {
        operator: null,
        field_id: 'SYSTEM_LAST_SEEN',
        field_type: 'timestamp',
        condition: 'within',
        direction: 'LAST',
        timeUnit: 'days',
        value: 7,
        offset: -19800000,
      },
      {
        operator: 'or',
        field_id: 'SYSTEM_CREATED_ON',
        field_type: 'date',
        condition: 'within',
        direction: 'TODAY',
        offset: -19800000,
      },
    ],
  },
});
```

No email on file:

```ts
{ operator: null, field_id: 'email', field_type: 'email', condition: 'is empty' }
```
