{"id":8258,"library":"klaviyo-api","title":"Klaviyo Python SDK","description":"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.","status":"active","version":"23.0.0","language":"en","source_language":"en","source_url":"https://github.com/klaviyo/klaviyo-api-python","tags":["marketing","email","automation","CRM","ecommerce"],"install":[{"cmd":"pip install klaviyo-api","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"KlaviyoAPI","correct":"from klaviyo_api import KlaviyoAPI"}],"quickstart":{"code":"import os\nfrom klaviyo_api import KlaviyoAPI\nfrom klaviyo_api.exceptions import KlaviyoApiException\n\n# Retrieve your Klaviyo Private API Key from environment variables.\n# You can find your API keys in Klaviyo Settings -> API Keys.\n# It should start with 'pk_'.\nKLAVIYO_PRIVATE_API_KEY = os.environ.get('KLAVIYO_PRIVATE_API_KEY', '')\n\nif not KLAVIYO_PRIVATE_API_KEY:\n    print(\"Error: KLAVIYO_PRIVATE_API_KEY environment variable not set. Please set it to your Klaviyo Private API Key.\")\n    exit(1)\n\ntry:\n    # Initialize the Klaviyo API client\n    klaviyo = KlaviyoAPI(api_key=KLAVIYO_PRIVATE_API_KEY)\n\n    # Example: Get account information (requires 'Accounts' scope)\n    # Make sure your API key has the necessary permissions.\n    response = klaviyo.Accounts.get_accounts()\n\n    if response.status_code == 200:\n        print(\"Successfully connected to Klaviyo API.\")\n        print(f\"Account ID: {response.body['data'][0]['id']}\")\n        print(\"First Account Name: \", response.body['data'][0]['attributes']['name'])\n    else:\n        print(f\"Error connecting to Klaviyo API: {response.status_code} - {response.body}\")\n\nexcept KlaviyoApiException as e:\n    print(f\"Klaviyo API Error: {e.status_code} - {e.body}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"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."},"warnings":[{"fix":"Remove `anonymous_id` from any profile creation or update payloads. Use other identifiers like `email`, `phone_number`, or `external_id`.","message":"The `anonymous_id` field was removed from profile payloads in version `22.0.0`. If you were previously including `anonymous_id` when creating or updating profiles, your requests will now fail or behave unexpectedly.","severity":"breaking","affected_versions":">=22.0.0"},{"fix":"Always use a Private API Key for server-side SDK interactions. Verify that the generated Private API Key has all the necessary scopes (e.g., 'Profiles' write, 'Events' write, etc.) for the API calls you intend to make.","message":"Klaviyo API keys have different types (Public, Private, Client-side) and specific scopes (permissions). Using the wrong type of key or a key without sufficient permissions for an endpoint will result in `401 Unauthorized` or `403 Forbidden` errors.","severity":"gotcha","affected_versions":"All"},{"fix":"Implement exponential backoff and retry logic for API calls. Consult the Klaviyo API documentation for specific rate limits per endpoint. Distribute requests over time or use bulk endpoints where available.","message":"Klaviyo APIs are subject to rate limits. Exceeding these limits will result in `429 Too Many Requests` errors. Specific endpoints might have different tiers (e.g., Conversations API uses the 'SMALL' tier: 3 requests/second burst, 60 requests/minute steady).","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Double-check your `KLAVIYO_PRIVATE_API_KEY` value. Ensure it's a Private API Key, not a Public API Key. Go to Klaviyo Settings -> API Keys to generate a new key with appropriate scopes for your needs.","cause":"The API key provided is either incorrect, invalid, or does not have the necessary permissions (scopes) for the requested operation.","error":"klaviyo_api.exceptions.KlaviyoApiException: (401) Unauthorized"},{"fix":"Before sending data to Klaviyo API, ensure all values in your payload are JSON serializable. Convert `UUID` objects to strings (`str(my_uuid_object)`) and `datetime` objects to ISO 8601 strings.","cause":"Some data types, like `UUID` objects, are not directly JSON serializable, but the SDK expects JSON-compatible values for request bodies. This commonly occurs when passing Python objects directly without conversion.","error":"TypeError: Object of type 'UUID' is not JSON serializable"}]}