HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityLog In

API Usage

This topic describes how to work with real-time segments via Optimizely Data Platform's (ODP's) APIs.

Overview

The following two APIs allow you to work with segments in different ways:

  • RealtimeSegments API - Allows you to create, delete, list and get segments.
  • GraphQL API - Allows you to view the results of existing segments. For more information, refer to the Querying Real-Time Segments section.

The RealtimeSegments API expands on what the GraphQL API allows you to do. This section focuses on the RealtimeSegments API. You can use the RealtimeSegments API to create new segments, delete existing segments, list all segments, and view segment definitions.

The hostname depends on your region, which you can find at https://???. This documentation uses api.zaius.com, but that hostname is only valid for the U.S. region.

These requests require your account id as well as your private API token. The account id goes in the first path segment, and the private API token goes in the header.

The data type for segment-id is string, but they must always conform to case insensitive regexp pattern [a-z][a-z0-9_]*.

Operations

The possible operations are

  • Create Segment
  • Update Segment
  • List Segments
  • Delete Segment
  • Get Segment

Create Segment

PUT https://{hostname}/v3/segments/{segment-id}

{
    description: {string},
    definition: {SegmentDefinition}
}

Parameter

Type

Example

segment-id

string

active_7_days

description

string

Customers that were active during the last week.
That is, customers with at least one event in the last 604800 seconds.

definition

SegmentDefinition

{
"description": "Users who were active in the last 7 days",
"rootCondition": {
"sequenceCondition": {
"sequence": {
"entryEventFilter": {
"pathReference": {
"value": "event.active_event"
}
},
"maxAgeSeconds": 604800
},
"minCount": 1
}
}
}

Responses

Status

Content-Type

Body

Comment

200

application/json

{
"revision": int
}

{
"revision": Starts at 0 when
segment is created,
incremented on update
}

400

application/json

{
"code": int,
"message": "string"
}

{
"code": Same as HTTP
status code.,
"message": "Provides
reason why segment
definition is invalid"
}

Example Request

curl -XPUT -H 'x-api-key: your-private-key' \
-H 'Content-Type': 'application/json' \
https://api.zaius.com/v3/segments/is_active \
-d '{
  "description": "Users who were active in the last 7 days",
  "rootCondition": {
    "sequenceCondition": {
      "sequence": {
      "entryEventFilter": {
        "pathReference": {
          "value": "event.active_event"
        }
      },
      "maxAgeSeconds": 604800
    },
    "minCount": 1
  }
}'

Update Segment

Updating a segment is similar to creating a segment. If you issue a PUT with a segment-id that already exists and a new segment definition, then that segment-id is updated with the new segment definition and the revision number is incremented.

Example Request

Assume we have two variables with segment definitions. The first one is ${ACTIVE_7_DAYS} and matches the Create Segment example body. The second one is ${ACTIVE_1_DAY}, which looks the same except that it has:

"maxAgeSeconds": 86400

This requires the user to have an active event within the last 86400 seconds (1 day).

The following requests would then:

  1. Create the segment "is_active", including users that were active in the last 7 days. This returns {"revision":0}.
  2. Update the description for segment "is_active". This returns {"revision":1}.
  3. Update the definition for segment "is_active". This returns {"revision":2}.
$ curl -XPUT -H'x-api-key: your-private-key' \
    https://api.zaius.com/v3/segments/is_active \
    -d @- << EOF
    {
      "description": "User was active in the last week.",
      "definition": ${ACTIVE_7_DAYS}
    }
    EOF
    {"revision":0}
    
    $ curl -XPUT -H'x-api-key: your-private-key' \
    https://api.zaius.com/v3/segments/is_active \
    -d @- << EOF
    {
      "description": "User was recently active",
      "definition": ${ACTIVE_7_DAYS}
    }
    EOF
    {"revision":1}

    $ curl -XPUT -H'x-api-key: your-private-key' \
    https://api.zaius.com/v3/segments/is_active \
    -d @- << EOF
    {
      "description": "User was recently active",
      "definition": ${ACTIVE_1_DAY}
    }
    EOF
    {"revision":2}

List Segments

GET https://{hostname}/v3/segments

Responses

Status

Content-Type

Body

200

application/json

{
segmentIds: [string]
}

Example Request

$ curl -H'x-api-key: your-private-key' \
https://api.zaius.com/v3/segments
{segmentIds: ["is_active"]}

Get Segment

Gets a previously defined segment.

GET https://{hostname}/v3/segments/{segment-id}

Responses

Status

Content-Type

Body

200

application/json

{
revision: int,
description: string,
definition: SegmentDefinition
}

400

application/json

{
status: 404,
requestId: null,
message: "Segment not found."
}

Delete Segment

Deletes a previously defined segment. Returns all segmentIds that exist after the delete.

DELETE https://{hostname}/v3/segments/{segment-id}

Responses

Status

Content-Type

Body

200

application/json

{
segmentIds: [string]
}

400

application/json

{
status: 404,
requestId: null,
message: "Segment not found."
}

Example Request

Assuming there are three installed segments with segmentIds "exampleOne", "exampleTwo" and "exampleThree":

$ curl -H'x-api-key: your-private-key' \
https://api.zaius.com/v3/segments/exampleTwo
{segmentIds: ["exampleOne", "exampleThree"]}

$ curl -H'x-api-key: your-private-key' \
https://api.zaius.com/v3/segments/exampleTwo
{"status": 404,"requestId":null,"message"Segment not found."}

Did this page help you?