> ## 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.

# Filter syntax

> The expression language used by the filter field of segments and triggers

Segments, triggers and `if` steps take a `filter`: an expression that is true for the customers it
should match. The segment and trigger editors in the embed write these expressions for you. Write
them yourself when you create segments or triggers through the API, as in
[Build segments and triggers with the API](https://docs.pocketflows.com/how-to/build-segments-and-triggers-with-the-api.md).

```
customer.plan == "gold" && Date.now() - Date(customer.last_check_in_at) > 2592000
```

## Variables

| Variable | Available in | Reads |
| - | - | - |
| `customer.<property>` | Segments, trigger filters, `if` steps | A built-in property (`full_name`, `first_name`, `last_name`, `email_address`, `phone_number`), or else the key of that name in the customer's `metadata` |
| `customer.external_id` | Segments | The customer's `external_id` |
| `business.<key>` | Trigger filters, `if` steps | The business's `name` and `metadata` |
| `event.<key>` | Trigger filters, `if` steps | The `data` of the event that started the trigger |

Use dots for nested keys: `customer.address.city` reads `metadata.address.city`. Names are
case-sensitive, so `customer.Plan` and `customer.plan` are different keys.

> [!WARNING]
>
> A segment filter can only use `customer.` variables. A segment whose filter uses any other
> variable matches no customers.

## Values

| Type | Write it as |
| - | - |
| String | `"gold"`, in double quotes |
| Number | `30`, `4.5`, or `-1` |
| Boolean | `true` or `false` |
| Null | `null` |
| List | `["gold", "silver"]` |
| Date | `Date("2026-01-01T00:00:00Z")`, from ISO 8601 |

Sync numbers as JSON numbers, not strings: `"visits": 12` rather than `"visits": "12"`. A number
stored as a string doesn't compare reliably with `>` or `<`.

## Operators

From loosest to tightest binding:

| Operator | Meaning |
| - | - |
| `\|\|` | Or |
| `&&` | And |
| `==` `!=` `>` `>=` `<` `<=` `in` | Comparison, and whether a value is in a list |
| `+` `-` | Addition and subtraction |
| `*` | Multiplication |
| `!` | Not, written before an expression |

Use parentheses to group: `(customer.plan == "gold" || customer.plan == "silver") && customer.vip == true`.
Division isn't supported, and `and`, `or` and `not` aren't keywords.

## Functions

| Function | Returns |
| - | - |
| `has(x)` | Whether the customer, business or event has a value for `x` |
| `x.contains("text")` | Whether the string `x` contains `text` |
| `x.lowerAscii()` | `x` in lowercase, for comparisons that ignore case |
| `x.size()` | The number of items in the list `x` |
| `Date(x)` | The date and time in the ISO 8601 string `x` |
| `Date.now()` | The current date and time |

Subtracting one date from another gives the difference in seconds. Dates are compared in UTC.

## Examples

| To match customers who... | Filter |
| - | - |
| are on the gold plan | `customer.plan == "gold"` |
| are on one of several plans | `customer.plan in ["gold", "silver"]` |
| have a tag | `"vip" in customer.tags` |
| don't have a tag (see below) | `!("vip" in customer.tags)` |
| haven't checked in for 30 days | `Date.now() - Date(customer.last_check_in_at) > 2592000` |
| signed up in the last 7 days | `Date.now() - Date(customer.signed_up_at) <= 604800` |
| have an email address at a domain, in any case | `customer.email_address.lowerAscii().contains("@example.com")` |
| have never been given a value for a key | `!has(customer.last_visit_at)` |
| (trigger filter) booked a particular service | `event.service == "Haircut"` |

Common durations in seconds: 1 day is `86400`, 7 days is `604800`, 30 days is `2592000`, and 90 days
is `7776000`.

## Missing values

`== null` and `!= null` are true or false as you'd expect for a missing value. Any other comparison
or arithmetic with a missing value doesn't match:

* **In a segment,** that comparison is false and the rest of the filter is still checked. A customer
  without `last_check_in_at` doesn't match `Date.now() - Date(customer.last_check_in_at) > 2592000`.
  The exception is `in` with a missing list: `"vip" in customer.tags` is neither true nor false for a
  customer without `tags`, so `!("vip" in customer.tags)` doesn't match them either.
* **In a trigger filter or `if` step,** the whole filter is false, even if another part of an `||`
  would match. Every part of the filter is checked, so `has()` can't guard a comparison: when a
  value can be missing, sync a default for it, such as `"visits": 0`.

So that "don't have a tag" filters match customers with no tags, sync an empty list for them, such as
`"tags": []`, rather than leaving the key out.

## Writing filters

* Separate tokens with spaces. Write the filter on a single line, without tabs or line breaks, and use
  double quotes for strings.
* Pocketflows checks that a filter parses when you save it, but not that the variables it uses
  exist. A misspelled key makes the filter match no one.


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