Impact data efficiently with APIs
Let's say you want to create a list in Klaviyo that contains customers who subscribed to receive newsletters about your Boston store. You know how to do this: you navigate to the Lists & segments tab in Klaviyo, then click Create New > Create list, where you enter your list name.
Now, imagine you have 100 stores around the country, and you get tasked with making individual lists for each store. That's a lot of clicking!
Alternatively, you could also do this with a series of API calls to Klaviyo's Create List endpoint. With some clever coding, APIs can greatly simplify the process of creating lists, and you can also use APIs to add profiles to this list, update the list, or ultimately delete the list.
Klaviyo APIs make it easy to manipulate every piece of data in your Klaviyo account.
The method of the API request provides information about how the request interacts with your Klaviyo data. POST requests generally create objects (like profiles and events) in your Klaviyo account, and GET requests retrieve information from Klaviyo. Other methods you may see in Klaviyo are PATCH requests, which update data, and DELETE requests, which delete data.
Want to see some of these requests in action? Check out our API tutorials YouTube playlist. The videos in this playlist walk through how to set up and make common API requests using Postman. Follow along to make your own API requests!
What data can we manipulate with APIs?
Flip through the tabs below to see some of the ways Klaviyo APIs can manipulate data in your Klaviyo account. Use these tabs to see how different parts of an API payload show up in Klaviyo. Later on in this lesson, you can access and copy the actual code used to make these requests.
Create Event
Create new events. You can completely customize the name of the event, timestamp, and the data that you send with this event.
Check out the image above for a visual of how the data in the API call maps to the Klaviyo UI.
Create event and update profile
In the same API request, you can create an event and simultaneously update a profile property. Just like when creating an event, you can customize all information about this event. At the same time, you can create and update custom profile properties on the profile that the event is logged to.
Check out how the data in the API payload maps to the Klaviyo UI. Code sample is provided in the dropdowns later in this lesson.
Create profile
You can use APIs to create new profiles. Make sure to include an email or phone number to identify this customer. Then, you can use this same identifier to later update or send events to this profile.
While creating a profile, you can also simultaneously add custom profile properties to this new profile.
Subscribe profile to email marketing
You can also use APIs to manage your profiles' email, and SMS consent statuses. In this example, see how an API call to the Bulk Subscribe Profiles endpoint allows you to subscribe a profile to marketing via API.
You can use this same endpoint to manage consent on multiple profiles in one call. You can manage email, SMS, and transactional SMS consent statuses for each profile.
Create list
This small but mighty payload allows you to create a new list.
Just specify the list name, and the Create List API will create a brand new list in your Klaviyo account. This API request will return your list ID, so you can refer to your list in other API requests.
JSON example code snippets
Click the arrow on each dropdown item below to view code snippets for common POST requests you might make to Klaviyo. The code snippets will constitute the body of the API request. You can copy the snippets into Postman, then customize to suit your use cases.
Create event
This is an example payload for an API request to our Create Event endpoint that creates an Ordered Product event.
{
"data": {
"type": "event",
"attributes": {
"properties": {
"product name": "silk pillow",
"color": "green"
},
"metric": {
"data": {
"type": "metric",
"attributes": {
"name": "Ordered Product"
}
}
},
"profile": {
"data": {
"type": "profile",
"attributes": {
"email": "michaela.klaviyo@gmail.com"
}
}
}
}
}
} This will send a corresponding event to the Klaviyo profile you specify.
Areas to customize:
- Properties: Update with the event data you want to include with the event. In this example, I included product name and color. Add as many event properties here as you would like.
- Name: Replaced Ordered Product with the name of your event.
- Email: Replace michaela.klaviyo@gmail.com with the email address of the profile you would like to send this event to. You can also use SMS or profile ID as an identifier instead.
Create event and update a profile property
Send this body to the Create Event endpoint to create an event and update a profile property at the same time:
{
"data": {
"type": "event",
"attributes": {
"properties": {
"product name": "silk pillow",
"color": "green"
},
"metric": {
"data": {
"type": "metric",
"attributes": {
"name": "Ordered Product"
}
}
},
"profile": {
"data": {
"type": "profile",
"attributes": {
"email": "michaela.klaviyo@gmail.com",
"properties": {
"last product ordered": "silk pillow"
}
}
}
}
}
}
} This both creates an event and updates a profile property on a single profile:
Areas to customize:
- Properties (within the event object): Update with the event data you want to include with the event. In this example, I included product name and color. Add as many event properties here as you would like.
- Name: Replaced Ordered Product with the name of your event.
- Email: Replace michaela.klaviyo@gmail.com with the email address of the profile you would like to send this event to. You can also use SMS or profile ID as an identifier instead.
- Properties (within the profile object): Replace "last product ordered": "silk pillow" with any number of custom profile properties you want to add to the profile.
Note: these are largely the same areas to edit as the first Create Event example, with the addition of updating the properties object in the profile object, which contains custom profile properties.
Create profile
Paste this code into the body of a POST request to the Create or Update Profile endpoint. This will create a new profile if there is no profile with the provided email address. If a profile with this email address already exists, it will be updated with the information provided here.
{
"data": {
"type": "profile",
"attributes": {
"email": "amanda@gmail.com",
"first_name": "Amanda",
"properties": {
"favorite color": "green"
}
}
}
} This will create a profile with the email address amanda@gmail.com, the first name Amanda, and a custom profile property favorite color with a value of green.
Areas to customize:
- Email: Replace amanda@gmail.com with the email address of the profile you want to create.
- First_name: Replace Amanda with the first name of the profile you want to create.
- Properties: replace "favorite color": "green" with the custom profile property you want to create.
Subscribe profile to email marketing
Paste this code into the body of a POST request to the Bulk Subscribe Profiles endpoint. This will subscribe a profile with this email address to email marketing and add it to the list you provide.
{
"data": {
"type": "profile-subscription-bulk-create-job",
"attributes": {
"profiles": {
"data": [
{
"type": "profile",
"attributes": {
"subscriptions": {
"email": {
"marketing": {
"consent": "SUBSCRIBED"
}
}
},
"email": "johndoe@gmail.com"
}
}
]
}
},
"relationships": {
"list": {
"data": {
"type": "list",
"id": "abc123"
}
}
}
}
} Areas to customize:
- Email: Replace johndoe@gmail.com with the email address of the profile you want to subscribe.
- Id: Replace abc123 with the ID of the list you want to subscribe the profile to.
Create list
Paste this code into the body of a POST request to the Create List endpoint. This is a POST request that will create a new list.
{
"data": {
"type": "list",
"attributes": {
"name": "Boston pop-up subscribers"
}
}
} Areas to customize:
- Name: Replace "Boston pop-up subscribers" with the name of the list you want to create
Bookmark this quick guide so you can frequently refer back to these code snippets!
For more information, check out these courses: