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

# Build segments and triggers with the API

> Create an audience and an automated message for a business from your own code, using a win-back text as the example

Businesses usually build segments and triggers themselves in the embed. This guide is for when you
want to set them up from your code instead, for example to give every new business a ready-made
automation.

The example texts gym members who haven't checked in for 30 days, limited to members on the gold
plan.

## 1. Sync the data the filter needs

The segment can only filter on data Pocketflows has, so sync each member's plan and last check-in time
in their `metadata`, along with a phone number:

```
POST /sync/businesses/:business_id/customers

Body
{
  "external_id": "member_123",
  "phone_number": "+14155550123",
  "first_name": "Sam",
  "metadata": {
    "plan": "gold",
    "last_check_in_at": "2026-11-01T16:42:03Z"
  }
}
```

Send dates as ISO 8601 strings, and sync again whenever a member checks in, so `last_check_in_at`
stays current.

## 2. Create the segment

Create a segment whose `filter` describes the members you want, written in the
[filter syntax](https://docs.pocketflows.com/reference/filter-syntax.md). 30 days is 2,592,000 seconds:

```
POST /businesses/:business_id/segments

Body
{
  "name": "Gold members who haven't checked in for 30 days",
  "filter": "customer.plan == \"gold\" && Date.now() - Date(customer.last_check_in_at) > 2592000"
}
```

The response includes the segment's `id`, which starts with `seg_`.

## 3. Create the trigger

Create a trigger that runs when a customer joins the segment, with the steps to run for them:

```
POST /businesses/:business_id/triggers

Body
{
  "name": "Win back lapsed gold members",
  "internal_event_definition": {
    "type": "customer_joined_segment",
    "segment": "seg_..."
  },
  "steps": [
    {
      "type": "sms",
      "message": "Hi {{ customer.first_name }}, we miss you at {{ business.name }}! Your next class is on us."
    }
  ]
}
```

To listen for an event you track instead, pass `project_defined_event` with the event's ID in place of
`internal_event_definition`. See
[Send messages when something happens in your product](https://docs.pocketflows.com/how-to/trigger-messages-from-events.md).

The trigger sends from the business's default phone number and email address. To choose others, pass
the ID of one in `phone_number` or `email_address`.

### Steps

Steps run in order for each customer:

| Step | Fields | Does |
| - | - | - |
| `email` | `subject` and `body`, or `email_template_id` | Sends an email |
| `sms` | `message`, or `sms_template_id` | Sends an SMS |
| `wait` | `duration`, in seconds | Waits before the next step |
| `if` | `filter` | Continues only if the filter matches the customer; otherwise stops |

For example, to send a reminder email a week after the text, but only to members who still haven't
checked in:

```json theme={null}
"steps": [
  { "type": "sms", "message": "Hi {{ customer.first_name }}, we miss you!" },
  { "type": "wait", "duration": 604800 },
  {
    "type": "if",
    "filter": "Date.now() - Date(customer.last_check_in_at) > 2592000"
  },
  {
    "type": "email",
    "subject": "Your next class is on us",
    "body": "Hi {{ customer.first_name }}, come back this week and your next class is free."
  }
]
```

## 4. Activate the trigger

A new trigger is a draft, with `status` set to `draft`, and does nothing until you activate it:

```
POST /triggers/:id/activate
```

Its `status` becomes `active`. To stop it later, call `POST /triggers/:id/deactivate`.

## How the trigger fires

* **Only new members are messaged.** Customers who are already in the segment when you activate the
  trigger don't receive anything. The trigger fires for customers who join afterwards.
* **Time passing counts.** Because the filter compares against `Date.now()`, a member joins the
  segment on the day they pass 30 days without a check-in, even if nothing about them changed.
* **It isn't instant.** Pocketflows checks segment membership about every 15 minutes, so a customer
  is messaged up to about 25 minutes after they join.
* **It can fire again.** When a member checks in, they leave the segment. If they lapse again later,
  they rejoin and the trigger runs again.

## Related

* [Filter syntax](https://docs.pocketflows.com/reference/filter-syntax.md) for every operator and function a filter can use.
* [How events and triggers work](https://docs.pocketflows.com/concepts/events-and-triggers.md) for the model behind triggers.
* [Let businesses build segments from your data](https://docs.pocketflows.com/how-to/set-up-segment-filters.md) to let businesses
  build their own segments in the embed instead.


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