Stream Chat Python Client
The `stream-chat` library is the official Python API client for Stream Chat, a service designed for building chat applications. It provides server-side access to the chat API endpoints. While actively maintained with critical fixes and requested features, it is currently in maintenance mode. New projects are strongly encouraged to use the newer, full-product `getstream` SDK for comprehensive Stream services, including Chat, Video, Moderation, and Feeds. The library typically sees regular minor releases to introduce new features and bug fixes.
Common errors
-
StreamChatAPIException: Authentication Error: Invalid credentials provided for StreamChat.
cause The API Key or API Secret provided during `StreamChat` client initialization is incorrect or missing.fixDouble-check your `STREAM_API_KEY` and `STREAM_API_SECRET` in your environment variables or code. Ensure they match the credentials found in your Stream Dashboard (https://getstream.io/dashboard/). Verify there are no leading/trailing spaces or typos. -
StreamChatAPIException: GetOrCreateChannel failed with error: "either data.created_by or data.created_by_id must be provided when using server side auth."
cause When creating a channel using server-side authentication (i.e., with an API Secret), the `created_by` or `created_by_id` field is missing in the channel creation data.fixEnsure that the `create()` method for a channel is called with a `created_by_id` parameter, which specifies the user creating the channel. Example: `channel.create(created_by_id='your-user-id')`. -
StreamChatAPIException: Channel with given ID does not exist
cause An operation was attempted on a channel (e.g., `channel.watch()`, `channel.send_message()`) that has not yet been created or initialized on the Stream platform.fixBefore performing operations on a channel, ensure it has been properly created or retrieved. Call `channel.create(created_by_id='...')` to create the channel, or use `client.query_channels(...)` to retrieve existing ones.
Warnings
- breaking The `stream-chat` library is in maintenance mode. While it continues to receive critical updates, new projects are strongly advised to use the newer `getstream` Python SDK (`pip install getstream`). The `getstream` library is the actively developed, full-product SDK covering Chat, Video, Moderation, and Feeds.
- gotcha As of v4.0, API responses are instances of `StreamResponse`, which inherits from `dict`. While backward compatible (you can access data like a dictionary), it also provides additional methods like `.rate_limit()`, `.headers()`, and `.status_code()` for enhanced metadata access. Unexpected type checks might fail if only `dict` is assumed.
- gotcha Do not rely on parsing error messages for business logic. Error messages are subject to change and may vary. Always use HTTP status codes and Stream's internal error codes for robust error handling.
Install
-
pip install stream-chat
Imports
- StreamChat
from stream_chat import StreamChat
Quickstart
import os
from stream_chat import StreamChat
# Initialize the client with your API Key and Secret
# Get these from your Stream Dashboard: https://getstream.io/dashboard/
STREAM_API_KEY = os.environ.get('STREAM_API_KEY', 'YOUR_STREAM_API_KEY')
STREAM_API_SECRET = os.environ.get('STREAM_API_SECRET', 'YOUR_STREAM_API_SECRET')
STREAM_USER_ID = os.environ.get('STREAM_USER_ID', 'test_user_id')
if not all([STREAM_API_KEY, STREAM_API_SECRET]):
raise ValueError("STREAM_API_KEY and STREAM_API_SECRET environment variables must be set.")
client = StreamChat(api_key=STREAM_API_KEY, api_secret=STREAM_API_SECRET)
# Create/update a user
user_data = {"id": STREAM_USER_ID, "name": "Test User"}
client.upsert_user(user_data)
print(f"User '{STREAM_USER_ID}' upserted.")
# Create a user token for client-side authentication (e.g., in a frontend app)
token = client.create_token(STREAM_USER_ID)
print(f"Generated user token: {token}")
# Create a channel
channel_id = "general"
channel_type = "messaging"
channel = client.channel(channel_type, channel_id)
channel.create(STREAM_USER_ID)
print(f"Channel '{channel_type}:{channel_id}' created/initialized.")
# Send a message to the channel
message_data = {"text": "Hello from the Python client!"}
channel.send_message(message_data, STREAM_USER_ID)
print("Message sent!")