Python SDK for Chatwoot API
Project description
Chatwoot SDK for Python
A modern, type-safe Python SDK for the Chatwoot API. This SDK provides both synchronous and asynchronous interfaces to interact with Chatwoot's REST API, with full type hints for better IDE support and code safety.
Features
- ๐ Type-safe: Full type hints with Pydantic models
- โก Async support: Both sync and async clients available
- ๐ฏ Comprehensive: Covers all major Chatwoot API resources
- ๐ก๏ธ Error handling: Custom exceptions for different error types
Installation
pip install chatwoot-sdk
# or
uv add chatwoot-sdk
Note: The PyPI package name is
chatwoot-sdk. The GitHub repository is namedchatwoot-sdk-python.
Quick Start
from chatwoot import ChatwootClient
# Initialize the client
client = ChatwootClient(
base_url="https://app.chatwoot.com",
api_token="your_api_token_here"
)
# Fetch your profile
profile = client.profile.get()
print(f"Logged in as: {profile.name}")
# List open conversations
conversations = client.conversations.list(
account_id=1,
status="open"
)
# Send a message
message = client.messages.create(
account_id=1,
conversation_id=42,
content="Hello from Python SDK!"
)
# Always close the client when done
client.close()
Usage Examples
Using Context Manager
from chatwoot import ChatwootClient
with ChatwootClient(base_url="...", api_token="...") as client:
profile = client.profile.get()
conversations = client.conversations.list(account_id=1, status="open")
# Client is automatically closed
Async Usage
import asyncio
from chatwoot import AsyncChatwootClient
async def main():
async with AsyncChatwootClient(base_url="...", api_token="...") as client:
profile = await client.profile.get()
conversations = await client.conversations.list(
account_id=1,
status="open"
)
asyncio.run(main())
Working with Conversations
# List conversations with filters
conversations = client.conversations.list(
account_id=1,
status="open",
assignee_type="me",
inbox_id=5,
labels=["urgent", "billing"]
)
# Get conversation details
conversation = client.conversations.get(account_id=1, conversation_id=42)
# Update conversation
updated = client.conversations.update(
account_id=1,
conversation_id=42,
status="resolved",
assignee_id=10
)
# Add labels to conversation
labels = client.conversations.labels.add(
account_id=1,
conversation_id=42,
labels=["bug", "priority"]
)
Managing Contacts
# Create a contact
contact = client.contacts.create(
account_id=1,
inbox_id=5,
name="John Doe",
email="john@example.com",
phone_number="+1234567890"
)
# Search contacts
results = client.contacts.search(account_id=1, query="john@example.com")
# Update contact
contact = client.contacts.update(
account_id=1,
contact_id=100,
name="Jane Doe",
custom_attributes={"plan": "premium"}
)
# Add labels to contact
labels = client.contacts.labels.add(
account_id=1,
contact_id=100,
labels=["vip", "premium"]
)
Sending Messages
# Send a text message
message = client.messages.create(
account_id=1,
conversation_id=42,
content="Hello! How can I help you?",
message_type="outgoing"
)
# Send a private note
note = client.messages.create(
account_id=1,
conversation_id=42,
content="Internal note for team",
message_type="outgoing",
private=True
)
# Update a message
updated = client.messages.update(
account_id=1,
message_id=123,
content="Updated message content"
)
Managing Teams
# List teams
teams = client.teams.list(account_id=1)
# Create a team
team = client.teams.create(
account_id=1,
name="Support Team",
description="Customer support team"
)
# Add agents to team
client.teams.agents.add(
account_id=1,
team_id=5,
agent_ids=[10, 11, 12]
)
# List team members
members = client.teams.agents.list(account_id=1, team_id=5)
Error Handling
from chatwoot import (
ChatwootClient,
ChatwootAuthError,
ChatwootNotFoundError,
ChatwootValidationError,
)
client = ChatwootClient(base_url="...", api_token="...")
try:
conversation = client.conversations.get(account_id=1, conversation_id=999)
except ChatwootAuthError as e:
print(f"Authentication failed: {e}")
except ChatwootNotFoundError as e:
print(f"Resource not found: {e}")
except ChatwootValidationError as e:
print(f"Validation error: {e}")
if e.errors:
for error in e.errors:
print(f" - {error['field']}: {error['message']}")
API Resources
The SDK provides access to the following Chatwoot API resources:
- Profile: User profile information
- Conversations: Conversation management and filtering
- Messages: Send and manage messages
- Contacts: Contact management and search
- Inboxes: Inbox configuration
- Teams: Team management
- Agents: Agent management
- Labels: Label management (nested under conversations and contacts)
Development
Setup
# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh
# Install dependencies
uv sync
# Run tests
uv run pytest
# Run tests with coverage
uv run pytest --cov=chatwoot --cov-report=term-missing
# Type check
uvx ty check .
# Lint
uv run ruff check .
# Format code
uv run ruff format .
Project Structure
chatwoot-sdk-python/
โโโ chatwoot/
โ โโโ __init__.py # Public API
โ โโโ client.py # Main client classes
โ โโโ exceptions.py # Custom exceptions
โ โโโ _http.py # HTTP client wrapper
โ โโโ types/ # Pydantic models
โ โ โโโ common.py
โ โ โโโ conversation.py
โ โ โโโ message.py
โ โ โโโ contact.py
โ โ โโโ ...
โ โโโ resources/ # API resource implementations
โ โโโ _base.py
โ โโโ profile.py
โ โโโ conversations.py
โ โโโ messages.py
โ โโโ ...
โโโ tests/
โ โโโ unit/
โโโ examples/
โโโ README.md
Requirements
- Python 3.11+
- httpx
- pydantic
License
MIT License - see LICENSE file for details
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chatwoot_sdk-0.2.0.tar.gz.
File metadata
- Download URL: chatwoot_sdk-0.2.0.tar.gz
- Upload date:
- Size: 68.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3db1f66c359f7a10057b22aaf8a6bccfebd4243b810f4f5d3f793402f5f2e45
|
|
| MD5 |
a25b3f73af05bce3ef08842467057feb
|
|
| BLAKE2b-256 |
de235d139a41318a69ea8f7e9085b5f65dcba645304b8cb5c2e772b0f1aac618
|
Provenance
The following attestation bundles were made for chatwoot_sdk-0.2.0.tar.gz:
Publisher:
publish.yml on chatwoot/chatwoot-sdk-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chatwoot_sdk-0.2.0.tar.gz -
Subject digest:
e3db1f66c359f7a10057b22aaf8a6bccfebd4243b810f4f5d3f793402f5f2e45 - Sigstore transparency entry: 968928111
- Sigstore integration time:
-
Permalink:
chatwoot/chatwoot-sdk-python@d8baa84cf4c97fd0e283fbae5fe6360a7cc11a91 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/chatwoot
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d8baa84cf4c97fd0e283fbae5fe6360a7cc11a91 -
Trigger Event:
release
-
Statement type:
File details
Details for the file chatwoot_sdk-0.2.0-py3-none-any.whl.
File metadata
- Download URL: chatwoot_sdk-0.2.0-py3-none-any.whl
- Upload date:
- Size: 26.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03e09a18192b8487b3a16655a72aa01c56a7682138d53769e4877d53001e2f5f
|
|
| MD5 |
93ce3a31b03280069edea228e6281b72
|
|
| BLAKE2b-256 |
4cf97c49b73678ae64df8569af172a2cfd932e7d80baaf628c74f0235d0db9b8
|
Provenance
The following attestation bundles were made for chatwoot_sdk-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on chatwoot/chatwoot-sdk-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chatwoot_sdk-0.2.0-py3-none-any.whl -
Subject digest:
03e09a18192b8487b3a16655a72aa01c56a7682138d53769e4877d53001e2f5f - Sigstore transparency entry: 968928148
- Sigstore integration time:
-
Permalink:
chatwoot/chatwoot-sdk-python@d8baa84cf4c97fd0e283fbae5fe6360a7cc11a91 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/chatwoot
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d8baa84cf4c97fd0e283fbae5fe6360a7cc11a91 -
Trigger Event:
release
-
Statement type: