Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Create a Segment



API Overview

api-overview page anchor
(warning)

Legacy Marketing Campaigns

You are viewing the Legacy Marketing Campaigns API reference. For guidance migrating to the current version of Marketing Campaigns, see Migrating from Legacy Marketing Campaigns

(information)

Info

For the most up-to-date information on the Segments API, please visit the new Marketing Campaigns Segments v2 API.

The Segments API allows you to create and manage segments for your contacts. Segments are used to group contacts based on conditions you set. For example, you might create a segment for contacts who have opened a certain number of your emails, or for contacts who have not opened any of your emails in the last 30 days.


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

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

authentication page anchor
Property nameTypeRequiredDescription
Authorizationstringrequired
Default: Bearer <<YOUR_API_KEY_HERE>>

on-behalf-ofstringOptional

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.

SchemaExample
Property nameTypeRequiredDescriptionChild properties
namestringrequired

The name of this segment.


list_idintegerOptional

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.


conditionsarray[object]required

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


recipient_countnumberOptional

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

200400401
SchemaExample
Property nameTypeRequiredDescriptionChild properties
idnumber

The ID of the segment.


namestring

The name of this segment.


list_idinteger

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.


conditionsarray[object]

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


recipient_countnumber

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

Create a Segment

create-a-segment page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
curl

_43
const client = require('@sendgrid/client');
_43
client.setApiKey(process.env.SENDGRID_API_KEY);
_43
_43
const data = {
_43
"name": "Last Name Miller",
_43
"list_id": 4,
_43
"conditions": [
_43
{
_43
"field": "last_name",
_43
"value": "Miller",
_43
"operator": "eq",
_43
"and_or": ""
_43
},
_43
{
_43
"field": "last_clicked",
_43
"value": "01/02/2015",
_43
"operator": "gt",
_43
"and_or": "and"
_43
},
_43
{
_43
"field": "clicks.campaign_identifier",
_43
"value": "513",
_43
"operator": "eq",
_43
"and_or": "or"
_43
}
_43
],
_43
"recipient_count": 1234
_43
};
_43
_43
const request = {
_43
url: `/v3/contactdb/segments`,
_43
method: 'POST',
_43
body: data
_43
}
_43
_43
client.request(request)
_43
.then(([response, body]) => {
_43
console.log(response.statusCode);
_43
console.log(response.body);
_43
})
_43
.catch(error => {
_43
console.error(error);
_43
});


Rate this page: