Python SDK
Synchronous and asynchronous Python SDK for the Synapse API. Published as pyrx-synapse on PyPI. Single dependency (httpx).
Requires Python 3.10+.
Installation
Quick Start
Synchronous
Asynchronous
Get your API key and workspace ID from the dashboard at Settings > API Keys.
Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
api_key | str | required | Your Synapse API key (psk_live_* or psk_test_*) |
workspace_id | str | required | Your workspace identifier |
base_url | str | https://synapse-api.pyrx.tech | API base URL |
timeout | float | 30.0 | Request timeout in seconds |
max_retries | int | 3 | Retry count for 429/5xx errors. Set to 0 to disable. |
Environment detection: The SDK detects test or live from your API key prefix (psk_test_* vs psk_live_*), available via client.environment.
Retry behavior: The SDK automatically retries on 429, 500, 502, 503, and 504 with exponential backoff and jitter (capped at 30s). On 429, uses the Retry-After header when present. Client errors (400, 401, 403, 404, 422) are never retried.
Track Events
Single Event
| Parameter | Type | Required | Description |
|---|---|---|---|
external_id | str | Yes | Your unique user identifier |
event_name | str | Yes | Event name (e.g., purchase_completed) |
attributes | dict | No | Arbitrary key-value event data |
contact | dict | No | Contact fields to upsert alongside the event |
idempotency_key | str | No | Prevents duplicate processing (7-day TTL) |
occurred_at | str | No | ISO 8601 timestamp. Defaults to server time. |
Batch Events
Track up to 50 events in a single request.
Identify Contacts
Single Contact
Create or update (upsert) a contact by external_id.
Batch Identify
Upsert up to 1,000 contacts in a single request.
Send Transactional Email
Send a one-off email using an NLT template, without a flow.
Requires a data-scoped API key (or higher). The template must exist in your workspace.
Contact Management
The client.contacts sub-client provides full CRUD operations. Requires a management or full scoped API key.
List Contacts
Get a Contact
Update a Contact
Delete a Contact
Template Management
The client.templates sub-client manages email templates. Requires a management or full scoped API key.
List Templates
Get a Template
Create a Template
Update a Template
Preview with Sample Data
Delete a Template
Error Handling
The SDK provides typed error classes for every failure mode.
Error Types
| Error Class | HTTP Status | Properties | When |
|---|---|---|---|
SynapseError | Any | status, message, code, request_id | Base class for all API errors |
SynapseAuthError | 401, 403 | message | Invalid or expired API key, scope mismatch |
SynapseValidationError | 422 | errors[] with field + message | Request body validation failed |
SynapseRateLimitError | 429 | retry_after (seconds) | Rate limit exceeded (auto-retried) |
SynapsePlanLimitError | 403 | limit_type, current, maximum, plan | Plan limit reached |
Environment Variables
For production deployments, load credentials from environment variables.
Full Method Reference
| Method | Description | Required Scope |
|---|---|---|
client.track(...) | Track a single event | data |
client.track_batch(...) | Track up to 50 events | data |
client.identify(...) | Upsert a single contact | data |
client.identify_batch(...) | Upsert up to 1,000 contacts | data |
client.send(...) | Send a transactional email | data |
client.contacts.list(...) | List contacts with pagination | management |
client.contacts.get(id) | Get a single contact | management |
client.contacts.update(id, data) | Update a contact | management |
client.contacts.delete(id) | Delete a contact | management |
client.templates.list() | List all templates | management |
client.templates.get(slug) | Get a template by slug | management |
client.templates.create(params) | Create a template | management |
client.templates.update(slug, params) | Update a template | management |
client.templates.preview(slug, data) | Preview rendered template | management |
client.templates.delete(slug) | Delete a template | management |
Framework Examples
FastAPI
Django
Flask
Context Managers
Both clients support context managers for automatic resource cleanup.
For long-lived processes (web servers), instantiate the client at module level and call client.close() (or await client.close()) on shutdown.
Related Resources
-
Node.js Server SDK -- server-side SDK for Node.js
-
Browser SDK -- client-side event tracking
-
Event Ingestion API -- raw REST API
-
API Reference -- interactive API explorer
-
Error Codes -- complete error code list