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:
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:
As soon as the CLI is both installed and configured, use it to create a Vonage application using the following command:
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.
Go to the Application's page on the Dashboard, and define a Name for your Application.

Make sure to click on the Generate public and private key button, and keep the file private.key around.
Then, enable the Voice capability. For the moment, leave everything by default.

Finally, click Save at the bottom of the page.
Rent a Number
You can rent a number using the Vonage CLI. The following command purchases an available number in the United States:
Specify an alternative two-character country code to purchase a number in another country.
In the Dashboard, go to the Buy Numbers page. Make sure to tick Voice in the search filter, and select the country you want to buy a number in.

You can then click the Buy button next to the number you want, and validate your purchase.
Congratulations! Your virtual number is now listed in Your Numbers
Link a Number
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:
Now that you have both an application and a number, you need to link them together.
Go to the Application page, and click on the application you created earlier.

In the Voice section, click on the Link button next to the number you want to link.
Example
Replace the following variables in the example code:
| Key | Description |
|---|---|
VONAGE_NUMBER | Your Vonage Number. E.g. |
YOUR_SECOND_NUMBER | The number you are connecting to. E.g. |
Prerequisites
npm install expressWrite 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);Run your code
Save this file to your machine and run it:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk-kotlin:1.1.2'
implementation 'io.ktor:ktor-server-netty'
implementation 'io.ktor:ktor-serialization-jackson'Write the code
Add the following to the main method of the ConnectInboundCall file:
embeddedServer(Netty, port = 8000) {
routing {
route("/webhooks/answer") {
handle {
call.response.header("Content-Type", "application/json")
call.respond(
Ncco(
connectToPstn(YOUR_SECOND_NUMBER) {
from(VONAGE_NUMBER)
}
).toJson()
)
}
}
}
}.start(wait = true)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.vonage.quickstart.kt.voice with the package containing ConnectInboundCall:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:8.15.1'
implementation 'com.sparkjava:spark-core:2.9.4'Write the code
Add the following to the main method of the ConnectInboundCall file:
/*
* Route to answer incoming calls with an NCCO response.
*/
Route answerRoute = (req, res) -> {
ConnectAction connect = ConnectAction.builder()
.endpoint(PhoneEndpoint.builder(YOUR_SECOND_NUMBER).build())
.from(VONAGE_NUMBER)
.build();
res.type("application/json");
return new Ncco(connect).toJson();
};
Spark.port(3000);
Spark.get("/webhooks/answer", answerRoute);
Spark.post("/webhooks/answer", answerRoute);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.vonage.quickstart.voice with the package containing ConnectInboundCall:
Prerequisites
Install-Package VonageWrite the code
Add the following to ConnectInboundCallController.cs:
[Route("webhooks/answer")]
public string Answer()
{
var yourSecondNumber = Environment.GetEnvironmentVariable("YOUR_SECOND_NUMBER") ?? "YOUR_SECOND_NUMBER";
var vonageNumber = Environment.GetEnvironmentVariable("VONAGE_NUMBER") ?? "VONAGE_NUMBER";
var talkAction = new TalkAction()
{
Text = "Thank you for calling",
Language = "en-gb",
Style = 2
};
var secondNumberEndpoint = new PhoneEndpoint() { Number=yourSecondNumber};
var connectAction = new ConnectAction() { From=vonageNumber, Endpoint= new[] { secondNumberEndpoint } };
var ncco = new Ncco(talkAction,connectAction);
return ncco.ToString();
}Prerequisites
composer require slim/slim:^3.8 vonage/clientWrite the code
Add the following to index.php:
require 'vendor/autoload.php';
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
define('YOUR_SECOND_NUMBER', getenv('YOUR_SECOND_NUMBER'));
define('VONAGE_NUMBER', getenv('VONAGE_NUMBER'));
$app = new \Slim\App();
$app->get('/webhooks/answer', function (Request $request, Response $response) {
$numberToConnect = new \Vonage\Voice\Endpoint\Phone(YOUR_SECOND_NUMBER);
$action = new \Vonage\Voice\NCCO\Action\Connect($numberToConnect);
$action->setFrom(VONAGE_NUMBER);
$ncco = new \Vonage\Voice\NCCO\NCCO();
$ncco->addAction($action);
return new JsonResponse($ncco->toArray());
});
$app->run();Run your code
Save this file to your machine and run it:
Prerequisites
pip install 'flask>=1.0'Write the code
Add the following to connect-an-inbound-call.py:
#!/usr/bin/env python3
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/webhooks/answer")
def answer_call():
ncco = [
{
"action": "connect",
"from": "VONAGE_NUMBER",
"endpoint": [{
"type": 'phone',
"number": "YOUR_SECOND_NUMBER"
}]
}
]
return jsonify(ncco)
Run your code
Save this file to your machine and run it:
Prerequisites
gem install sinatra sinatra-contribWrite the code
Add the following to connect_an_inbound_call.rb:
require 'sinatra'
require 'sinatra/multi_route'
require 'json'
before do
content_type :json
end
route :get, :post, '/webhooks/answer' do
[{
action: 'connect',
endpoint: [{
type: 'phone',
number: YOUR_SECOND_NUMBER
}]
}].to_json
end
set :port, 3000Run your code
Save this file to your machine and run it:
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.