The V2 Segmentation API allows you to create and manage segments using a subset of SQL that includes statements and operations necessary to define any segment. In addition, this version of the Segmentation API exposes the contact_data and event_data tables to enable SQL-compatible queries.
You can use this API to engage your contacts that meet specific criteria that you specify from a set of data. The segment can be based anything from a combination of criteria on a set of the contacts' attributes, as well as any contacts that have specific engagement events that also satisfy the aforementioned criteria.
The Segmentation V2 API uses SQL instead of the proprietary SendGrid Query Language (sgql) to specify segments, meaning you do not need to learn a new query language and can leverage skills in widely used SQL. Since this version of the API exposes the contact_data and event_data tables and gives you SQL access, any specification that is expressible via SQL that would result in a meaningful segment given the structure of the table is possible. Some segments that we can build now that we couldn't with V1 are:
contact_id
, and updated_at
in the primary select clause. This in turn constrains the rest of the query in specific ways which is an effect of how SQL works. The examples in the reference will clarify these aspects of the V2 API.Before you can use the V2 Segmentation API you will need to do the following:
Below are some examples of segments you can create using the V2 Segmentation API. For more code samples and use cases, see the Marketing Campaigns Segmentation V2 Query Reference.
Example Body
1{2"name":"string (required)",3"query_dsl":"SQL string (required)",4"parent_list_ids":"array[string] (optional)"5}
Select all contacts
SELECT contact_id, updated_at FROM contact_data
Select all contacts who engaged with an email by clicking
1SELECT c.contact_id, c.updated_at2FROM contact_data as c3JOIN event_data as e ON c.contact_id= e.contact_id WHERE e.event_type = 'clicked'
You can manually migrate an existing segment from v1 to v2 from the Marketing Campaigns user interface by duplicating the segment. The new duplicate will be created using the v2 API.
You should also delete any segments created using the v1 API once the v2 replacement is created.
You can manually migrate an existing v1 segment to v2 using the Segmentation APIs by creating a new v2 segment with the same filtering criteria as an existing v1 segment. For code samples, see the API reference linked in the appropriate steps.
GET
List of Segments v1 endpoint if you have not done so already.query_dsl
string containing a SQL query that will filter contacts to construct the segment. This query can be modified and used to create the same segment with the v2 API.query_dsl
string retrieved from your v1 segment to conform to v2 query_dsl
format. See Marketing Campaigns V2 Segmentation Query Reference, to modify and structure your query.query_dsl
string is modified to work with the v2 query language, you can pass it to the Create Segment v2 API endpoint, which requires a query_dsl
string.You should also delete any segments created using the v1 API once the v2 replacement is created.
GET
List of Segments v1 endpoint if you have not done so already.