Vonage Voice Application Helpers

1.4.0 · active · verified Thu Apr 16

The `vonage-voice` library provides helper functionality for building Voice Applications with the Vonage Voice API in Python. It is designed to be used in conjunction with the main `vonage` Python SDK (version 3.0.0 or higher), which contains the core Voice API client. This package facilitates NCCO (Nexmo Call Control Object) generation and other voice-related utilities, simplifying the creation of interactive voice experiences. The current version is 1.4.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple NCCO (Nexmo Call Control Object) using the `vonage-voice` helpers. It also shows the initialization of the core Vonage API client from the `vonage` package, which is necessary for making actual API calls or serving the NCCO. Replace placeholders with your actual API credentials and phone numbers.

import os
from vonage import Client
from vonage_voice import Ncco, TalkNcco

# Ensure VONAGE_API_KEY and VONAGE_API_SECRET are set as environment variables
# OR VONAGE_APPLICATION_ID and VONAGE_PRIVATE_KEY

# Initialize the Vonage Client (from the 'vonage' package)
client = Client(
    key=os.environ.get('VONAGE_API_KEY', 'YOUR_API_KEY'),
    secret=os.environ.get('VONAGE_API_SECRET', 'YOUR_API_SECRET'),
    application_id=os.environ.get('VONAGE_APPLICATION_ID', ''),
    private_key=os.environ.get('VONAGE_PRIVATE_KEY', '').replace('\\n', '\n')
)

# Create an NCCO using vonage-voice helpers
ncco = Ncco()
ncco.add_action(TalkNcco(text='Hello from Vonage! This is an NCCO generated by vonage-voice.'))

# In a real application, you would return this NCCO as a JSON response
# to a webhook for an incoming call. For demonstration, print:
print(ncco.build())

# Example of making an outbound call (using the core 'vonage' client):
# Requires VONAGE_APPLICATION_ID and VONAGE_PRIVATE_KEY for voice calls.
# try:
#     call = client.voice.create_call({
#         'to': [{'type': 'phone', 'number': 'RECIPIENT_PHONE_NUMBER'}],
#         'from': {'type': 'phone', 'number': 'VIRTUAL_NUMBER'},
#         'answer_url': ['https://example.com/webhooks/answer'] # Your public webhook URL
#     })
#     print(f"Call initiated with UUID: {call['uuid']}")
# except Exception as e:
#     print(f"Error initiating call: {e}")

view raw JSON →