Python helpers

Klaviyo helpers

Update a customer's Klaviyo subscriptions and record events from Python, with no API key in your code.

klaviyo_subscribe_to_list(context: dict, email: str, list_id: str) -> dict
klaviyo_suppress_profile(context: dict, email: str) -> dict
klaviyo_create_event(context: dict, email: str, event_name: str, properties: dict = {}) -> dict
klaviyo_get_lists(context: dict) -> dict

Helpers for the most common Klaviyo tasks in a support conversation. For example, when a customer asks to stop receiving marketing emails, an action can unsubscribe them on the spot. Each helper uses the Klaviyo connection of the conversation's business, so your code never holds the API key. Setup, key permissions and what the bot does on its own are on the Klaviyo integration page.

HelperWhat it doesKey scopes it needs
klaviyo_subscribe_to_list(context, email, list_id)Subscribes the email to marketing and adds it to the listLists, Profiles, Subscriptions: Write
klaviyo_suppress_profile(context, email)Unsubscribes the email from all marketing emailProfiles, Subscriptions: Write
klaviyo_create_event(context, email, event_name, properties)Records a custom event on the profile, for example to start a flowEvents: Write
klaviyo_get_lists(context)Returns the account's lists as {"lists": [{"id": ..., "name": ...}]}Lists: Read

The first three return {"success": True}.

Requirements and errors

  • The helpers read the business from context["conversation"]["businessSlug"], so they need a conversation in the context. Without one they raise ValueError.
  • They raise requests.HTTPError when Klaviyo isn't connected for the business or Klaviyo rejects the request, for example because the key lacks a scope.

For any other Klaviyo endpoint, read the key with get_integration_credentials and call the API directly, as described on the Klaviyo integration page.


Examples

Start a Klaviyo flow after a support conversation

def execute_action(context):
    email = context["args"]["email"]
    klaviyo_create_event(
        context,
        email,
        "Support Conversation Resolved",
        {"topic": context["args"].get("topic", ""), "conversationId": context["conversation"]["id"]},
    )
    return {"success": True}

Subscribe a customer to a specific list

VIP_LIST_ID = "XyZ123"  # from klaviyo_get_lists or the Klaviyo dashboard

def execute_action(context):
    klaviyo_subscribe_to_list(context, context["args"]["email"], VIP_LIST_ID)
    return {"success": True, "message": "The customer is subscribed to VIP updates."}

On this page