Rocket.Chat API Wrapper
rocketchat-api is a Python API wrapper for Rocket.Chat, allowing developers to programmatically interact with Rocket.Chat instances. It provides a convenient interface to various Rocket.Chat API endpoints, abstracting away direct HTTP requests. The library is actively maintained, with a typical release cadence of minor versions every 1-2 months, and its current version is 3.5.0.
Common errors
-
AttributeError: 'dict' object has no attribute 'json'
cause Attempting to call `.json()` on the result of an API method after upgrading to v2.0.0 or later. Methods now directly return the JSON-parsed response.fixRemove the `.json()` call. The API method already returns the dictionary or list directly. Example: Change `rocket.me().json()` to `rocket.me()`. -
rocketchat_API.rocketchat.RocketChatAPIException: Unauthorized
cause Incorrect username, password, or authentication token, or an invalid Rocket.Chat server URL/port.fixDouble-check your `ROCKETCHAT_USER`, `ROCKETCHAT_PASS` (or `ROCKETCHAT_AUTH_TOKEN`), `ROCKETCHAT_HOST`, `ROCKETCHAT_SCHEME`, and `ROCKETCHAT_PORT` environment variables or settings passed to `RocketChatAPI`. Verify network connectivity to the Rocket.Chat server. -
rocketchat_API.rocketchat.RocketChatAPIException: User not found
cause Attempting to access information or perform an action for a user that does not exist on the Rocket.Chat instance or for whom the authenticated user lacks permissions.fixVerify the username or user ID is correct. Ensure the user associated with the API credentials has the necessary permissions to view or interact with the target user. -
TypeError: addUserToRole() got an unexpected keyword argument 'role_name'
cause Calling `addUserToRole` (or `removeUserFromRole`) with `role_name` after upgrading to v2.1.0, which changed the parameter to `role_id`.fixUpdate the method call to use `role_id` instead of `role_name`. You may need to fetch the `_id` of the role first via the roles API. -
rocketchat_API.rocketchat.RocketChatAPIException: MethodNotFound
cause Attempting to call an API method that either does not exist, has been removed from the Rocket.Chat API (or the library wrapper), or the user lacks permissions for.fixConsult the `rocketchat-api` library documentation and Rocket.Chat API documentation for the correct method name and available endpoints. If a method was deprecated, look for an alternative.
Warnings
- breaking Starting with v3.0.0, the library's exception handling was reworked. Exceptions now carry more detailed error information. Code relying on generic exception catching or checking specific exception attributes might need updates.
- breaking In v2.1.0, several API method signatures changed. `rooms_upload` was removed. `addUserToRole` and `removeUserFromRole` now require `role_id` instead of `role_name`. `users_create_token` no longer accepts `username` but requires a `secret` parameter.
- breaking As of v2.0.0, API methods no longer return the raw `requests` response object directly. Instead, they return the JSON body of the response, often as a dictionary or list. Calling `.json()` on the return value will result in an `AttributeError`.
- gotcha When providing a custom port for the Rocket.Chat instance, ensure it is passed as an integer in the `settings` dictionary during `RocketChatAPI` initialization, even if read from environment variables (which are strings).
Install
-
pip install rocketchat-api
Imports
- RocketChatAPI
from rocketchat_API.rocketchat import RocketChatAPI
Quickstart
import os
from rocketchat_API.rocketchat import RocketChatAPI
# Configure connection details and credentials via environment variables for security
ROCKETCHAT_SCHEME = os.environ.get('ROCKETCHAT_SCHEME', 'https')
ROCKETCHAT_HOST = os.environ.get('ROCKETCHAT_HOST', 'demo.rocket.chat')
ROCKETCHAT_PORT = os.environ.get('ROCKETCHAT_PORT', '443') # Note: Port should be string for consistent config
ROCKETCHAT_USER = os.environ.get('ROCKETCHAT_USER', 'your_username')
ROCKETCHAT_PASS = os.environ.get('ROCKETCHAT_PASS', 'your_password')
rocket = RocketChatAPI(settings={
'scheme': ROCKETCHAT_SCHEME,
'host': ROCKETCHAT_HOST,
'port': int(ROCKETCHAT_PORT), # Ensure port is int if expected by library
'username': ROCKETCHAT_USER,
'password': ROCKETCHAT_PASS
})
# Example: Get information about the current user
try:
me_info = rocket.me().json()
print(f"Logged in as: {me_info.get('username')}")
except Exception as e:
print(f"Error connecting or authenticating: {e}")
# Example: Send a direct message
try:
if me_info and 'username' in me_info:
# Replace 'otheruser' with a valid Rocket.Chat username to send a message to
response = rocket.chat_post_message(room_id='GENERAL', text='Hello from rocketchat-api!').json()
print(f"Message sent status: {response.get('success')}")
except Exception as e:
print(f"Error sending message: {e}")