Brevo (formerly Sendinblue) API Python SDK
The `sib-api-v3-sdk` is the official Python client library for the Brevo (formerly Sendinblue) API v3. It provides a wrapper around the RESTful API, enabling developers to programmatically manage email campaigns, transactional emails, SMS, contacts, and retrieve statistics. The library is currently at version 7.6.0 and is actively maintained, with updates typically released as needed for bug fixes and new features.
Common errors
-
ERROR: Command errored out with exit status 1: ... Running setup.py install for sib-api-v3-sdk ... error
cause This often indicates missing C/C++ compilers or other build tools required by one of the SDK's transitive dependencies (e.g., `cryptography` for `urllib3` if an older system Python is used), or issues with permissions.fixEnsure your system has necessary build tools (e.g., `build-essential` on Debian/Ubuntu, Xcode Command Line Tools on macOS) and that your `pip` is up-to-date (`pip install --upgrade pip`). If using a virtual environment, ensure it's active. Sometimes, `pip install --no-binary :all: sib-api-v3-sdk` or installing `wheel` first (`pip install wheel`) can help. -
AttributeError: module 'sib_api_v3_sdk' has no attribute 'TransactionalEmailsApi'
cause This error occurs when trying to access API client classes or model classes directly from the top-level `sib_api_v3_sdk` module, instead of their correct submodules.fixAPI client classes are usually in `sib_api_v3_sdk.api` and model classes in `sib_api_v3_sdk.model`. For example, use `from sib_api_v3_sdk.api import transactional_emails_api` and `from sib_api_v3_sdk.model.send_smtp_email import SendSmtpEmail`. -
sib_api_v3_sdk.rest.ApiException: (401) Reason: Unauthorized HTTP response body: {'code': 'unauthorized', 'message': 'Invalid API Key'}cause The provided API key is either incorrect, revoked, or has insufficient permissions for the attempted operation.fixVerify that your API key is correct and active in your Brevo account. Ensure it has the necessary permissions for the API calls you are making (e.g., Transactional Emails permission for sending emails). Double-check for leading/trailing spaces or typos.
Warnings
- breaking Sendinblue rebranded to Brevo in 2023. While the Python SDK package name `sib-api-v3-sdk` remains the same, documentation, branding, and some concepts now refer to 'Brevo'. This can cause confusion when searching for resources or matching old Sendinblue terminology to new Brevo contexts.
- gotcha API keys are sensitive credentials. Hardcoding them directly in your source code is a security risk. Exposure can lead to unauthorized access to your Brevo account.
- gotcha The SDK is auto-generated from an OpenAPI specification (Swagger Codegen). This means class structures and method names might not always follow typical Pythonic conventions, which can occasionally lead to unexpected import paths or method signatures.
Install
-
pip install sib-api-v3-sdk
Imports
- sib_api_v3_sdk
import sib_api_v3_sdk
- Configuration
import sib_api_v3_sdk.Configuration
from sib_api_v3_sdk import Configuration
- TransactionalEmailsApi
from sib_api_v3_sdk import TransactionalEmailsApi
from sib_api_v3_sdk.api import transactional_emails_api
- SendSmtpEmail
from sib_api_v3_sdk import SendSmtpEmail
from sib_api_v3_sdk.model.send_smtp_email import SendSmtpEmail
Quickstart
import sib_api_v3_sdk
from sib_api_v3_sdk.api import transactional_emails_api
from sib_api_v3_sdk.model.send_smtp_email import SendSmtpEmail
import os
# Configure API key authorization: api-key
configuration = sib_api_v3_sdk.Configuration()
configuration.api_key['api-key'] = os.environ.get('BREVO_API_KEY', 'YOUR_API_KEY')
# Create an instance of the API class
api_instance = transactional_emails_api.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
# Define the email to be sent
sender = {"name":"Sender Name", "email":"sender@example.com"}
to = [{"email":"recipient@example.com", "name":"Recipient Name"}]
send_smtp_email = SendSmtpEmail(
sender=sender,
to=to,
subject="My Test Subject",
html_content="<html><body><h1>This is a test email from Brevo!</h1></body></html>"
)
try:
# Send a transactional email
api_response = api_instance.send_transac_email(send_smtp_email)
print(f"API called successfully. Returned data: {api_response}")
except sib_api_v3_sdk.rest.ApiException as e:
print(f"Exception when calling TransactionalEmailsApi->send_transac_email: {e}")