Menu

Segmenting Contacts

Segments are similar to contact lists, except they update dynamically over time as information stored about your contacts or the criteria used to define your segments changes. When you segment your audience, you are able to create personalized Automation emails and Single Sends that directly address the wants and needs of your particular audience.

The Marketing Campaigns Segments API allows you to create, edit, and delete segments as well as retrieve a list of segments or an individual segment by ID.

Note that Twilio SendGrid checks for newly added or modified contacts who meet a segment's criteria on an hourly schedule. Only existing contacts who meet a segment's criteria will be included in the segment searches within 15 minutes.

Segments built using engagement data such as "was sent" or "clicked" will take approximately 30 minutes to begin populating.

Segment samples and counts are refreshed approximately once per hour; they do not update immediately. If no contacts are added to or removed from a segment since the last refresh, the sample and UI count displayed will be refreshed at increasing time intervals with a maximum sample and count refresh delay of 24 hours.

For more information on creating segments with the Twilio SendGrid Marketing Campaigns UI see "Segmenting your Contacts."

For help with Segmentation Query Language, see our Segmentation Query Language reference

Create Segment

Create Segment

POST /marketing/segments

This endpoint allows you to create a segment.

Authentication

  • API Key

Headers

Authorization
string
default: Bearer <<YOUR_API_KEY_HERE>>
required

Request Body

object
name
string

Name of the segment.

maxLength: 100minLength: 1
required
parent_list_ids
array[string]

The array of list ids to filter contacts on when building this segment. It allows only one such list id for now. We will support more in future

uniqueItems: True
query_dsl
string

SQL query which will filter contacts based on the conditions provided

required
parent_list_id
string

The id of the list if this segment is a child of a list. This implies the query is rewritten as (${query_dsl}) AND CONTAINS(list_ids, ${parent_list_id})

format: uuidmaxLength: 36minLength: 36

Responses

object
id
string
format: uuidmaxLength: 36minLength: 36
required
contacts_count
integer
required
created_at
string

ISO8601 of created timestamp

format: date-time
required
name
string

Name of the segment.

maxLength: 100minLength: 1
required
parent_list_id
string

The id of the list if this segment is a child of a list. This implies the query AND CONTAINS(list_ids, ${parent_list_id})

format: uuidmaxLength: 36minLength: 36
sample_updated_at
string

ISO8601 timestamp the sample was last updated

format: date-time
required
updated_at
string

ISO8601 timestamp the object was last updated

format: date-time
required
next_sample_update
string

ISO8601 string that is equal to sample_updated_at plus an internally calculated offset that depends on how often contacts enter or exit segments as the scheduled pipeline updates the samples.

contacts_sample
array[object]
required
id
string

ID assigned to a contact when added to the system.

format: uuidmaxLength: 36pattern: [a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}
email
string

Email of the contact. This is a reserved field.

format: emailmaxLength: 254minLength: 3
alternate_emails
array[string]

Alternate emails of the contact. This is a reserved field.

minItems: 0uniqueItems: True
first_name
string

First name of the contact. This is a reserved field.

minLength: 1
last_name
string

Last name of the contact. This is a reserved field.

minLength: 1
address_line_1
string

First line of address of the contact. This is a reserved field.

minLength: 0
address_line_2
string

Second line of address of the contact. This is a reserved field.

minLength: 0
city
string

City associated with the contact. This is a reserved field.

minLength: 0pattern: ^[a-zA-Z\u0080-\u024F\s\/\-\)\(\`\.\"\']+$
state_province_region
string

State associated with the contact. This is a reserved field.

minLength: 0
postal_code
integer

Zipcode associated with the address of the contact. This is a reserved field.

country
string

Country associated with the address of the contact. This is a reserved field.

minLength: 0
list_ids
array[string]

IDs of all lists the contact is part of

uniqueItems: True
custom_fields
object

The user may choose to create up to 120 custom fields or none at all. This is not a reserved field.

custom_field_name1
string
minLength: 0
custom_field_name2
string
minLength: 0
string
segment_ids
array[string]

IDs of all segments the contact is part of

uniqueItems: True
query_json
object

AST representation of the query DSL

parent_list_ids
array[string]

The array of list ids to filter contacts on when building this segment. It allows only one such list id for now. We will support more in future

uniqueItems: True
query_dsl
string

SQL query which will filter contacts based on the conditions provided

required
{
  "id": "58567a46-305e-48d1-b4f8-a006c906920e",
  "contacts_count": 9266921,
  "created_at": "2085-08-08T21:07:05.692Z",
  "sample_updated_at": "3407-09-25T04:25:02.140Z",
  "updated_at": "4431-05-07T22:23:22.352Z",
  "contacts_sample": [
    {
      "contact_id": "c1183ada-b784-49ac-9b1f-50e73578a6dc",
      "primary_email": "ft88@d.izxx",
      "alternate_emails": [
        "yKDUP11FDch@QoU.vwy",
        "ZNSN5@czAMqPi.at",
        "7wr51kFVVKlcBSH@DWxOueOZepetzBrku.qosk",
        "iib-ObtO7Fxz5@vLJPRIFKPOqJGCEqcIDab.ypn"
      ],
      "first_name": "est",
      "last_name": "eiusmod in laboris velit cupidatat",
      "address_line_1": "sunt aliqua",
      "address_line_2": "sit proident Lorem veniam labore",
      "city": "\u009c\u020e\u0163\u0238\u00db\t\u010d\u000bC\u0141",
      "state_province_region": "ut proident",
      "postal_code": 30296612,
      "country": "do reprehenderit qui",
      "custom_fields": {
        "custom_field_name2": "in consectetur ut aliqua sint",
        "custom_field_name1": "esse"
      }
    }
  ],
  "name": "culpa",
  "query_dsl": "cillum eiusmod",
  "parent_list_id": "2357714d-3d82-4c80-826c-b44a4147f81c",
  "next_sample_update": ""
}
object
errors
array[object]
message
string

the error message

field
string

the field that generated the error

help
object

helper text or docs for troubleshooting

id
string
{
  "errors": [
    {
      "field": "field_name",
      "message": "error message"
    }
  ]
}
object
errors
array[object]
message
string

the error message

field
string

the field that generated the error

help
object

helper text or docs for troubleshooting

id
string
{
  "errors": [
    {
      "field": "field_name",
      "message": "error message"
    }
  ]
}
object
errors
array[object]
message
string

the error message

field
string

the field that generated the error

help
object

helper text or docs for troubleshooting

id
string
{
  "errors": [
    {
      "field": "field_name",
      "message": "error message"
    }
  ]
}
object
errors
array[object]
required
message
string

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the SendGrid tag on Stack Overflow.