Rate this page:

Getting Started the Marketing Campaigns V2 Segmentation API

The Segmentation v2 API is in public beta. Creating and editing v2 segments is available through the API only at this time.

You can see the v2 segments you have created with their contacts in the UI, but the details of a segment and the ability to edit a segment will not be displayed. In the UI, both V1 and V2 segments can be exported and as an audience for a Single Send or Automation.

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

Benefits of this new API

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:

  • Segments built over multiple engagement events,
  • Segments of contacts that have never been engaged.

Limitations of this new API

  • Not all of SQL is supported, meaning the Segmentation V2 API's query language is a SQL-compatible subset of SQL.
  • You must select 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.
  • None of the data modification constructs of SQL are supported.

Prerequisites

Before you can use the V2 Segmentation API you will need to do the following:

  1. Create or log into your SendGrid account.
  2. Navigate to Settings and create an API Key.
  3. Upload contacts using the UI or API.

Create a Segment

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

{
"name":"string (required)",
"query_dsl":"SQL string (required)",
"parent_list_ids":"array[string] (optional)"
}

Select all contacts

SELECT contact_id, updated_at FROM contact_data 

Select all contacts with an event_type of clicked

SELECT c.contact_id, c.updated_at 
FROM contact_data as c 
JOIN event_data as e ON c.contact_id= e.contact_id WHERE e.event_type = ‘clicked’

Rate this page:

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.

      
      
      

      Thank you for your feedback!

      We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

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

      Thanks for your feedback!

      Refer us and get $10 in 3 simple steps!

      Step 1

      Get link

      Get a free personal referral link here

      Step 2

      Give $10

      Your user signs up and upgrade using link

      Step 3

      Get $10

      1,250 free SMSes
      OR 1,000 free voice mins
      OR 12,000 chats
      OR more