Customer.io Python Client

2.3.0 · active · verified Sat Apr 11

The official Python client library for Customer.io, providing bindings to their Track API (for user identification and event tracking) and App API (for transactional emails, push notifications, SMS, and inbox messages). Currently at version 2.3.0, it sees regular minor updates to support new Customer.io API features.

Warnings

Install

Imports

Quickstart

Initialize the `Client` with your Customer.io credentials (Site ID, API Key, and optionally App Key for transactional messages) and perform common operations like identifying users, tracking events, and sending transactional emails.

import os
from customerio import Client

# It's recommended to load credentials from environment variables
site_id = os.environ.get('CUSTOMERIO_SITE_ID', 'YOUR_SITE_ID')
api_key = os.environ.get('CUSTOMERIO_API_KEY', 'YOUR_API_KEY')
app_key = os.environ.get('CUSTOMERIO_APP_KEY', 'YOUR_APP_KEY') # For Transactional API

# Initialize the client. A single client can handle both Track and App APIs.
cio = Client(site_id=site_id, api_key=api_key, app_key=app_key)

# 1. Identify a user (Track API)
try:
    response = cio.identify(
        customer_id="customer_123",
        email="user@example.com",
        first_name="John",
        last_name="Doe",
        created_at=1678886400 # Unix timestamp (e.g., for March 15, 2023)
    )
    print(f"Identify response: {response.status_code} {response.text}")
except Exception as e:
    print(f"Error identifying user: {e}")

# 2. Track an event for a user (Track API)
try:
    response = cio.track(
        customer_id="customer_123",
        name="product_purchased",
        data={
            "product_id": "SKU456",
            "price": 99.99,
            "currency": "USD"
        }
    )
    print(f"Track event response: {response.status_code} {response.text}")
except Exception as e:
    print(f"Error tracking event: {e}")

# 3. Send a transactional email (App API, requires app_key)
# Ensure 'transactional_message_id' corresponds to an existing template in Customer.io
if app_key != 'YOUR_APP_KEY': # Check if app_key was actually provided from env
    try:
        response = cio.send_email(
            to="user@example.com",
            transactional_message_id=123, # Replace with your actual transactional email ID
            message_data={
                "name": "John Doe",
                "item": "Widget"
            }
        )
        print(f"Send email response: {response.status_code} {response.text}")
    except Exception as e:
        print(f"Error sending email: {e}")
else:
    print("Skipping transactional email example: CUSTOMERIO_APP_KEY not configured.")

print("Customer.io operations complete (check console for API responses).")

view raw JSON →