This tutorial explains how to make and receive phone calls with Node.js and the Nexmo Voice API. With Nexmo's Voice API, you can build high-quality programmable voice applications in the cloud and handle tasks such as managing outbound and inbound calls in JSON, recording and storing calls, creating a conference call, sending text-to-speech messages in 23 languages, and so on.
In this tutorial, you will learn how to use the Application and Voice APIs to create a text-to-speech outbound phone call securely and then how to receive inbound calls by implementing a webhook. This tutorial assumes you are working with a Unix or Linux-based operating system such as Mac OS X or Ubuntu.
Securing Your Nexmo Application
Some of Nexmo's APIs use Nexmo applications, which contain the security and configuration information you need to connect to the Voice API endpoints.
- holds security information - to connect to Nexmo endpoints
- contains configuration data for your app and the URLs to your webhook endpoints
- uses Nexmo Call Control Objects (NCCO) to control your calls
Figure 1: Using the Nexmo Voice API to call your mobile phone
Creating a Nexmo Application and Generating a Private Key
Your app needs to authenticate requests to the Voice API. In this section, you will generate a private key with the Application API, which allows you to create JSON Web Tokens (JWT) to make the requests.
Begin by creating an application with the Nexmo command-line interface (CLI) tool, which will set up the app and generate a private key for you.
Make sure Node.js 4.0 or above is installed on your machine, then install nexmo-cli from npm:
$ npm install nexmo-cli -g
When you initially signup at Nexmo.com, you will be presented with your Nexmo API key and secret. It will look something like this:
This information is also easily retrieved at any time by visiting the settings area of your Nexmo dashboard.
Next, set up the CLI with your Nexmo API key and secret:
$ nexmo setup API_KEY API_SECRET
$ nexmo setup aabb11cc fB1234567890abcdefabcde
This will locally save your credentials to a hidden file in your home directory: ~/.nexmorc.
Once configured, you are going to create an application in your working directory using the CLI.