Vonage Server SDK for Python


This is the Python server SDK to help you use Vonage APIs in your Python application. To use it you'll need a Vonage account. Sign up for free on the Vonage site.
Contents:
Installation
It's recommended to create a new virtual environment to install the SDK. You can do this with
# Create the virtual environment
python3 -m venv venv
# Activate the virtual environment in Mac/Linux
. ./venv/bin/activate
# Or on Windows Command Prompt
venv\Scripts\activate
To install the Python SDK package using pip:
To upgrade your installed client library using pip:
pip install vonage --upgrade
Alternatively, you can clone the repository via the command line, or by opening it on GitHub desktop.
Migration Guides
V3 to V4
This version of the Vonage Python SDK (4.x+) works very differently to the previous SDK. See the v3 -> v4 migration guide for help migrating your application code using v3 of the SDK to the new structure.
OpenTok to Vonage Video API
This SDK includes support for the Vonage Video API. If you have an application that uses OpenTok for video and want to migrate (which is highly recommended!) then A migration guide is available here which will help you to migrate your applications to use Vonage Video.
Calling Vonage APIs
The Vonage Python SDK is a monorepo, with separate packages for each API. When you install the Python SDK, you'll see there's a top-level package, vonage, and then specialised packages for every API class.
Most methods to call Vonage APIs are accessed through the top-level vonage package. Many require specific custom data models accessed though the specific Vonage package corresponding to the API you're trying to use.
For example, to send an SMS, you will access the SMS method from vonage and the SmsMessage object from the vonage-sms package. This looks something like this:
from vonage_sms import SmsMessage, SmsResponse
message = SmsMessage(to='1234567890', from_='Acme Inc.', text='Hello, World!')
response: SmsResponse = vonage_client.sms.send(message) # vonage_client is an instance of `vonage.Vonage`
print(response.model_dump(exclude_unset=True))
Usage
Many of the use cases require you to buy a Vonage Number, which you can do in the Vonage Developer Dashboard.
from vonage import Vonage, Auth, HttpClientOptions
# Create an Auth instance
auth = Auth(api_key='your_api_key', api_secret='your_api_secret')
# Create HttpClientOptions instance
# (not required unless you want to change options from the defaults)
options = HttpClientOptions(api_host='api.nexmo.com', timeout=30)
# Create a Vonage instance
vonage = Vonage(auth=auth, http_client_options=options)
The Vonage class provides access to various Vonage APIs through its properties. For example, to use methods to call the SMS API:
from vonage_sms import SmsMessage
message = SmsMessage(to='1234567890', from_='Vonage', text='Hello World')
response = client.sms.send(message)
print(response.model_dump_json(exclude_unset=True))
You can also access the underlying HttpClient instance through the http_client property:
user_agent = vonage.http_client.user_agent
Convert a Pydantic Model to Dict or Json
Most responses to API calls in the SDK are Pydantic models. To convert a Pydantic model to a dict, use model.model_dump. To convert to a JSON string, use model.model_dump_json
response = vonage.api_package.api_call(...)
response_dict = response.model_dump()
response_json = response.model_dump_json()
Account API
Get Account Balance
balance = vonage_client.account.get_balance()
print(balance)
Top-Up Account
response = vonage_client.account.top_up(trx='1234567890')
print(response)
Update the Default SMS Webhook
This will return a Pydantic object (SettingsResponse) containing multiple settings for your account.
settings: SettingsResponse = vonage_client.account.update_default_sms_webhook(
mo_callback_url='https://example.com/inbound_sms_webhook',
dr_callback_url='https://example.com/delivery_receipt_webhook',
)
print(settings)
Get Service Pricing for a Specific Country
from vonage_account import GetCountryPricingRequest
response = vonage_client.account.get_country_pricing(
GetCountryPricingRequest(type='sms', country_code='US')
)
print(response)
Get Service Pricing for All Countries
response = vonage_client.account.get_all_countries_pricing(service_type='sms')
print(response)
Get Service Pricing by Dialing Prefix
from vonage_account import GetPrefixPricingRequest
response = client.account.get_prefix_pricing(
GetPrefixPricingRequest(prefix='44', type='sms')
)
print(response)
List Secrets Associated with the Account
response = vonage_client.account.list_secrets()
print(response)
Create a New Account Secret
secret = vonage_client.account.create_secret('Mytestsecret12345')
print(secret)
Get Information About One Secret
secret = vonage_client.account.get_secret(MY_SECRET_ID)
print(secret)
Revoke a Secret
Note: it isn't possible to revoke all account secrets, there must always be one valid secret. Attempting to do so will give a 403 error.
client.account.revoke_secret(MY_SECRET_ID)
Application API
List Applications
With no custom options specified, this method will get the first 100 applications. It returns a tuple consisting of a list of ApplicationData objects and an int showing the page number of the next page of results.
from vonage_application import ListApplicationsFilter, ApplicationData
applications, next_page = vonage_client.application.list_applications()
# With options
options = ListApplicationsFilter(page_size=3, page=2)
applications, next_page = vonage_client.application.list_applications(options)
Create a New Application
from vonage_application import ApplicationConfig
app_data = vonage_client.application.create_application()
# Create with custom options (can also be done with a dict)
from vonage_application import ApplicationConfig, Keys, Voice, VoiceWebhooks
voice = Voice(
webhooks=VoiceWebhooks(
event_url=VoiceUrl(
address='https://example.com/event',
http_method='POST',
connect_timeout=500,
socket_timeout=3000,
),
),
signed_callbacks=True,
)
capabilities = Capabilities(voice=voice)
keys = Keys(public_key='MY_PUBLIC_KEY')
config = ApplicationConfig(
name='My Customised Application',
capabilities=capabilities,
keys=keys,
)
app_data = vonage_client.application.create_application(config)
Get an Application
app_data = client.application.get_application('MY_APP_ID')
app_data_as_dict = app.model_dump(exclude_none=True)
Update an Application
To update an application, pass config for the updated field(s) in an ApplicationConfig object