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

Example

Replace the following variables in the example code:

Key Description
VONAGE_NUMBER Your Vonage Number. E.g. 447700900000
TO_NUMBER The number you are calling. E.g. 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

nexmo app:create "Outbound Call with NCCO code snippet" https://example.com/webhooks/answer https://example.com/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-with-ncco.sh:

Copy to Clipboard
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"
        }
      ]}'

View full source

Run your code

Save this file to your machine and run it:

sh make-an-outbound-call-with-ncco.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

nexmo app:create "Outbound Call with NCCO code snippet" https://example.com/webhooks/answer https://example.com/webhooks/events --keyfile private.key

npm install @vonage/server-sdk

Create a file named make-call-ncco.js and add the following code:

Copy to Clipboard
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
})

View full source

Write the code

Add the following to make-call-ncco.js:

Copy to Clipboard
vonage.calls.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"
  }]
}, (error, response) => {
  if (error) console.error(error)
  if (response) console.log(response)
})

View full source

Run your code

Save this file to your machine and run it:

node make-call-ncco.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

nexmo app:create "Outbound Call with NCCO code snippet" https://example.com/webhooks/answer https://example.com/webhooks/events --keyfile private.key

Add the following to `build.gradle`:

compile 'com.vonage:client:5.5.0'

Create a class named OutboundTextToSpeechWithNcco and add the following code to the main method:

Copy to Clipboard
VonageClient client = VonageClient.builder()
        .applicationId(VONAGE_APPLICATION_ID)
        .privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
        .build();

View full source

Write the code

Add the following to the main method of the OutboundTextToSpeechWithNcco class:

Copy to Clipboard
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));

View full source

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:

Copy to Clipboard
  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:

gradle run -Pmain=com.vonage.quickstart.voice.OutboundTextToSpeechWithNcco

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

nexmo app:create "Outbound Call with NCCO code snippet" https://example.com/webhooks/answer https://example.com/webhooks/events --keyfile private.key

Install-Package Vonage

Create a file named MakeCallWithNcco.cs and add the following code:

Copy to Clipboard
using Vonage.Request;
using Vonage;
using Vonage.Voice;
using Vonage.Voice.Nccos;
using Vonage.Voice.Nccos.Endpoints;

View full source

Add the following to MakeCallWithNcco.cs:

Copy to Clipboard
var creds = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(creds);

View full source

Write the code

Add the following to MakeCallWithNcco.cs:

Copy to Clipboard
var toEndpoint = new PhoneEndpoint() { Number = TO_NUMBER };
var fromEndpoint = new PhoneEndpoint() { Number = VONAGE_NUMBER };
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);

View full source

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

nexmo app:create "Outbound Call with NCCO code snippet" https://example.com/webhooks/answer https://example.com/webhooks/events --keyfile private.key

composer require vonage/client

Create a file named index.php and add the following code:

Copy to Clipboard
use Vonage\Voice\NCCO\NCCO;

View full source

Add the following to index.php:

Copy to Clipboard
require_once __DIR__ . '/../../config.php';
require_once __DIR__ . '/../../vendor/autoload.php';

$keypair = new \Vonage\Client\Credentials\Keypair(
    file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
    VONAGE_APPLICATION_ID
);
$client = new \Vonage\Client($keypair);

View full source

Write the code

Add the following to index.php:

Copy to Clipboard
$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);

View full source

Run your code

Save this file to your machine and run it:

php index.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

nexmo app:create "Outbound Call with NCCO code snippet" https://example.com/webhooks/answer https://example.com/webhooks/events --keyfile private.key

pip install vonage

Create a file named make-outbound-call-ncco.py and add the following code:

Copy to Clipboard
client = vonage.Client(
    application_id=VONAGE_APPLICATION_ID,
    private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
)

View full source

Write the code

Add the following to make-outbound-call-ncco.py:

Copy to Clipboard
voice = vonage.Voice(client)

response = voice.create_call({
    'to': [{'type': 'phone', 'number': TO_NUMBER}],
    'from': {'type': 'phone', 'number': VONAGE_NUMBER},
    'ncco': [{'action': 'talk', 'text': 'This is a text to speech call from Nexmo'}]
})

pprint(response)

View full source

Run your code

Save this file to your machine and run it:

python3 make-outbound-call-ncco.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

nexmo app:create "Outbound Call with NCCO code snippet" https://example.com/webhooks/answer https://example.com/webhooks/events --keyfile private.key

gem install vonage

Create a file named make-outbound-call-with-ncco.rb and add the following code:

Copy to Clipboard
client = Vonage::Client.new(
  application_id: VONAGE_APPLICATION_ID,
  private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH)
)

View full source

Write the code

Add the following to make-outbound-call-with-ncco.rb:

Copy to Clipboard
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'
    }
  ]
)

View full source

Run your code

Save this file to your machine and run it:

ruby make-outbound-call-with-ncco.rb

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.