Vonage Messages API Python Client
The `vonage-messages` library is an extension for the main `vonage` Python SDK, providing access to the Vonage Messages API for sending SMS, MMS, and WhatsApp messages. It is currently at version 1.6.2 and is actively maintained with regular updates.
Common errors
-
AttributeError: 'Client' object has no attribute 'messages'
cause The `vonage-messages` package, which adds the `.messages` attribute to the `vonage.Client`, has not been installed.fixInstall the `vonage-messages` package: `pip install vonage-messages`. -
ModuleNotFoundError: No module named 'vonage'
cause The core `vonage` Python SDK, which is a required dependency for `vonage-messages`, is not installed.fixInstall the `vonage` package: `pip install vonage`. -
from nexmo import Client
cause Attempting to use an old import path from the deprecated `nexmo-python` SDK instead of the current `vonage-python-sdk`.fixUpdate your import statement to `from vonage import Client` and refactor subsequent API calls according to the `vonage` SDK documentation. -
vonage.errors.VonageError: HTTP error: 401 Client Error: Unauthorized for url: ...
cause Invalid or missing Vonage API Key or API Secret used for authentication.fixVerify that your `VONAGE_API_KEY` and `VONAGE_API_SECRET` are correctly set in your environment variables or passed to the `Client` constructor, and that they are valid for your Vonage account.
Warnings
- gotcha The `vonage-messages` package is an extension and requires the core `vonage` package to be installed alongside it. Without `vonage`, you will encounter import errors or `AttributeError`.
- breaking This library is designed for the modern Vonage Messages API, which handles SMS, MMS, and WhatsApp via a unified endpoint. It does not directly support the older, legacy Vonage (formerly Nexmo) SMS API. If you need the legacy SMS API, refer to the `vonage.Sms` client.
- breaking Users migrating from the deprecated `nexmo-python` SDK to `vonage-python-sdk` will encounter significant breaking changes, including package renames (`nexmo` to `vonage`), module paths, and method signatures.
- gotcha For convenience, the `vonage.Client` can automatically pick up `VONAGE_API_KEY` and `VONAGE_API_SECRET` from environment variables. If you pass empty strings or incorrect values, authentication will fail silently until an API call is made.
Install
-
pip install vonage-messages -
pip install vonage
Imports
- Client
from vonage_messages import Client
from vonage import Client
Quickstart
import os
from vonage import Client
VONAGE_API_KEY = os.environ.get('VONAGE_API_KEY', 'YOUR_API_KEY')
VONAGE_API_SECRET = os.environ.get('VONAGE_API_SECRET', 'YOUR_API_SECRET')
if not VONAGE_API_KEY or not VONAGE_API_SECRET:
print("Please set VONAGE_API_KEY and VONAGE_API_SECRET environment variables or replace placeholders.")
else:
client = Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET)
try:
response_data = client.messages.send(
{
"channel": "sms",
"to": "YOUR_RECIPIENT_NUMBER", # e.g., "447900000000"
"from": "VONAGE_NUMBER", # Your Vonage virtual number
"text": "Hello from Vonage Python SDK!"
}
)
if response_data["messages"][0]["status"] == "0":
print("Message sent successfully.")
else:
print(f"Message failed with error: {response_data['messages'][0]['error_text']}")
except Exception as e:
print(f"An error occurred: {e}")