Hi! 😊
Customer.io is a customer engagement platform that helps businesses send targeted emails, push notifications, and in-app messages. It's particularly popular for its behavioral targeting capabilities — meaning you can trigger messages based on user actions or data.
The Customer.io API allows developers to integrate Customer.io into their applications to:
- Track customer behavior
- Trigger transactional or behavioral emails
- Manage customer data
- Create and manage segments
- Sync users or delete them
🔧 Getting Started with the Customer.io API
There are two main APIs you might use depending on your needs:
1. Track API (for events and identifying people)
2. App API (for managing resources like customers, segments, campaigns)
You'll need an API key, which you can find under:
Settings > Integrations > API
There are different keys for:
- Track API Key (used to track events)
- App API Key (used for managing resources)
🐍 Using Python + requests to Interact with Customer.io
Here’s how you can interact with the Track API using Python and the requests library.
✅ Example 1: Identify a Customer
import requests
import base64
import json
TRACK_API_KEY = "YOUR_TRACK_API_KEY"
TRACK_API_SITE_ID = "YOUR_SITE_ID"
url = f"https://track.customer.io/api/v1/customers/{CUSTOMER_ID}"
# Prepare Basic Auth header
auth_header = base64.b64encode(f"{TRACK_API_SITE_ID}:{TRACK_API_KEY}".encode()).decode()
headers = {
"Authorization": f"Basic {auth_header}",
"Content-Type": "application/json"
}
data = {
"email": "user@example.com",
"name": "John Doe",
"created_at": 1337,
# You can add custom attributes here
}
response = requests.put(url, headers=headers, data=json.dumps(data))
print(response.status_code)
print(response.json())
This will identify or update a customer profile in Customer.io.
✅ Example 2: Track an Event
url = f"https://track.customer.io/api/v1/customers/{CUSTOMER_ID}/events"
event_data = {
"name": "purchased",
"timestamp": int(time.time()),
"type": "event",
"data": {
"product_id": "12345",
"price": 19.99
}
}
response = requests.post(url, headers=headers, data=json.dumps(event_data))
print(response.status_code)
print(response.json())
This triggers an event that can be used to power workflows or segment users.
✅ Example 3: Send a Triggered Broadcast (via App API)
If you want to manually trigger a campaign or broadcast, you can use the App API.
APP_API_KEY = "YOUR_APP_API_KEY"
url = "https://api.customer.io/v1/broadcast/YOUR_BROADCAST_ID/send"
headers = {
"Authorization": f"Bearer {APP_API_KEY}",
"Content-Type": "application/json"
}
payload = {
"recipient_event": {
"name": "My Event",
"data": {
"email": "user@example.com",
"custom_attribute": "value"
}
}
}
response = requests.post(url, headers=headers, json=payload)
print(response.status_code)
print(response.json())
Make sure the broadcast is set up as a triggered send so it can be triggered via the API.
📌 Tips & Best Practices
- Always handle errors (
response.status_code) to ensure successful delivery.
- Use environment variables to store your API keys securely.
- For high-volume tracking, consider batching events or using async HTTP clients like
aiohttp.
🧠 When Should You Use Each API?
| Task | API |
|---|
| Identify customers, track events | Track API |
| Sync user data, delete users | Track API |
| Trigger broadcasts, manage segments, get stats | App API |
Let me know what specific use case you have in mind — I can help tailor the example!