Vonage Messages API Python Client

1.6.2 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates sending an SMS message using the Vonage Messages API. It initializes the `vonage.Client` (which is extended by `vonage-messages`) and then uses the `client.messages.send` method. Remember to replace placeholder numbers and either set API key/secret as environment variables or directly in the code.

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

view raw JSON →