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

# Build a segment

> Define an audience, check its size before saving it, and save it for reuse.

Goal: define an audience, check its size before saving it, and save it for reuse.

## The rules

* Each condition is one field, one operator and one value.
* **All conditions must hold** — they are combined with AND.
* Three fields compare numbers (`totalAmountSpent`, `totalTransactions`,
  `lastActivityDays`) and accept `gt`, `lt` or `eq`.
* `country` compares text and accepts `eq` only: there is no meaningful ordering
  of country names, so the contract, the builder and the API all reject anything
  else with a `validation_failed` problem.

## In the app

<Steps>
  ### New segment

  Open **Segments** and select **New segment**.

  ### Name it

  Give it a name.

  ### Set a condition

  Choose a field, an operator, and type a threshold.

  ### Read the audience size

  It updates as you type, before anything is saved — the API counts matching
  customers for unsaved conditions.

  ### Narrow further

  Add further conditions to narrow the audience.

  ### Save

  The segment is stored with its conditions.
</Steps>

## From the API

Count an audience first:

```bash theme={null}
curl -X POST http://localhost:4000/api/v1/segments/preview \
  -H "authorization: Bearer $TOKEN" -H 'content-type: application/json' \
  -d '{"conditions":[{"field":"lastActivityDays","operator":"gt","value":90}]}'
```

```json theme={null}
{ "matchCount": 41 }
```

Then save it:

```bash theme={null}
curl -X POST http://localhost:4000/api/v1/segments \
  -H "authorization: Bearer $TOKEN" -H 'content-type: application/json' \
  -d '{"name":"Dormant 90 days","conditions":[{"field":"lastActivityDays","operator":"gt","value":90}]}'
```

The saved segment carries its conditions, so it stays readable and editable later.

## Notes

<Note>
  **A segment with no conditions is refused.** An empty audience is almost always a
  mistake, and a segment that matches everything is rarely what was meant.
</Note>

<Note>
  **The stored audience size is a snapshot.** It was true when the segment was
  written; customer activity changes. The composer re-counts when you preview.
</Note>

<Note>
  **Deleting a segment does not touch campaigns generated from it.** Each campaign
  keeps its own copy of the segment name, so past work stays legible.
</Note>


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