Make an outbound call

This building block makes an outbound call and plays a text-to-speech message when the call is answered.

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 Building Block" https://developer.nexmo.com/ncco/tts.json https://example.com/webhooks/events --keyfile private.key

Write the code

Add the following to make-an-outbound-call.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'"},
      "answer_url":["https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json"]}'

View full source

Run your code

Save this file to your machine and run it:

$ bash make-an-outbound-call.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 Building Block" https://developer.nexmo.com/ncco/tts.json https://example.com/webhooks/events --keyfile private.key
Install dependencies

$ npm install nexmo dotenv
Initialize your dependencies

Create a file named app.js and add the following code:

Copy to Clipboard
const Nexmo = require('nexmo')

const nexmo = new Nexmo({
  apiKey: NEXMO_API_KEY,
  apiSecret: NEXMO_API_SECRET,
  applicationId: NEXMO_APPLICATION_ID,
  privateKey: NEXMO_APPLICATION_PRIVATE_KEY_PATH
})

View full source

Write the code

Add the following to app.js:

Copy to Clipboard
nexmo.calls.create({
  to: [{
    type: 'phone',
    number: TO_NUMBER
  }],
  from: {
    type: 'phone',
    number: NEXMO_NUMBER
  },
  answer_url: ['https://developer.nexmo.com/ncco/tts.json']
})

View full source

Run your code

Save this file to your machine and run it:

$ node app.js

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 Building Block" https://developer.nexmo.com/ncco/tts.json https://example.com/webhooks/events --keyfile private.key
Install dependencies

Add the following to build.gradle:

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

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

Copy to Clipboard
NexmoClient client = new 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 OutboundTextToSpeech class:

Copy to Clipboard
client.getVoiceClient().createCall(new Call(TO_NUMBER, NEXMO_NUMBER, ANSWER_URL));

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 OutboundTextToSpeech:

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

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 Building Block" https://developer.nexmo.com/ncco/tts.json 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
    },
    answer_url = new[]
    {
        "https://developer.nexmo.com/ncco/tts.json"
    }

View full source

Run your code

Save this file to your machine and run it:

$ Run using your IDE

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 Building Block" https://developer.nexmo.com/ncco/tts.json https://example.com/webhooks/events --keyfile private.key
Install dependencies

$ composer require nexmo/client slim/slim:^3.8
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
    ],
    'answer_url' => ['https://developer.nexmo.com/ncco/tts.json'],
]);

View full source

Run your code

Save this file to your machine and run it:

$ php -t . -S localhost:3000

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 Building Block" https://developer.nexmo.com/ncco/tts.json https://example.com/webhooks/events --keyfile private.key
Install dependencies

$ pip install nexmo
Initialize your dependencies

Create a file named make-an-outbound-call.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-an-outbound-call.py:

Copy to Clipboard
response = client.create_call({
  'to': [{'type': 'phone', 'number': TO_NUMBER}],
  'from': {'type': 'phone', 'number': NEXMO_NUMBER},
  'answer_url': ['https://developer.nexmo.com/ncco/tts.json']
})

pprint(response)

View full source

Run your code

Save this file to your machine and run it:

$ python3 make-an-outbound-call.py

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 Building Block" https://developer.nexmo.com/ncco/tts.json https://example.com/webhooks/events --keyfile private.key
Install dependencies

$ gem install nexmo
Initialize your dependencies

Create a file named outbound_tts_call.rb and add the following code:

Copy to Clipboard
require 'nexmo'

client = Nexmo::Client.new(
  api_key: NEXMO_API_KEY,
  api_secret: NEXMO_API_SECRET,
  application_id: NEXMO_APPLICATION_ID,
  private_key: File.read(NEXMO_APPLICATION_PRIVATE_KEY_PATH)
)

View full source

Write the code

Add the following to outbound_tts_call.rb:

Copy to Clipboard
response = client.calls.create(
  to: [{
    type: 'phone',
    number: RECIPIENT_NUMBER
  }],
  from: {
    type: 'phone',
    number: NEXMO_NUMBER
  },
  answer_url: [
    'https://developer.nexmo.com/ncco/tts.json'
  ]
)

View full source

Run your code

Save this file to your machine and run it:

$ ruby outbound_tts_call.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.