Vonage SMS Package

1.1.6 · active · verified Thu Apr 16

The `vonage-sms` package provides Python access to Vonage's SMS API, enabling sending and receiving text messages globally. It is designed to be used as part of the main `vonage` Python SDK. The current version is 1.1.6, with updates typically aligned with the broader Vonage Python SDK releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to send an SMS message using the `vonage` client, which integrates the `vonage-sms` package. It uses environment variables for API credentials and phone numbers, and includes basic error handling. Phone numbers must be in E.164 format.

import os
from vonage import Vonage
from vonage_sms import SmsMessage

# Ensure VONAGE_API_KEY and VONAGE_API_SECRET are set as environment variables
api_key = os.environ.get('VONAGE_API_KEY', 'YOUR_API_KEY')
api_secret = os.environ.get('VONAGE_API_SECRET', 'YOUR_API_SECRET')

if api_key == 'YOUR_API_KEY' or api_secret == 'YOUR_API_SECRET':
    print("Please set VONAGE_API_KEY and VONAGE_API_SECRET environment variables.")
else:
    client = Vonage(key=api_key, secret=api_secret)

    # Replace with your Vonage virtual number (sender) and recipient number
    # Numbers must be in E.164 format (e.g., '12345678900')
    from_number = os.environ.get('VONAGE_NUMBER', 'YOUR_VONAGE_NUMBER')
    to_number = os.environ.get('RECIPIENT_NUMBER', 'YOUR_RECIPIENT_NUMBER')

    if from_number == 'YOUR_VONAGE_NUMBER' or to_number == 'YOUR_RECIPIENT_NUMBER':
        print("Please set VONAGE_NUMBER and RECIPIENT_NUMBER environment variables.")
    else:
        message = SmsMessage(
            to=to_number,
            from_=from_number,
            text='Hello from the Vonage Python SDK!'
        )

        try:
            response = client.sms.send(message)
            if response.messages[0].status == '0':
                print(f"Message sent successfully to {to_number}.")
            else:
                print(f"Message failed with error: {response.messages[0].error_text} (status: {response.messages[0].status})")
        except Exception as e:
            print(f"An error occurred: {e}")

view raw JSON →