Menu

Expand
Rate this page:

Contacts API - Segments

Elements that can be shared among more than one endpoint definition.

Create a Segment

POST /v3/contactdb/segments

Base url: https://api.sendgrid.com

This endpoint allows you to create a new segment.

Valid operators for create and update depend on the type of the field for which you are searching.

Dates - "eq", "ne", "lt" (before), "gt" (after) - You may use MM/DD/YYYY for day granularity or an epoch for second granularity. - "empty", "not_empty" - "is within" - You may use an ISO 8601 date format or the # of days.

Text - "contains" - "eq" (is/equals - matches the full field) - "ne" (is not/not equals - matches any field where the entire field is not the condition value) - "empty" - "not_empty"

Numbers - "eq" (is/equals) - "lt" (is less than) - "gt" (is greater than) - "empty" - "not_empty"

Email Clicks and Opens - "eq" (opened) - "ne" (not opened)

All field values must be a string.

Conditions using "eq" or "ne" for email clicks and opens should provide a "field" of either clicks.campaign_identifier or opens.campaign_identifier. The condition value should be a string containing the id of a completed campaign.

The conditions list may contain multiple conditions, joined by an "and" or "or" in the "and_or" field.

The first condition in the conditions list must have an empty "and_or", and subsequent conditions must all specify an "and_or".

Authentication

  • API Key

Headers

Authorization
string
default: Bearer <<YOUR_API_KEY_HERE>>
required
on-behalf-of
string

The on-behalf-of header allows you to make API calls from a parent account on behalf of the parent's Subusers or customer accounts. You will use the parent account's API key when using this header. When making a call on behalf of a customer account, the property value should be "account-id" followed by the customer account's ID (e.g., on-behalf-of: account-id <account-id>). When making a call on behalf of a Subuser, the property value should be the Subuser's username (e.g., on-behalf-of: <subuser-username>). See On Behalf Of for more information.

default: None

Request Body

object
name
string

The name of this segment.

required
list_id
integer

The list id from which to make this segment. Not including this ID will mean your segment is created from the main contactdb rather than a list.

conditions
array[object]

The conditions for a recipient to be included in this segment.

required
field
string
required
value
string
required
operator
string
Allowed Values: eq, ne, lt, gt, contains
required
and_or
string
Allowed Values: and, or,
recipient_count
number

The count of recipients in this list. This is not included on creation of segments.

{
  "name": "Last Name Miller",
  "list_id": 4,
  "conditions": [
    {
      "field": "last_name",
      "value": "Miller",
      "operator": "eq",
      "and_or": ""
    },
    {
      "field": "last_clicked",
      "value": "01/02/2015",
      "operator": "gt",
      "and_or": "and"
    },
    {
      "field": "clicks.campaign_identifier",
      "value": "513",
      "operator": "eq",
      "and_or": "or"
    }
  ],
  "recipient_count": 1234
}

Responses

object
id
number

The ID of the segment.

required
name
string

The name of this segment.

required
list_id
integer

The list id from which to make this segment. Not including this ID will mean your segment is created from the main contactdb rather than a list.

conditions
array[object]

The conditions for a recipient to be included in this segment.

required
field
string
required
value
string
required
operator
string
Allowed Values: eq, ne, lt, gt, contains
required
and_or
string
Allowed Values: and, or,
recipient_count
number

The count of recipients in this list. This is not included on creation of segments.

{
  "name": "Last Name Miller",
  "list_id": 4,
  "conditions": [
    {
      "field": "last_name",
      "value": "Miller",
      "operator": "eq",
      "and_or": ""
    },
    {
      "field": "last_clicked",
      "value": "01/02/2015",
      "operator": "gt",
      "and_or": "and"
    },
    {
      "field": "clicks.campaign_identifier",
      "value": "513",
      "operator": "eq",
      "and_or": "or"
    }
  ],
  "recipient_count": 1234
}
object
errors
array[object]
message
string

the error message

required
field
string or null

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

required
field
string or null

the field that generated the error

help
object

helper text or docs for troubleshooting

id
string
{
  "errors": [
    {
      "field": "field_name",
      "message": "error message"
    }
  ]
}
Loading Code Sample...
        
        

        Create a Segment

        Rate this page:

        Need some help?

        We all do sometimes. Get help now from the Twilio SendGrid Support Team.

        Running into a coding hurdle? Lean on the wisdom of the crowd by browsing the SendGrid tag on Stack Overflow or visiting Twilio's Stack Overflow Collective.

        Loading Code Sample...
              
              
              

              Thank you for your feedback!

              Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

              Sending your feedback...
              🎉 Thank you for your feedback!
              Something went wrong. Please try again.

              Thanks for your feedback!

              thanks-feedback-gif