Brevo Python SDK
The Brevo Python SDK is a client library for interacting with the Brevo (formerly Sendinblue) API, providing access to email, SMS, contacts, CRM, and other marketing automation services. It is a modern, Fern-generated client built on `httpx` and `pydantic`. The library is actively maintained with frequent releases, currently at version 4.0.10.
Common errors
-
ModuleNotFoundError: No module named 'brevo_python'
cause You are attempting to use import statements from the v1.x SDK (`brevo_python`) with the v4.x SDK (`brevo-python`) installed. The module name changed in version 4.fixUpdate your import statements. For example, change `from brevo_python import Brevo` to `from brevo import Brevo`. -
brevo.exceptions.ApiError: (401) Unauthorized
cause The API key provided is either missing, incorrect, or does not have the necessary permissions for the requested operation.fixEnsure your `BREVO_API_KEY` environment variable or the API key passed to `Brevo()` is correct and valid. Verify it has the required permissions in your Brevo account. -
brevo.exceptions.ApiError: (400) The contact already exists.
cause This error message might occur when using `create_contact` with `update_enabled=True` on `brevo-python` versions before 4.0.9, even though the Brevo API successfully updated the contact.fixUpgrade to `brevo-python>=4.0.9`. This bug was fixed to correctly return a successful (empty) response without raising an `ApiError` in this scenario. -
TypeError: Expected <class 'int'>, got <class 'str'>
cause This error can occur when parsing the `duplicate_email_id` field from `get_process` or `get_processes` responses on SDK versions before 4.0.10, due to a type mismatch with the actual API response (URL string vs. expected integer).fixUpgrade to `brevo-python>=4.0.10` to correctly handle the `duplicate_email_id` as an `Optional[str]`.
Warnings
- breaking Version 4.x of `brevo-python` is a complete rewrite with significant breaking changes compared to v1.x. The module name changed from `brevo_python` to `brevo`, and the client object structure, API call methods, and exception handling are entirely different.
- gotcha Prior to v4.0.9, calling `client.contacts.create_contact(..., update_enabled=True)` for an existing contact would incorrectly raise an `ApiError` even though the Brevo API returned a successful 204 No Content response.
- gotcha In `get_process` and `get_processes` responses, the `duplicate_email_id` field's expected type was changed from `Optional[int]` to `Optional[str]` (which is a URL) to match the actual API response.
- gotcha The `status` field for `get_process` and `get_processes` endpoints did not include the `"in_process"` literal type prior to v4.0.10, causing deserialization failures for active processes.
Install
-
pip install brevo-python -
pip install 'brevo-python<4.0'
Imports
- Brevo
from brevo_python import Brevo
from brevo import Brevo
- ApiError
from brevo_python.rest import ApiException
from brevo.exceptions import ApiError
Quickstart
import os
from brevo import Brevo
from brevo.exceptions import ApiError
# Set your Brevo API key as an environment variable (e.g., BREVO_API_KEY)
api_key = os.environ.get("BREVO_API_KEY", "")
if not api_key:
print("Error: BREVO_API_KEY environment variable not set. Please set it to your Brevo API key.")
else:
try:
client = Brevo(api_key=api_key)
# Example: Get account details
account = client.account.get_account()
print(f"Successfully connected to Brevo API. Account email: {account.email}")
print(f"Company name: {account.company_name}")
# Example: Get contacts (first 10)
contacts_response = client.contacts.get_contacts(limit=10)
print(f"Found {len(contacts_response.contacts)} contacts.")
if contacts_response.contacts:
print(f"First contact email: {contacts_response.contacts[0].email}")
except ApiError as e:
print(f"Brevo API Error: {e}")
if "Unauthorized" in str(e):
print("Please check if your BREVO_API_KEY is valid and has the necessary permissions.")
except Exception as e:
print(f"An unexpected error occurred: {e}")