Get Started with Lytics Segments

View as Markdown
Last updated November 20, 2026

Get Started with Lytics Segments

This guide takes an in-depth look at Lytics segments. It covers how to define a segment with SegmentQL or SegmentAST, and how to use the Segment API endpoints to create, read, update, delete, scan, and list the segments in your account.

This guide uses the terms segment and audience interchangeably. Specifically, an audience is a segment on the user table. Your Lytics account also has a separate table and schema for content; a segment on that table is often called a content collection.

Difficulty: Beginner

TL;DR: Check out the Segment API documentation.

Other resources:

Prerequisites

  • httpie - (Optional) A more user-friendly alternative to cURL, used in this guide for readability.
  • Lytics Command Line Tool - (Optional) This guide shows how to run commands with the Lytics CLI for each endpoint it supports.
  • A valid API token for your Lytics account (learn about managing API tokens). For ease of use, add it as an environment variable in your command line:
export LIOKEY={API Token}

1. Defining A Segment

If you haven't yet, take a minute to look at the audience builder in your account. To open it, go to Audiences & Profiles > Audiences and click + Create New.

The audience builder is a visual interface for what this first section covers: defining a segment. At its core, a segment definition is a conditional statement. If the data collected on a user meets the segment's conditions, that user is a member. Lytics has two syntaxes for these conditional segment definitions: SegmentQL and SegmentAST.

SegmentQL

SegmentQL is a simple query language built on filter statements. Before diving into the syntax, look at the SegmentQL definition of an example segment.

FILTER OR (
  AND (
    cm_status = "active"
    INCLUDE last_open_7_days
  )
  last_active_ts > "now-7d"
)
FROM user 
ALIAS active_web_or_email

In this example, cm_status refers to the subscriber status in Campaign Monitor, an email provider. A marketer might build this audience to target all users who were active across email and web channels in the last 7 days.

If you're a developer, this query language is probably intuitive. The Lytics UI lets marketers build audiences, but you might find it quicker to write SegmentQL and create a segment through the API. Here is the anatomy of a filter statement:

FILTER [phrase] [from] [alias]
[phrase]Notes
AND ([phrase], [phrase], ...)
OR ([phrase], [phrase], ...)
NOT [phrase]
EXISTS [identifier][identifier] is a field name.
[identifier] IN ([literal], [literal], ...)
[identifier] CONTAINS [literal]
[identifier] INTERSECTS ([literal], [literal], ...)INTERSECTS = "contains one of"
[identifier] LIKE [literal][literal] must be a string. Can use * as a wildcard.
INCLUDE [identifier][identifier] is a segment ID or slug (to include an existing segment).
[identifier] > [literal]
[identifier] >= [literal]
[identifier] < [literal]
[identifier] <= [literal]
[identifier] = [literal]
[identifier] != [literal]
  • [identifier] = an alphanumeric string (often the name of a data field, table, or segment).
  • [literal] = a string, int, float, boolean, or timestamp literal value.
[from]Notes
FROM [identifier][identifier] is the name of the table, such as user or content.
  • [from] is optional if creating a filter on the user table.
[alias]Notes
ALIAS [identifier][identifier] is the saved name of the segment.
  • [alias] is optional if you do not wish to save the segment.

SegmentAST

SegmentAST expressions are similar to SegmentQL, but instead of using a filter syntax, they're formatted as JSON. SegmentAST is less readable for humans, but if you're writing a program that creates or updates segment definitions, it might be easier to format your request bodies in SegmentAST. Here is the same example from above in SegmentAST.

{
  "op": "or",
    "args": [{
    "op": "and",
    "args": [{
      "op": "=",
      "args": [{
        "ident": "cm_status"
      },
      {
        "val": "active"
      }]
    },
    {
      "op": "include",
      "args": [{
        "ident": "last_open_7_days"
      }]
    }]
  },
  {
    "op": ">",
    "args": [{
      "ident": "last_active_ts"
    },
    {
      "val": "now-7d"
    }]
  }]
}

It isn't as easy to read as SegmentQL, but if you scan the JSON, you'll see it contains the same expressions. Here is a breakdown of the key components:

keytypeNotes
opstringoperator used to evaluate the args
argsarraya list of objects containing the identifiers and literals to operate on
identstringidentifier name
valstringliteral value

2. Segment Validate API

The segment validate endpoint accepts a plain-text SegmentQL statement and checks that the definition is valid. This is especially useful when creating a segment: you might want to validate the definition before you send a request to build the segment through the API.

echo 'FILTER AND (
  utm_campaigns CONTAINS "summer_froyo_promo",
  EXISTS email,
  scores.momentum > 40
)' | http POST https://api.lytics.io/api/segment/validate access_token==$LIOKEY

