Stream Chat Python Client

4.31.0 · maintenance · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

Initializes the Stream Chat client, upserts a user, generates a user token for client-side authentication, creates or retrieves a channel, and sends a message to it. Ensure `STREAM_API_KEY`, `STREAM_API_SECRET`, and `STREAM_USER_ID` are set as environment variables or replaced with actual values.

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!")

view raw JSON →