Skip to main content

Basic Usage (Python)

The OpenCDP Python SDK is built with asyncio for high performance and compatibility with modern web frameworks like FastAPI and Django (async).

Initialization​

Initialize the CDPClient once and share it across your application.

import asyncio
from cdp_client.client import CDPClient
from cdp_client.models import CDPConfig, CustomerIoConfig

async def main():
# 1. Configure the SDK
config = CDPConfig(
cdp_api_key="your_api_key_here",
# Optional: Dual-write to Customer.io
send_to_customer_io=True,
customer_io=CustomerIoConfig(
site_id="site_id",
api_key="api_key",
region="us"
),
debug=True
)

# 2. Create the client
client = CDPClient(config)

try:
# Use the client here...
pass
finally:
# 3. Always close the client to release resources
await client.close()

if __name__ == "__main__":
asyncio.run(main())

Identifying Users​

Use identify to create or update a user profile. Traits can include any standard or custom attributes.

async def identify_user(client: CDPClient):
user_id = "user_123"
traits = {
"email": "[email protected]",
"first_name": "Alex",
"plan": "premium",
"tags": ["beta_tester", "early_adopter"]
}

await client.identify(user_id, traits)

Tracking Events​

Use track to record user actions and their associated properties.

async def track_action(client: CDPClient):
user_id = "user_123"
properties = {
"item_id": "99",
"category": "electronics",
"source": "search_results"
}

await client.track(user_id, "item_viewed", properties)

Device Registration​

Register a device token for push notifications.

async def register_device(client: CDPClient):
await client.register_device(
user_id="user_123",
device_id="unique_uuid_for_device",
platform="android", # or 'ios', 'web'
token="device_token_from_fcm_or_apns",
attributes={"model": "Pixel 8"}
)