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
NEXMO_NUMBER Your Nexmo 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

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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
Generate your JWT

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": "'$NEXMO_NUMBER'"},
      "ncco": [
        {
          "action": "talk",
          "text": "This is a text to speech call from Nexmo"
        }
      ]}'

View full source

Run your code

Save this file to your machine and run it:

sh make-an-outbound-call-with-ncco.sh

Prerequisites

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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 dependencies

Add the following to build.gradle:

compile 'com.nexmo:client:4.3.0'
Initialize your dependencies

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

Copy to Clipboard
NexmoClient client = NexmoClient.builder()
        .applicationId(NEXMO_APPLICATION_ID)
        .privateKeyPath(NEXMO_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 Nexmo").build());

client.getVoiceClient().createCall(new Call(TO_NUMBER, NEXMO_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:

apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Run the following gradle command to execute your application, replacing com.nexmo.quickstart.voice with the package containing OutboundTextToSpeechWithNcco:

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

Prerequisites

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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 dependencies

Install-Package Nexmo.Csharp.Client
Initialize your dependencies

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

Copy to Clipboard
var client = new Client(creds: new Nexmo.Api.Request.Credentials
{
    ApiKey = "NEXMO_API_KEY",
    ApiSecret = "NEXMO_API_SECRET",
    ApplicationId = "NEXMO_APPLICATION_ID",
    ApplicationKey = "NEXMO_APPLICATION_PRIVATE_KEY"
});

View full source

Write the code

Add the following to VoiceController.cs:

Copy to Clipboard
    var results = Client.Call.Do(new Call.CallCommand
    {
        to = new[]
        {
            new Call.Endpoint {
                type = "phone",
                number = TO_NUMBER
            }
        },
        from = new Call.Endpoint
        {
            type = "phone",
            number = NEXMO_NUMBER
        },

        Ncco = CreateNCCO()
    });

    Session["UUID"] = results.uuid;

    return RedirectToAction("MakeCall"); ;
}

private JArray CreateNCCO()
{
    dynamic TalkNCCO = new JObject();
    TalkNCCO.action = "talk";
    TalkNCCO.text = "This is a text to speech call from Nexmo";

    JArray ncco = new JArray();
    ncco.Add(TalkNCCO);
    return ncco;
}

View full source

Prerequisites

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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 dependencies

composer require nexmo/client
Initialize your dependencies

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

Copy to Clipboard
$keypair = new \Nexmo\Client\Credentials\Keypair(
    file_get_contents(NEXMO_APPLICATION_PRIVATE_KEY_PATH),
    NEXMO_APPLICATION_ID
);
$client = new \Nexmo\Client($keypair);

View full source

Write the code

Add the following to index.php:

Copy to Clipboard
$call = $client->calls()->create([
    'to' => [[
        'type' => 'phone',
        'number' => TO_NUMBER
    ]],
    'from' => [
        'type' => 'phone',
        'number' => NEXMO_NUMBER
    ],
    'ncco' => [
        [
            'action' => 'talk',
            'text' => 'This is a text to speech call from Nexmo'
        ]
    ]
]);

View full source

Run your code

Save this file to your machine and run it:

php index.php

Prerequisites

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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 dependencies

pip install nexmo
Initialize your dependencies

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

Copy to Clipboard
import nexmo
from pprint import pprint

client = nexmo.Client(
    application_id=APPLICATION_ID,
    private_key=APPLICATION_PRIVATE_KEY_PATH,
)

View full source

Write the code

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

Copy to Clipboard
response = client.create_call({
    'to': [{'type': 'phone', 'number': TO_NUMBER}],
    'from': {'type': 'phone', 'number': NEXMO_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:

python make-outbound-call-ncco.py

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.