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

# Set Opening Templates by Segment

> Choose a task's opening WhatsApp template by lead segment, for example one template per campaign. The list you send replaces the whole configuration.

Use this when leads of the same task should get a different first WhatsApp message depending on where they came from, for example one template per campaign for leads you upload in bulk. Read the current segments with [Get a WhatsApp Template Pool](https://docs.getnexor.ai/docs/en/api/templates/get-template-pools) (`opening_segments`). MCP keys need `whatsapp:write`; the MCP tool is `set_opening_segments`.

## Set the segments

```json theme={null}
{
  "workflow_id": "7d3c1a52-5b8e-4f0e-9c1a-2b6f4e8d9a10",
  "segments": [
    {
      "name": "Instagram",
      "condition_rules": { "field": "metadata.campaign", "op": "==", "value": "instagram" },
      "templates": [
        { "name": "spring_promo_ig_v1", "weight": 3 },
        { "name": "spring_promo_ig_v2" }
      ]
    },
    {
      "name": "TikTok",
      "condition_rules": { "field": "metadata.campaign", "op": "in", "value": ["tiktok", "tiktok_ads"] },
      "templates": [{ "name": "spring_promo_tiktok_v1" }]
    },
    {
      "name": "Everyone else",
      "templates": [{ "name": "spring_promo_general_v1" }]
    }
  ]
}
```

* The list replaces the whole configuration. Send every segment you want to keep. `"segments": []` removes it.
* Up to 20 segments. Each `name` is required and appears once.
* Segments are checked in order. The first one whose `condition_rules` match the lead wins.
* Inside a segment, one template is drawn by `weight`: an integer from 1 to 100, `1` when you leave it out. In the example, `spring_promo_ig_v1` goes out about three times as often as `spring_promo_ig_v2`.
* Each segment has 1 to 10 templates, with no repeated names inside a segment.
* Every template must be APPROVED on the account's WhatsApp and written in the agent's language.

## Who gets which template

| Lead | Opening template |
| - | - |
| Matches a segment's `condition_rules` | One template of the first matching segment, drawn by weight |
| Matches no segment, and the last segment has no `condition_rules` | One template of that last segment (the catch-all) |
| Matches no segment, and there is no catch-all | The task's normal opening template, set with [Configure Opening Templates](https://docs.getnexor.ai/docs/en/api/templates/put-template-pools-opening) |

A segment without `condition_rules` is the catch-all and must be the last one. Without a catch-all, leads that don't carry the segment field keep getting the normal opening template.

## Condition rules

A rule is `{ "field", "op", "value" }`, or a group `{ "operator": "and" | "or", "rules": [...] }` with up to 10 single rules. Groups do not nest.

| Part | Values |
| - | - |
| `field` | A lead path: `metadata.<key>` (for example a column of the file you uploaded), `raw_payload.<key>`, or `source` (`csv_import`, `api`, ...) |
| `op` | `==`, `!=`, `>`, `<`, `>=`, `<=`, `in` (`value` is an array), `exists` (no `value`), `days_until_lt`, `days_until_lte`, `days_until_gt`, `days_until_gte` (days left until a date in `field`) |
| `value` | What to compare against. Required for every `op` except `exists` |

```json theme={null}
{
  "operator": "and",
  "rules": [
    { "field": "source", "op": "==", "value": "csv_import" },
    { "field": "metadata.campaign", "op": "==", "value": "instagram" }
  ]
}
```

## Errors

| Status | When |
| - | - |
| 400 `Invalid input` | Wrong shape or limits, duplicate names, a catch-all that is not last, or a template that is unknown or not APPROVED. `message` names the segment or template |
| 400 `INVALID_CONDITION_RULES` | A malformed rule. `reason` gives the cause, for example `condition_rules_in_needs_array` |
| 400 `TEMPLATE_LANGUAGE_MISMATCH` | A template is not in the agent's language. Pick one in that language |
| 404 | The task does not exist in this account |


## API Specification

The full API specification for this endpoint is available in the [documentation index](https://docs.getnexor.ai/llms.txt).


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