Make an outbound call with an NCCO
This code snippet makes an outbound call and plays a text-to-speech message when the call is answered. You don't need to run a server hosting an answer_url to run this code snippet, as you provide your NCCO as part of the request
Prerequisites
Create an Application
You can install the CLI with the following command:
Before you can start working with your apps, you need to register your configuration: API Key and Secret. You can find them via the Dashboard, in API Settings. Once set, initialize your account using the following command:
As soon as the CLI is both installed and configured, use it to create a Vonage application using the following command:
The command starts an interactive prompt to ask for the application name, and the capabilities you want to enable - make sure to enable Voice.
When finished, it creates the vonage_app.json file in the current directory containing the Application ID, Application name and private key. It also creates a second file with the private key name app_name.key.
Go to the Application's page on the Dashboard, and define a Name for your Application.

Make sure to click on the Generate public and private key button, and keep the file private.key around.
Then, enable the Voice capability. For the moment, leave everything by default.

Finally, click Save at the bottom of the page.
Rent a Number
You can rent a number using the Vonage CLI. The following command purchases an available number in the United States:
Specify an alternative two-character country code to purchase a number in another country.
In the Dashboard, go to the Buy Numbers page. Make sure to tick Voice in the search filter, and select the country you want to buy a number in.

You can then click the Buy button next to the number you want, and validate your purchase.
Congratulations! Your virtual number is now listed in Your Numbers
Link a Number
Now that you have both an application and a number, you need to link them together.
Replace YOUR_VONAGE_NUMBER with the number you bought and APPLICATION_ID with your application id, then run the following command:
Now that you have both an application and a number, you need to link them together.
Go to the Application page, and click on the application you created earlier.

In the Voice section, click on the Link button next to the number you want to link.
Example
Replace the following variables in the example code:
| Key | Description |
|---|---|
VONAGE_NUMBER | Your Vonage Number. E.g. |
TO_NUMBER | The number you are calling. E.g. |
Prerequisites
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-with-ncco.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'"},
"ncco": [
{
"action": "talk",
"text": "This is a text to speech call from Vonage"
}
]}'
Run your code
Save this file to your machine and run it:
Prerequisites
npm install @vonage/server-sdkCreate a file named make-call-ncco.js and add the following code:
const { Vonage } = require('@vonage/server-sdk');
const { NCCOBuilder, Talk } = require('@vonage/voice');
const vonage = new Vonage({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_APPLICATION_PRIVATE_KEY_PATH,
});Write the code
Add the following to make-call-ncco.js:
const builder = new NCCOBuilder();
builder.addAction(new Talk('This is a text to speech call from Vonage'));
vonage.voice.createOutboundCall({
ncco: builder.build(),
to: [
{
type: 'phone',
number: TO_NUMBER,
},
{
type: 'phone',
number: VONAGE_NUMBER,
},
],
})
.then((result) => console.log(result))
.catch((error) => console.error(error));Run your code
Save this file to your machine and run it:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk-kotlin:1.1.2'Create a class named OutboundTextToSpeechCallWithNcco and add the following code to the main method:
Write the code
Add the following to the main method of the OutboundTextToSpeechCallWithNcco class:
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.kt.voice with the package containing OutboundTextToSpeechCallWithNcco:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:8.15.1'Write the code
Add the following to the main method of the OutboundTextToSpeechWithNcco class:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();
final String VONAGE_NUMBER = envVar("VONAGE_NUMBER");
final String TO_NUMBER = envVar("TO_NUMBER");
Ncco ncco = new Ncco(TalkAction.builder("This is a text to speech call from Vonage").build());
client.getVoiceClient().createCall(new Call(TO_NUMBER, VONAGE_NUMBER, ncco.getActions()));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 OutboundTextToSpeechWithNcco:
Prerequisites
Install-Package VonageWrite the code
Add the following to MakeCallWithNcco.cs:
var creds = Credentials.FromAppIdAndPrivateKeyPath(vonageApplicationId, vonagePrivateKeyPath);
var client = new VonageClient(creds);
var toEndpoint = new PhoneEndpoint() { Number = toNumber };
var fromEndpoint = new PhoneEndpoint() { Number = vonageNumber };
var extraText = "";
for (var i = 0; i < 50; i++)
extraText += $"{i} ";
var talkAction = new TalkAction() { Text = "This is a text to speech call from Vonage " + extraText };
var ncco = new Ncco(talkAction);
var command = new CallCommand() { To = new Endpoint[] { toEndpoint }, From = fromEndpoint, Ncco = ncco, EventUrl = eventUrl };
var response = await client.VoiceClient.CreateCallAsync(command);Prerequisites
composer require vonage/clientWrite the code
Add the following to index.php:
$keypair = new \Vonage\Client\Credentials\Keypair(
file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
VONAGE_APPLICATION_ID
);
$client = new \Vonage\Client($keypair);
$outboundCall = new \Vonage\Voice\OutboundCall(
new \Vonage\Voice\Endpoint\Phone(TO_NUMBER),
new \Vonage\Voice\Endpoint\Phone(VONAGE_NUMBER)
);
$ncco = new NCCO();
$ncco->addAction(new \Vonage\Voice\NCCO\Action\Talk('This is a text to speech call from Nexmo'));
$outboundCall->setNCCO($ncco);
$response = $client->voice()->createOutboundCall($outboundCall);
var_dump($response);Run your code
Save this file to your machine and run it:
Prerequisites
pip install vonageWrite the code
Add the following to make-outbound-call-ncco.py:
from vonage import Auth, Vonage
from vonage_voice.models import CreateCallRequest, Phone, Talk, ToPhone
client = Vonage(
Auth(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
)
)
response = client.voice.create_call(
CreateCallRequest(
ncco=[Talk(text='Hello world')],
to=[ToPhone(number=TO_NUMBER)],
from_=Phone(number=VONAGE_NUMBER),
)
)
pprint(response)Run your code
Save this file to your machine and run it:
Prerequisites
gem install vonageWrite the code
Add the following to make-outbound-call-with-ncco.rb:
client = Vonage::Client.new(
application_id: VONAGE_APPLICATION_ID,
private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH)
)
client.voice.create(
to: [{
type: 'phone',
number: TO_NUMBER
}],
from: {
type: 'phone',
number: VONAGE_NUMBER
},
ncco: [
{
'action' => 'talk',
'text' => 'This is a text to speech call from Vonage'
}
]
)Run your code
Save this file to your machine and run it:
Try it out
When you run the code the TO_NUMBER will be called and a text-to-speech message will be heard if the call is answered.
Further Reading
- Voice Notifications - In this guide, you will learn how to contact a list of people by phone, convey a message, and see who confirmed that they had received the message. These voice-based critical alerts are more persistent than a text message, making your message more likely to be noticed. Additionally, with the recipient confirmation, you can be sure that your message made it through.
- Conference Calling - This guide explains the two concepts Vonage associates with a call, a leg and a conversation.
- Voice Bot with Google Dialogflow - This guide will help you to start with an example Dialogflow bot and interact with it from phone calls using provided sample reference codes using Vonage Voice API.