Zulip Python API Client
The `zulip` Python library provides official client bindings for the Zulip message API, enabling Python applications to interact with a Zulip server. It handles authentication, request formatting, and offers convenient wrapper methods for various API endpoints. Maintained by the Zulip core team, it is considered the most complete and best-documented client library, actively developed to support features up to Zulip server versions. The current version is `0.9.1` on PyPI, with continuous development in line with the broader Zulip project.
Warnings
- gotcha Naming a local file or directory 'zulip.py' or 'zulip' in your project can cause a module shadowing issue, leading to `AttributeError: module 'zulip' has no attribute 'Client'`.
- breaking Some API queries beyond `send_message` are under active development. While they function, their interfaces or behaviors might change in future versions without major version bumps (as the library is pre-1.0.0).
- gotcha When handling API errors, always check the `response['code']` field for specific error conditions rather than `response['msg']`. The `msg` field is human-readable, internationalized, and its string content can change, making it unreliable for programmatic checks.
- gotcha API success responses might include an `ignored_parameters_unsupported` array. This indicates parameters sent in the request that were not supported by the specific endpoint, potentially signaling a client bug or an attempt to use a new feature on an older Zulip server.
- gotcha Configuration for the `zulip.Client` can be provided via command-line options, environment variables (e.g., `ZULIP_API_KEY`, `ZULIP_EMAIL`, `ZULIP_SITE`), or a `~/.zuliprc` file. The precedence order is command-line > environment variables > config file.
Install
-
pip install zulip
Imports
- Client
import zulip client = zulip.Client(...)
Quickstart
import os
import zulip
# Best practice: configure via ~/.zuliprc or environment variables
# For quick testing, you can pass credentials directly (less secure for production)
# Ensure ZULIP_EMAIL, ZULIP_API_KEY, and ZULIP_SITE environment variables are set
# or a ~/.zuliprc file exists.
# Example using environment variables:
# Initialize client from environment variables (or ~/.zuliprc)
client = zulip.Client(config_file=os.environ.get('ZULIP_CONFIG_FILE', '~/.zuliprc'))
# If using direct credentials for testing (replace with your actual bot/user details)
# client = zulip.Client(
# email=os.environ.get('ZULIP_EMAIL', 'bot-email@example.com'),
# api_key=os.environ.get('ZULIP_API_KEY', 'your_api_key'),
# site=os.environ.get('ZULIP_SITE', 'https://your-domain.zulipchat.com')
# )
# Send a stream message
message = {
"type": "stream",
"to": "general",
"topic": "API Test",
"content": "Hello from the Zulip Python API!"
}
try:
result = client.send_message(message)
if result['result'] == 'success':
print("Message sent successfully!")
else:
print(f"Failed to send message: {result['msg']}")
except Exception as e:
print(f"An error occurred: {e}")