Connect an inbound call

In this code snippet you see how to connect an inbound call to another person by making an outbound call.

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

YOUR_SECOND_NUMBER

The number you are connecting to. E.g. 447700900002

Prerequisites

npm install express

Write the code

Add the following to connect-an-inbound-call.js:

const Express = require('express');

const app = new Express();

const onInboundCall = (request, response) => {
  const ncco = [
    {
      action: 'connect',
      from: VONAGE_NUMBER,
      endpoint: [
        {
          type: 'phone',
          number: YOUR_SECOND_NUMBER,
        },
      ],
    },
  ];

  response.json(ncco);
};

app.get('/webhooks/answer', onInboundCall);

app.listen(3000);

View full source

Run your code

Save this file to your machine and run it:

node connect-an-inbound-call.js

Try it out

You'll need to expose your server to the open internet. During development you can use a tool like Ngrok to do that.

When you call your Vonage Number you will automatically be connected to the number you specified in place of YOUR_SECOND_NUMBER.

## Further Reading

  • Interactive Voice Response (IVR) - Build an automated phone system for users to input information with the keypad and hear a spoken response.
  • 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.
  • Masked Calling - Enable users to call each other, keeping their real numbers private.
  • Conference Calling - This guide explains the two concepts Vonage associates with a call, a leg and a conversation.
  • Call Tracking - Keep track of which campaigns are working well by using different numbers for each one and tracking the incoming calls. This guide shows you how to handle incoming calls, connect them to another number, and track the phone numbers that called each of your Vonage numbers.