Installation
Copy
pip install transmit-sdk
Requirements
- Python 3.8 or higher
Quick Start
Copy
from transmit_sdk import TransmitClient
import os
client = TransmitClient(api_key=os.environ.get('TRANSMIT_API_KEY'))
# Send an email
response = client.emails.send({
'from': 'hello@yourdomain.com',
'to': 'user@example.com',
'subject': 'Welcome!',
'html': '<h1>Hello World</h1>'
})
print(f'Email sent: {response["id"]}')
Configuration
Initialize the Client
Copy
from transmit_sdk import TransmitClient
client = TransmitClient(
api_key='tx_your_api_key_here',
base_url='https://api.transmit.dev', # optional
timeout=30 # optional, in seconds
)
Environment Variables
Copy
import os
from transmit_sdk import TransmitClient
# Load API key from environment
client = TransmitClient(api_key=os.environ.get('TRANSMIT_API_KEY'))
Sending Emails
Basic Email
Copy
response = client.emails.send({
'from': 'hello@yourdomain.com',
'to': 'user@example.com',
'subject': 'Hello',
'html': '<p>Welcome!</p>'
})
Multiple Recipients
Copy
response = client.emails.send({
'from': 'hello@yourdomain.com',
'to': ['user1@example.com', 'user2@example.com'],
'cc': ['manager@example.com'],
'bcc': ['admin@example.com'],
'subject': 'Team Update',
'html': '<p>Important update</p>'
})
With Attachments
Copy
with open('./invoice.pdf', 'rb') as f:
file_content = f.read()
response = client.emails.send({
'from': 'hello@yourdomain.com',
'to': 'user@example.com',
'subject': 'Invoice',
'html': '<p>Your invoice is attached</p>',
'attachments': [
{
'filename': 'invoice.pdf',
'content': file_content,
'content_type': 'application/pdf'
}
]
})
Using Templates
Copy
response = client.emails.send({
'from': 'hello@yourdomain.com',
'to': 'user@example.com',
'template_id': 'welcome-email',
'variables': {
'name': 'John Doe',
'confirm_url': 'https://example.com/confirm'
}
})
Async Support
The SDK supports async/await for asynchronous operations:Copy
import asyncio
from transmit_sdk import AsyncTransmitClient
async def send_email():
client = AsyncTransmitClient(api_key='tx_your_api_key_here')
response = await client.emails.send({
'from': 'hello@yourdomain.com',
'to': 'user@example.com',
'subject': 'Hello',
'html': '<p>Welcome!</p>'
})
print(f'Email sent: {response["id"]}')
# Run the async function
asyncio.run(send_email())
Error Handling
Copy
from transmit_sdk import TransmitClient, TransmitError
client = TransmitClient(api_key='tx_your_api_key_here')
try:
response = client.emails.send({
'from': 'hello@yourdomain.com',
'to': 'invalid-email',
'subject': 'Test',
'html': '<p>Test</p>'
})
except TransmitError as e:
print(f'API Error: {e.code} - {e.message}')
print(f'Status: {e.status_code}')
except Exception as e:
print(f'Unexpected error: {e}')
Type Hints
The SDK includes type hints for better IDE support:Copy
from transmit_sdk import TransmitClient
from transmit_sdk.types import Email, EmailResponse
client = TransmitClient(api_key='tx_your_api_key_here')
email_data: Email = {
'from': 'hello@yourdomain.com',
'to': 'user@example.com',
'subject': 'Test',
'html': '<p>Test</p>'
}
response: EmailResponse = client.emails.send(email_data)
Retrieving Emails
Copy
# Get a single email by ID
email = client.emails.get('email_abc123')
# List emails with pagination
emails = client.emails.list(page=1, limit=50, status='delivered')
print(emails['data'])
print(emails['pagination'])
Managing Contacts
Copy
# Add a contact
contact = client.contacts.create({
'email': 'user@example.com',
'first_name': 'John',
'last_name': 'Doe',
'tags': ['customer', 'pro-plan']
})
# List contacts
contacts = client.contacts.list(page=1, limit=50)
# Update a contact
client.contacts.update('contact_abc123', {
'tags': ['customer', 'enterprise-plan']
})
# Delete a contact
client.contacts.delete('contact_abc123')
Templates
Copy
# Create a template
template = client.templates.create({
'name': 'Welcome Email',
'subject': 'Welcome {{name}}!',
'html': '<h1>Hello {{name}}</h1>'
})
# List templates
templates = client.templates.list()
# Get a template
template = client.templates.get('template_abc123')
# Update a template
client.templates.update('template_abc123', {
'html': '<h1>Updated template</h1>'
})
# Delete a template
client.templates.delete('template_abc123')
Context Manager
Use the SDK as a context manager:Copy
from transmit_sdk import TransmitClient
with TransmitClient(api_key='tx_your_api_key_here') as client:
response = client.emails.send({
'from': 'hello@yourdomain.com',
'to': 'user@example.com',
'subject': 'Test',
'html': '<p>Test</p>'
})
print(f'Email sent: {response["id"]}')
# Client automatically closed
GitHub Repository
View on GitHub
Browse the source code, report issues, and contribute