Skip to main content

    Fundamentals of building with Klaviyo

    Course overview
    Lesson
    4 min read

    Understand how to use Klaviyo's APIs

    Learn the difference between Klaviyo's client-side and server-side APIs, and identify useful features that will help you build more efficiently.

    Use Klaviyo APIs to integrate and automate

    Klaviyo’s APIs allow you to build highly personalized messages and experiences at scale. They make it possible to integrate Klaviyo with virtually any other system or application, automate marketing workflows, and perform a wide range of tasks, including adding and updating contacts, creating and sending emails, and retrieving data. In other words, you can think of these APIs as the tools that allow you to interact with the 3 primary objects in Klaviyo: profiles, events, and catalogs.

    Differentiate between client-side and server-side

    Klaviyo's APIs can be split into 2 broad categories: client-side and server-side. The main difference between them is where the API requests should be initiated and processed. Client-side API calls only require a public API key to authenticate, but all server-side API calls require a private API key. Let’s explore the difference a bit further.

    Client-side APIs

    Client-side APIs are built to make requests directly from a user's browser or device, and the requests are processed on the client-side. These APIs are used to capture user behavior (e.g., clicks, form submissions, product views, etc.) on a publicly available website or app. They are implemented using JavaScript and can trigger events or create and update profiles in Klaviyo.

    Example cURL Create Client Subscription API request

    Server-side APIs

    Server-side APIs make requests from a server, and the requests are processed on the server-side. These APIs are used to automate marketing workflows and perform tasks like importing and exporting data, sending emails, and retrieving analytics data. They can also be used to build custom integrations from other applications to Klaviyo.

    These APIs are language-agnostic, meaning you can use a variety of programming languages to access them. Klaviyo offers software development kits (SDKs) in popular programming languages like Python, Ruby, PHP, and JavaScript, so you can build in whichever language works best for you. All server-side calls, which are the majority of Klaviyo APIs, will require a private API key in order to authenticate.

    Example cURL Get Segments API request

    Install onsite tracking to use client-side APIs

    A core capability of Klaviyo is tracking onsite activity at the profile level. By tracking these events, you can trigger other actions to happen based on specific user activity. Klaviyo offers a variety of pre-built integrations with popular ecommerce platforms to make it easy for businesses to implement onsite tracking automatically. When you build a custom integration with Klaviyo, you have to install this code yourself. The primary code snippet that will allow you to track onsite activity is known as klaviyo.js. Klaviyo.js forms the connection between your site and a specific Klaviyo account using the account ID (i.e., the public API key) of the account.

    To make development simpler and faster, use the klaviyo object, which is supported by klaviyo.js. This object offers a short-hand way to send events into Klaviyo and interact with client-side APIs. The klaviyo object removes some of the manual work that is required when using the client-side APIs.

    Understand the features of Klaviyo's JSON APIs

    Filters

    The ?filter query parameter can be used to filter almost all Klaviyo APIs. Filter data using common operations such as “equals”, “contains”, “starts-with”, and more.

    Resource relationships

    The relationships object allows you to query specific groups of related resources efficiently, avoiding redundant queries. A common relationship example is profiles and lists, where a single profile can be associated with multiple lists. Relationships can be one-to-one or one-to-many.

    Sparse fields

    Several API endpoints support sparse fieldsets, which allow you to retrieve specific fields of resources rather than retrieving entire resources. Resources can be very large, so sparse fieldsets can reduce the size of data being passed.

    Acceptable payloads

    For all APIs, there are a few common elements that should be included in the payload. The payload should include:

    • Required fields indicated in the specific endpoint documentation.
    • Object structure that is specific to the endpoint.
    • Data that is structured or nested properly for segmentation.
    • Proper JSON formatting with each key-value pair separated by a comma, and enclosed in curly braces.

    For more details, see our developer documentation on JSON:API features.

    Understand how to use Klaviyo's APIs