> ## Documentation Index
> Fetch the complete documentation index at: https://spotzee.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Build a segment via the API

> Define a dynamic membership rule programmatically so segments stay in sync with code-driven attributes.

Most segments are best authored in the Spotzee UI. Operators iterate quickly, preview membership, and version their work. Use the API when the rules themselves are derived from your code or config: a generated cohort per pricing plan, a per-region segment driven by a feature-flag rollout, or a backfill that needs to recreate dozens of segments at once.

> [!NOTE]
>
> Today the API exposes segments as **dynamic lists**. There is no separate `/segments` endpoint. A segment is a list with `type: "dynamic"` and a rule tree. Static lists (explicit member add/remove) use the same endpoints with `type: "static"`.

## What you'll build

A script that creates a dynamic list with a rule tree, then verifies membership in the Spotzee app.

## Prerequisites

You'll need a `sk_` project key with write access. See [Authentication](https://spotzee.com/docs/guides/authentication.md).

## Walkthrough

**1. Decide what membership means**

A segment is a boolean rule that yields true or false for any contact. Common shapes:

* **Attribute filter**: `data.plan = "professional"`
* **Behavioural filter**: contacts who triggered `order_placed` in the last 30 days
* **Combined**: `plan = "professional"` AND triggered `order_placed` in the last 30 days

Sketch the rule as plain language first; the JSON shape follows from there.

**2. Build the rule tree**

Spotzee rules are a tree of nodes. The root is a `wrapper` node that combines its children with `and` / `or`; leaves are typed comparisons against a contact field (`group: "user"`) or event payload (`group: "event"`).

Every node carries:

| Field | Notes |
| - | - |
| `uuid` | Stable client-supplied UUID, preserved across edits. Generate one per node. |
| `type` | `wrapper` for groups; `string` / `number` / `boolean` / `date` / `array` for leaves. |
| `group` | `user` (compare against the contact) or `event` (compare against an event). Use `parent` to inherit. |
| `path` | Dotted path on the contact or event payload (e.g. `data.plan`, `name`). Empty on wrappers. |
| `operator` | `=`, `!=`, `<`, `<=`, `>`, `>=`, `and`, `or`, etc. |
| `value` | Leaf comparison value. Omitted on wrappers. |
| `frequency` | Optional event-frequency window: `{ period, operator, count }`. |
| `children` | Array of child nodes (present on wrappers). |

Example: contacts on the Professional plan who triggered `order_placed` at least once in the last 30 days.

```json theme={null}
{
  "uuid": "11111111-1111-1111-1111-111111111111",
  "type": "wrapper",
  "group": "user",
  "path": "",
  "operator": "and",
  "children": [
    {
      "uuid": "22222222-2222-2222-2222-222222222222",
      "type": "string",
      "group": "user",
      "path": "data.plan",
      "operator": "=",
      "value": "professional"
    },
    {
      "uuid": "33333333-3333-3333-3333-333333333333",
      "type": "wrapper",
      "group": "event",
      "path": "name",
      "operator": "and",
      "value": "order_placed",
      "frequency": {
        "period": { "type": "rolling", "unit": "day", "value": 30 },
        "operator": ">=",
        "count": 1
      }
    }
  ]
}
```

Refer to the **RuleNode**, **RuleType**, **RuleGroup**, and **RuleFrequency** schemas in the [API reference](https://spotzee.com/docs/main-api/introduction.md) for the exact enum values.

**3. Create the list**

`POST /lists` with `type: "dynamic"`:

```http theme={null}
POST /api/client/lists HTTP/1.1
Host: apix.spotzee.com
Authorization: Bearer sk_…
Idempotency-Key: segment-pro-monthly
Spotzee-Version: 2026-04-28
Content-Type: application/json

{
  "name": "Professional plan, active in last 30 days",
  "type": "dynamic",
  "rule": {
    "uuid": "11111111-1111-1111-1111-111111111111",
    "type": "wrapper",
    "group": "user",
    "path": "",
    "operator": "and",
    "children": [
      {
        "uuid": "22222222-2222-2222-2222-222222222222",
        "type": "string",
        "group": "user",
        "path": "data.plan",
        "operator": "=",
        "value": "professional"
      },
      {
        "uuid": "33333333-3333-3333-3333-333333333333",
        "type": "wrapper",
        "group": "event",
        "path": "name",
        "operator": "and",
        "value": "order_placed",
        "frequency": {
          "period": {
            "type": "rolling",
            "unit": "day",
            "value": 30
          },
          "operator": ">=",
          "count": 1
        }
      }
    ]
  }
}
```

The response includes the list `id`. Save it. That's how you'll reference the segment from journeys, campaigns, and member-listing endpoints.

**4. Verify membership**

A new dynamic list starts as a draft. Inspect its matching contacts in the Spotzee app, then publish the intended rule before using the segment. A contact search does not evaluate the list's complete rule tree and is not a substitute for checking membership.

**5. Update the rule**

`PATCH /lists/{listId}` with a new `rule` block. The updated rule determines which contacts match when the segment is evaluated. For a draft list, include `published: true` when you are ready to publish it.

## Pitfalls to avoid

> [!WARNING]
>
> Don't build a hot loop that recreates the same segment every minute. Recomputation is cheap but not free. Update the rule in place with `PATCH` rather than deleting and recreating.

* **Don't filter on fields you haven't populated yet.** A rule on `data.plan` returns zero members until at least one contact has that field. Sync your contacts first ([Sync contacts](https://spotzee.com/docs/guides/sync-users.md)), then build segments.
* **Don't bury the rule in too many layers.** Spotzee accepts deeply nested groups, but a rule that spans more than two `and`/`or` layers is hard for operators to read in the UI.

## Reference

* API surface: see [Main API → Lists](https://spotzee.com/docs/main-api/introduction.md)
* Rule node schema: see **RuleNode** in the [API reference](https://spotzee.com/docs/main-api/introduction.md)
* Sync contacts first: see [Sync contacts from your CRM](https://spotzee.com/docs/guides/sync-users.md)
* Pagination: see [Pagination](https://spotzee.com/docs/guides/api-pagination.md)
* Conceptual overview: see [Segments](https://spotzee.com/docs/guides/segments.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.