Getting started overview

Integrating with the Klaviyo API allows developers to automate marketing workflows, synchronize customer data, and extend Klaviyo's platform capabilities programmatically. This guide focuses on the initial steps to get an integration operational, covering account setup, API key generation, and executing a basic API call. The Klaviyo API supports various operations for managing profiles, events, campaigns, and more, as detailed in the Klaviyo API reference documentation.

The core process involves:

  1. Account Creation: Setting up a Klaviyo account.
  2. API Key Generation: Obtaining the necessary credentials for authentication.
  3. First Request: Making a simple API call to verify connectivity and authentication.

A quick reference for these steps is provided below:

Step What to Do Where
1. Sign Up Create a new Klaviyo account. Klaviyo homepage
2. Get API Keys Generate Private API Keys in settings. Klaviyo Dashboard > Account > Settings > API Keys
3. Make Request Send a basic API call (e.g., retrieving profiles). Using a tool like cURL, Postman, or a language-specific SDK

Create an account and get keys

Before making any API requests, a Klaviyo account is required. Klaviyo offers a free tier that supports up to 500 contacts and 1,000 email sends per month, which is suitable for initial development and testing. To create an account, navigate to the Klaviyo website and follow the signup process.

Generating API Keys

Access to the Klaviyo API is secured using API keys. Klaviyo utilizes private API keys for most server-side integrations, which should be kept confidential. Public API keys are also available for client-side use cases, such as tracking website activity, but are not typically used for direct API calls that modify data or require sensitive access.

To generate a private API key:

  1. Log in to your Klaviyo account.
  2. Click on your organization name in the bottom left corner and select Account.
  3. Navigate to Settings, then select API Keys.
  4. Click Create Private API Key.
  5. Provide a descriptive name for the key (e.g., "My Integration API Key").
  6. Optionally, restrict access to specific API scopes to follow the principle of least privilege.
  7. Copy the generated API key. This key will be displayed only once. Store it securely.

The API key functions as a bearer token for authentication. For example, when making a request, it will be included in the Authorization header as Bearer YOUR_PRIVATE_API_KEY.

Your first request

Once an API key is obtained, you can make your first request to verify connectivity and authentication. A common starting point is to retrieve a list of profiles. This example uses the /api/profiles/ endpoint to fetch customer profile data.

The Klaviyo API follows a RESTful architecture and uses JSON for request and response bodies. All API endpoints are prefixed with https://a.klaviyo.com/api/.

Example: Get Profiles

This example demonstrates how to fetch a list of customer profiles using cURL, a common command-line tool for making HTTP requests:

curl --request GET \
  --url 'https://a.klaviyo.com/api/profiles/' \
  --header 'Accept: application/json' \
  --header 'Authorization: Bearer YOUR_PRIVATE_API_KEY'

Replace YOUR_PRIVATE_API_KEY with the actual key generated in the previous step.

A successful response will return a JSON object containing an array of profile data, similar to this (truncated for brevity):

{
  "data": [
    {
      "type": "profile",
      "id": "01ARZ3NDEKTSV4RRQ1SFMTK0B9",
      "attributes": {
        "email": "[email protected]",
        "phone_number": null,
        "external_id": null,
        "anonymous_id": null,
        "first_name": "John",
        "last_name": "Doe",
        "organization": null,
        "title": null,
        "image": null,
        "created": "2023-01-01T12:00:00+00:00",
        "updated": "2023-01-01T12:00:00+00:00",
        "last_event_time": null,
        "subscriptions": {
          "email": {
            "marketing": {
              "can_receive_email": true,
              "is_consented": true
            }
          }
        },
        "location": {
          "address1": null,
          "address2": null,
          "city": null,
          "country": null,
          "region": null,
          "zip": null,
          "latitude": null,
          "longitude": null
        },
        "properties": {}
      },
      "links": {
        "self": "https://a.klaviyo.com/api/profiles/01ARZ3NDEKTSV4RRQ1SFMTK0B9/"
      }
    }
  ],
  "links": {
    "self": "https://a.klaviyo.com/api/profiles/"
  }
}

Using SDKs

Klaviyo provides official SDKs for Python, Node.js, and Ruby, which abstract away the HTTP request details and provide language-specific methods for interacting with the API. Using an SDK can simplify development and reduce boilerplate code.

For example, using the Node.js SDK:

const { ApiClient } = require('klaviyo-api');

const client = new ApiClient({
  apiKey: 'YOUR_PRIVATE_API_KEY',
});

async function getProfiles() {
  try {
    const profiles = await client.profiles.getProfiles();
    console.log(profiles.body);
  } catch (error) {
    console.error('Error fetching profiles:', error);
  }
}

getProfiles();

Refer to the Klaviyo developer documentation for detailed SDK usage and installation instructions.

Common next steps

After successfully making your first API call, consider these next steps to deepen your integration:

  • Explore Endpoints: Review the Klaviyo API reference to understand the available endpoints for managing profiles, events, campaigns, and more.
  • Implement Webhooks: Set up webhooks to receive real-time notifications about events in Klaviyo, such as profile changes or email opens. This can reduce polling and enable event-driven architectures. Twilio provides a webhook security guide that is relevant for general webhook implementation best practices.
  • Handle Rate Limits: Be aware of Klaviyo's API rate limits to prevent your application from being throttled. Implement retry logic with exponential backoff for robust integrations.
  • Error Handling: Implement comprehensive error handling to gracefully manage API responses that indicate errors, such as invalid authentication or malformed requests.
  • Data Synchronization: Begin synchronizing customer data, such as creating new profiles or updating existing ones with custom properties.
  • Event Tracking: Send custom events to Klaviyo to track user behavior and trigger automated flows.

Troubleshooting the first call

Encountering issues during the first API call is common. Here are some troubleshooting tips:

  • Incorrect API Key: Double-check that the API key in your request matches the private API key generated in your Klaviyo account. Ensure there are no leading or trailing spaces.
  • Authorization Header Format: Verify that the Authorization header is correctly formatted as Bearer YOUR_PRIVATE_API_KEY.
  • Endpoint URL: Confirm that the base URL and endpoint path are correct (e.g., https://a.klaviyo.com/api/profiles/).
  • Missing Accept Header: The Klaviyo API expects an Accept: application/json header for most requests.
  • Network Connectivity: Ensure your development environment has unrestricted outbound access to a.klaviyo.com.
  • Rate Limiting: While less likely on a first call, repeated failed attempts can trigger temporary rate limits. Review Klaviyo's rate limit documentation if experiencing consistent connection issues after multiple attempts.
  • API Key Scopes: If you restricted the API key's scopes, ensure the key has the necessary permissions for the endpoint you are trying to access (e.g., profiles:read for retrieving profiles).
  • Error Messages: Pay close attention to the error messages returned in the API response. These often provide specific details about what went wrong. For example, a 401 Unauthorized typically indicates an authentication issue, while a 403 Forbidden might point to incorrect API key scopes.