Klaviyo Python SDK

23.0.0 · active · verified Thu Apr 16

The Klaviyo Python SDK provides a convenient way to interact with the Klaviyo API for marketing automation, customer data management, and more. It offers full coverage of the Klaviyo API, including profiles, events, campaigns, flows, and more. The library is actively maintained with monthly releases, often aligned with new Klaviyo API revisions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Klaviyo API client using a private API key from an environment variable and make a simple request to retrieve account information. Ensure your API key has the necessary scopes (e.g., 'Accounts' read scope) for the endpoints you wish to use.

import os
from klaviyo_api import KlaviyoAPI
from klaviyo_api.exceptions import KlaviyoApiException

# Retrieve your Klaviyo Private API Key from environment variables.
# You can find your API keys in Klaviyo Settings -> API Keys.
# It should start with 'pk_'.
KLAVIYO_PRIVATE_API_KEY = os.environ.get('KLAVIYO_PRIVATE_API_KEY', '')

if not KLAVIYO_PRIVATE_API_KEY:
    print("Error: KLAVIYO_PRIVATE_API_KEY environment variable not set. Please set it to your Klaviyo Private API Key.")
    exit(1)

try:
    # Initialize the Klaviyo API client
    klaviyo = KlaviyoAPI(api_key=KLAVIYO_PRIVATE_API_KEY)

    # Example: Get account information (requires 'Accounts' scope)
    # Make sure your API key has the necessary permissions.
    response = klaviyo.Accounts.get_accounts()

    if response.status_code == 200:
        print("Successfully connected to Klaviyo API.")
        print(f"Account ID: {response.body['data'][0]['id']}")
        print("First Account Name: ", response.body['data'][0]['attributes']['name'])
    else:
        print(f"Error connecting to Klaviyo API: {response.status_code} - {response.body}")

except KlaviyoApiException as e:
    print(f"Klaviyo API Error: {e.status_code} - {e.body}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →