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:

npm install --location=global @vonage/cli

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:

vonage config:set --apiKey=XXXXXX --apiSecret=XXXXXX

As soon as the CLI is both installed and configured, use it to create a Vonage application using the following command:

vonage apps:create

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.

Rent a Number

You can rent a number using the Vonage CLI. The following command purchases an available number in the United States:

vonage numbers:search USvonage numbers:buy 15555555555 US

Specify an alternative two-character country code to purchase a number in another country.

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:

vonage apps:link APPLICATION_ID --number=YOUR_VONAGE_NUMBER

Example

Replace the following variables in the example code:

KeyDescription
VONAGE_NUMBER

Your Vonage Number. E.g. 447700900000

TO_NUMBER

The number you are calling. E.g. 447700900001

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

View full source

Run your code

Save this file to your machine and run it:

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

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.