Zulip Python API Client

0.9.1 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

Initializes the Zulip client using either a configuration file (like `~/.zuliprc`) or direct credentials, and then sends a stream message. For production, `~/.zuliprc` or environment variables are recommended for securely managing API keys and other credentials. Direct credentials are shown for quick testing, but should be replaced with environment variables for security.

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

view raw JSON →