Skip to contentSkip to navigationSkip to topbar
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 (The Twilio SendGrid v3 API)

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.

Encoding type:application/json
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 SegmentLink to code sample: Create a Segment
1
const client = require('@sendgrid/client');
2
client.setApiKey(process.env.SENDGRID_API_KEY);
3
4
const data = {
5
"name": "Last Name Miller",
6
"list_id": 4,
7
"conditions": [
8
{
9
"field": "last_name",
10
"value": "Miller",
11
"operator": "eq",
12
"and_or": ""
13
},
14
{
15
"field": "last_clicked",
16
"value": "01/02/2015",
17
"operator": "gt",
18
"and_or": "and"
19
},
20
{
21
"field": "clicks.campaign_identifier",
22
"value": "513",
23
"operator": "eq",
24
"and_or": "or"
25
}
26
],
27
"recipient_count": 1234
28
};
29
30
const request = {
31
url: `/v3/contactdb/segments`,
32
method: 'POST',
33
body: data
34
}
35
36
client.request(request)
37
.then(([response, body]) => {
38
console.log(response.statusCode);
39
console.log(response.body);
40
})
41
.catch(error => {
42
console.error(error);
43
});