This endpoint does not validate field names in your account. It only checks the syntax of the SegmentQL statement.

3. Segment CRUD API

Segment Create

The segment create endpoint accepts plain text (containing SegmentQL) or a JSON body (SegmentQL or SegmentAST).

# example 1: plain text
echo 'FILTER (
  lytics_content.Yogurt >= 0.75
)
FROM user 
ALIAS affinity_for_yogurt
' | http POST https://api.lytics.io/api/segment Content-type:text/plain access_token==$LIOKEY
# example 2: json segmentQL
echo '{
  "name": "High Affinity for Yogurt",
  "segment_ql": "FILTER (lytics_content.Yogurt >= 0.75)",
  "slug_name": "affinity_for_yogurt",
  "is_public": true,
  "description": "Segment containing all users highly interested in the topic Yogurt.",
  "table": "user"

}' | http POST https://api.lytics.io/api/segment Content-type:application/json access_token==$LIOKEY
# example 3: json segmentAST
echo '{
  "name": "High Affinity for Yogurt",
  "slug_name": "affinity_for_yogurt",
  "is_public": true,
  "description": "Segment containing all users highly interested in the topic Yogurt.",
  "table": "user",
  "ast": {
    "op": ">=",
    "args": [{
      "ident": "lytics_content.Yogurt"
    },
    {
      "val": "0.75"
    }]
  }

}' | http POST https://api.lytics.io/api/segment Content-type:application/json access_token==$LIOKEY

Segment Read

You need the segment ID or slug name to fetch the segment through the read endpoint.

# example 1:
http GET https://api.lytics.io/api/segment/affinity_for_yogurt access_token==$LIOKEY
# example 2: lytics cli
lytics --segments=affinity_for_yogurt segment

Segment Update

You need the ID or slug of the segment to update.

echo '{
  "slug_name": "affinity_for_yogurt",
  "segment_ql": "FILTER (lytics_content.Yogurt >= 0.9)"
}' | http PUT https://api.lytics.io/api/segment/affinity_for_yogurt access_token==$LIOKEY

Segment Delete

You need the ID or slug of the segment to delete.

http DELETE https://api.lytics.io/api/segment/affinity_for_yogurt access_token==$LIOKEY

4. Segment List API

Many of the CRUD operations above require the segment's ID or slug name, so a list of all existing segments in your account is a useful reference. The list endpoint returns all segments in your account, including each segment's ID, slug, and SegmentQL and/or SegmentAST definition.

# example 1: list all audiences
http GET https://api.lytics.io/api/segment access_token==$LIOKEY
# example 2: lytics cli
lytics segment
# example 3: list all content collections
http GET https://api.lytics.io/api/segment access_token==$LIOKEY table==content

5. Segment Scan API

The scan endpoint returns a list of all entities in a segment. For audiences (segments on the user table), the entities returned are user profiles. The scan endpoint accepts a number of query parameters. This guide doesn't cover all of them; see the Segment API reference for full details on this API.

You can use this endpoint to scan an existing, saved segment, in which case you need the segment ID or slug. You can also scan an ad hoc segment by passing SegmentQL to the endpoint.

# example 1: existing audience
http GET https://api.lytics.io/api/segment/affinity_for_yogurt/scan access_token==$LIOKEY ortfield==last_purchase_ts
# example 2: lytics cli
lytics --id=affinity_for_yogurt segmentscan
# example 3: ad-hoc content collection
echo 'FILTER OR (
  global.Yogurt > 0.5,
  global.`Ice Cream` > 0.5
)
FROM content
' | http GET https://api.lytics.io/api/segment/scan access_token==$LIOKEY limit==10
# example 4: ad-hoc lytics cli
lytics segmentscan '
FILTER (
  aspects INTERSECTS ("article", "video")
)
FROM content'

If the total number of entities in the segment exceeds the limit in the request (the default is 20), the API paginates the results. The JSON body includes a token with the key next. To get the next page of results, make another request with the query parameter start set to the next token from the prior request. The API returns a next token in each response until you've scanned the entire segment.

What's next?

The endpoints covered here get you started with Lytics segments. A couple of segment APIs, mainly around segment size, aren't covered in this guide. They're straightforward; see the Segment API reference if you're interested. If you're unfamiliar with the fields in your Lytics account, the catalog schema and query APIs can also help. Everything in the Lytics web UI is built on the Lytics APIs, so you never need the UI to complete a task in Lytics. With the time and resources, you can build many kinds of custom applications that interface with your Lytics account through the APIs. Lytics is excited to work with developers on new use cases. If the docs aren't enough, you can always contact Lytics.