Voice API Overview
The Vonage Voice API is the easiest way to build high-quality voice applications in the Cloud. With the Voice API you can:
- Build apps that scale with the web technologies you are already using
- Control the flow of inbound and outbound calls in JSON with Nexmo Call Control Objects (NCCO). (Nexmo is now Vonage).
- Record and store inbound or outbound calls
- Create conference calls
- Send text-to-speech messages in 40 languages with different genders and accents
Contents
In this document you can learn about:
- Vonage Voice API Concepts to introduce terminology
- How to Get Started with the Voice API including examples in your language
- Guides learn about working with the Voice API
- Code Snippets code snippets to help with specific tasks
- Use Cases detailed use cases with code examples
- Reference API documentation and other supporting content
Concepts
Authentication with JWTs - interaction with the Voice API are authenticated using JWTs (JSON Web Tokens). The libraries handle JWT generation using a unique Vonage Voice Application ID and a Private Key. For more information see authenticating your applications.
Vonage Voice Applications - Vonage Voice Applications represent a one-to-one mapping with the application that you are building. They contain configuration such as virtual numbers and webhook callback URLs. You can create Vonage Voice Applications using the Vonage Dashboard, the Nexmo CLI, or via the Application API.
NCCOs - Nexmo Call Control Objects are a set of actions that instruct Vonage how to control the call to your Vonage application. For example, you can
connect
a call, send synthesized speech usingtalk
,stream
audio, orrecord
a call. They are represented in JSON form as an Array of objects. For more information see the NCCO Reference.Numbers - The key concepts of using phone numbers in the Vonage Voice API.
Webhooks - HTTP requests are made to your application web server so that you can act upon them. For example, an incoming call will send a webhook.
Getting Started
Voice Playground
In the Developer Dashboard, you can try out the Voice API interactively in the Voice Playground. Once you are signed up for a Vonage API account, you can go to Voice Playground in the Dashboard (Voice ‣ Voice Playground).
More details are available in this blog post: Meet Voice Playground, Your Testing Sandbox for Vonage Voice Apps
Using the API
The primary way that you'll interact with the Vonage API voice platform is via the public API. To place an outbound call, you make a POST
request to https://api.nexmo.com/v1/calls
.
To make this easier, Vonage provides Server SDKs in various languages that take care of authentication and creating the correct request body for you.
To get started, choose your language below and replace the following variables in the example code:
Key | Description |
---|---|
VONAGE_NUMBER |
Your Vonage number that the call will be made from. For example 447700900000 . |
TO_NUMBER |
The number you would like to call to in E.164 format. For example 447700900001 . |
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Vonage needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Outbound Call code snippet" https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Execute the following command at your terminal prompt to create the JWT for authentication:
export JWT=$(nexmo jwt:generate $PATH_TO_PRIVATE_KEY application_id=$NEXMO_APPLICATION_ID)
Write the code
Add the following to make-an-outbound-call.sh
:
curl -X POST https://api.nexmo.com/v1/calls\
-H "Authorization: Bearer "$JWT\
-H "Content-Type: application/json"\
-d '{"to":[{"type": "phone","number": "$TO_NUMBER"}],
"from": {"type": "phone","number": "$VONAGE_NUMBER"},
"answer_url":["https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json"]}'
Run your code
Save this file to your machine and run it:
bash make-an-outbound-call.sh
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Vonage needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Outbound Call code snippet" https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json http://demo.ngrok.io/webhooks/events --keyfile private.key
npm install @vonage/server-sdk
Create a file named make-call.js
and add the following code:
const Vonage = require('@vonage/server-sdk')
const vonage = new Vonage({
apiKey: VONAGE_API_KEY,
apiSecret: VONAGE_API_SECRET,
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_APPLICATION_PRIVATE_KEY_PATH
})
Write the code
Add the following to make-call.js
:
const ANSWER_URL = 'https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json'
vonage.calls.create({
to: [{
type: 'phone',
number: TO_NUMBER
}],
from: {
type: 'phone',
number: VONAGE_NUMBER
},
answer_url: [ANSWER_URL]
}, (error, response) => {
if (error) console.error(error)
if (response) console.log(response)
})
Run your code
Save this file to your machine and run it:
node make-call.js
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Vonage needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Outbound Call code snippet" https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Add the following to `build.gradle`:
compile 'com.vonage:client:5.5.0'
Create a class named OutboundTextToSpeech
and add the following code to the main
method:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();
Write the code
Add the following to the main
method of the OutboundTextToSpeech
class:
final String ANSWER_URL = "https://nexmo-community.github.io/ncco-examples/talk.json";
client.getVoiceClient().createCall(new Call(TO_NUMBER, VONAGE_NUMBER, ANSWER_URL));
Run your code
We can use the application
plugin for Gradle to simplify the running of our application.
Update your build.gradle
with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''
Run the following gradle
command to execute your application, replacing com.vonage.quickstart.voice
with the package containing OutboundTextToSpeech
:
gradle run -Pmain=com.vonage.quickstart.voice.OutboundTextToSpeech
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Vonage needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Outbound Call code snippet" https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Install-Package Vonage
Create a file named MakeOutboundCall.cs
and add the following code:
using Vonage;
using Vonage.Request;
using Vonage.Voice;
using Vonage.Voice.Nccos.Endpoints;
Add the following to MakeOutboundCall.cs
:
var creds = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(creds);
Write the code
Add the following to MakeOutboundCall.cs
:
var ANSWER_URL = "https://nexmo-community.github.io/ncco-examples/text-to-speech.json";
var toEndpoint = new PhoneEndpoint() { Number = TO_NUMBER };
var fromEndpoint = new PhoneEndpoint() { Number = VONAGE_NUMBER };
var command = new CallCommand() { To = new Endpoint[] { toEndpoint }, From = fromEndpoint, AnswerUrl = new[] { ANSWER_URL } };
var response = client.VoiceClient.CreateCall(command);
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Vonage needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Outbound Call code snippet" https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json http://demo.ngrok.io/webhooks/events --keyfile private.key
composer require vonage/client
Create a file named text-to-speech-outbound.php
and add the following code:
$keypair = new \Vonage\Client\Credentials\Keypair(
file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
VONAGE_APPLICATION_ID
);
$client = new \Vonage\Client($keypair);
Write the code
Add the following to text-to-speech-outbound.php
:
$outboundCall = new \Vonage\Voice\OutboundCall(
new \Vonage\Voice\Endpoint\Phone(TO_NUMBER),
new \Vonage\Voice\Endpoint\Phone(VONAGE_NUMBER)
);
$outboundCall->setAnswerWebhook(
new \Vonage\Voice\Webhook(
'https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json',
\Vonage\Voice\Webhook::METHOD_GET
)
);
$response = $client->voice()->createOutboundCall($outboundCall);
var_dump($response);
Run your code
Save this file to your machine and run it:
php text-to-speech-outbound.php
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Vonage needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Outbound Call code snippet" https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json http://demo.ngrok.io/webhooks/events --keyfile private.key
pip install vonage
Create a file named make-an-outbound-call.py
and add the following code:
client = vonage.Client(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
)
Write the code
Add the following to make-an-outbound-call.py
:
voice = vonage.Voice(client)
response = voice.create_call({
'to': [{'type': 'phone', 'number': TO_NUMBER}],
'from': {'type': 'phone', 'number': VONAGE_NUMBER},
'answer_url': ['https://developer.nexmo.com/ncco/tts.json']
})
pprint(response)
Run your code
Save this file to your machine and run it:
python3 make-an-outbound-call.py
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Vonage needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Outbound Call code snippet" https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json http://demo.ngrok.io/webhooks/events --keyfile private.key
gem install vonage
Create a file named outbound_tts_call.rb
and add the following code:
client = Vonage::Client.new(
api_key: VONAGE_API_KEY,
api_secret: VONAGE_API_SECRET,
application_id: VONAGE_APPLICATION_ID,
private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH)
)
Write the code
Add the following to outbound_tts_call.rb
:
response = client.voice.create(
to: [{
type: 'phone',
number: TO_NUMBER
}],
from: {
type: 'phone',
number: VONAGE_NUMBER
},
answer_url: [
'https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json'
]
)
puts response.inspect
Run your code
Save this file to your machine and run it:
ruby outbound_tts_call.rb
Guides
- Call Flow: The various stages of a call and how they interact.
- Numbers: Numbers are a key part of using the Vonage Voice API. This guide covers number formatting, outgoing caller IDs and incoming call numbers.
- Legs and Conversations: When a phone call is made or received by Vonage it is added to a conversation. A single conversation contains one or more phone calls (sometimes referred to as legs).
-
Nexmo Call Control Objects: To tell Vonage how to handle a phone call, you must provide a Nexmo Call Control Object (NCCO) when a call is placed or answered. There are various actions available, such as
talk
,input
andrecord
. - Text to Speech: Using our Text-To-Speech engine, you can play machine-generated speech to your callers
- Customizing Spoken Text: Use Speech Synthesis Markup Language (SSML) to control how text-to-speech is read out
-
Endpoints: When connecting a call, you can connect to another phone number, a
sip
endpoint or awebsocket
. These are known as endpoints. - Recording: Recording audio input from a caller or recording the conversation between two callers.
- Speech Recognition: Capture user input by converting user speech to text form during a call.
- DTMF: Capture user input by detecting DTMF tones (button presses) during a call.
- WebSockets: You can connect the audio of a call to a websocket to work with it in real time.
- Signed Webhooks: A method for your application to verify a request is coming from Vonage.
- Contact Center Intelligence: Learn how to enhance your contact center solution with Voice API.
- Fraud Prevention: Protect your Voice API applications from fraud
Code Snippets
- Before you begin
- Make an outbound call with an NCCO
- Connect an inbound call
- Download a recording
- Earmuff a call
- Handle user input with ASR
- Handle user input with DTMF
- Connect callers into a conference
- Make an outbound call
- Mute a call
- Play an audio stream into a call
- Play DTMF into a call
- Play text-to-speech into a call
- Receive an inbound call
- Record a call with split audio
- Record a call
- Record a conversation
- Record a message
- Retrieve information for a call
- Retrieve information for all calls
- Transfer a call
- Transfer a call with inline NCCO
- Track NCCO progress
Use Cases
- Local Numbers
- Private Voice Communication
- Voice Bot
- Interactive Voice Response (IVR)
- Contact Center Augmentation
- Call Whisper
- Voice-based Critical Alerts
- Call Recording Transcription
- Call Tracking