How to Receive Phone Calls with Node-RED

This is the third article in a series of "Getting Started with Nexmo and Node-RED" tutorials.

In the previous tutorials you've learnt how to send and receive SMS messages programatically using the Nexmo SMS API and how to handle delivery receipts.

Next, you'll be moving on to the next chapter, exploring the Nexmo Voice API.

By the end of this article, you'll have handled your first inbound call with Node-RED.

Prerequisites

Before getting started, you’ll need a few things:

DT API Account

To complete this tutorial, you will need a DT API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the DT API Dashboard.

This tutorial also uses a virtual phone number. To purchase one, go to Numbers > Buy Numbers and search for one that meets your needs.

Getting Your Credentials

In order to interact with the Voice API, you'll need to make note of a couple of things. Once you've created a Nexmo account, go to the dashboard to find your API key and secret.

Next, you'll need a Voice enabled virtual number. Go to Numbers > Buy numbers to get one.

Setting Up Your Node-RED Editor

First, you’ll need to install the runtime and editor. This could be done either on your local machine, on a Single Board Computer (eg Raspberry Pi), or a number of cloud-hosted options. This example will be using your local machine, so once you've installed Node-RED globally, just type the command below in your terminal to get started.

node-red

You can then access the Node-RED editor by pointing your browser at http://localhost:1880.

Once you have your editor open, you'll need to install the Nexmo nodes. You can do so under the Manage palette menu, by searching for the node-red-contrib-nexmo package and clicking install.

nexmo node rednexmo node red

Now you should see all of the Nexmo nodes appear on the left side of your screen, among the other default nodes.

Handle an Inbound Phone Call with Node-RED

Exposing Your Local Server to the Internet

First, you'll have to expose your local server to the internet, so that Nexmo can access it. If you’re running Node-RED on a public webserver instead of your local machine, you can skip this stage.

Otherwise, a convenient way to do this is by using a tunneling service like ngrok.

Download and install ngrok, then run it in the terminal to start a tunnel on port 1880.

./ngrok http 1880

Navigate to the URL displayed to find your Node-RED Editor.

ngrok inbound callngrok inbound call

Creating a Nexmo Application

In the previous SMS tutorials, you were able to configure a phone number directly with an endpoint; however, this is not always the case.

Some of Nexmo’s APIs, including the Voice API, use Nexmo Applications to hold security and config information needed to connect to Nexmo endpoints.

In the Nexmo Node-RED palette, several nodes have the ability to create these applications: getrecording, earmuff, mute, hangup, transfer, createcall, playaudio, playtts and playdtmf.

Drag any of these nodes into your workspace, then double-click on it to open up the node editor.

Next to the Nexmo Credentials, select "Add new nexmovoiceapp..." from the drop-down menu and click the edit button. Fill in the details below and click Create New Application.

KEY DESCRIPTION
Name Choose a name for your Voice Application, for example inbound call.
API Key Your Nexmo API key, shown in your account overview.
API Secret Your Nexmo API secret, shown in your account overview.
Answer URL YOUR_URL/answer, you'll be hosting a Nexmo Call Control Object (NCCO) here. - more about this later on.
Event URL YOUR_URL/event, you'll need to reference this when setting up the event handler.

Node-RED will then create a new Nexmo Application on your account and fill in the App ID and Private Key fields for you to save. After this step, feel free to delete the Nexmo node you used, as a nexmovoiceapp config node has been created, and that contains all the Nexmo credentials this flow needs.

Next stepNext step

Next, you'll have to link your virtual number to this application.

Find the Voice Application you've just created in your Nexmo Dashboard by navigating to Voice > Your Applications.

Click on the name of this application, then under the Numbers tab click on the Link button next to the virtual number you've rented earlier.

Alternatively, if the number you'd like to use is already linked to another app, click on Manage number and configure it to forward incoming calls to your app.

link numberlink number

Build the Nexmo Call Control Object (NCCO)

Nexmo calls are controlled using Nexmo Call Control Objects, also known as NCCOs. An NCCO defines a list of actions to be followed when a call is handled. There are lots of different actions available, find the corresponding nodes under the Nexmo palette in your Node-RED editor or check out the NCCO Reference to find out more about them.

For this tutorial, you'll be using the talk action.

Drag and drop the talk node into your workspace, then connect it to a voice webhook input node and a return NCCO output node.

Next, in the voice webhook node, select GET as a method and type something like /answer in the answer URL field.

Finally, go to the talk node properties and set the Text{} field to the message you'd like to be read out when the call is answered. Note the {} sign next to the Text label, indicating that this value can be set dynamically, using Mustache templating. You can also select a Voice Name, see the Text to Speech Guide for the full list of options.

Create tts nccoCreate tts ncco

Setting Up a Handler for the Event URL

Connect a http input node to a http response node, as well as to a debug node, so that you can view your delivery receipt in the debug area.

In the http input node, select POST as a Method and fill in the URL field with something like /event.

The http response node should have 200 set as Status code, but don't worry about it, this is the default value as well.

Now hit Deploy, call your virtual number and follow the flow of your call in the debug sidebar.

inbound call debuginbound call debug

Next Steps

In this tutorial, you've learnt how to play a text-to-speech message to a caller. In a quite similar manner, you could also play an audio file to them, or forward the call to a phone number. If you'd like to take it further, why not record the conversation or set up your custom voicemail? Stay tuned to find out how!

Resources

Julia BiroVonage Alumni

Julia is a Developer Advocate for Vonage focusing on low-code technologies and integrations. Maker and breaker of things, explorer of yet another builder tool. Forever challenging the barriers to entry of tech, she is passionate about learning and teaching, creating resources for the community.

Ready to start building?

Experience seamless connectivity, real-time messaging, and crystal-clear voice and video calls-all at your fingertips.