Transfer a call

A building block that shows how to transfer control of the current call to control by a new NCCO.

Example

Replace the following variables in the example code:

Key Description
UUID The UUID of the call to modify.
action The action to use in updating the call. In this case transfer.
url The URL of the NCCO to transfer control to. URLs must be array type (for Python library).

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Write the code

Add the following to transfer-a-call.sh:

Copy to Clipboard
curl -X PUT https://api.nexmo.com/v1/calls/$UUID \
  -H "Authorization: Bearer "$JWT \
  -H "Content-Type: application/json"\
  -d '{"action": "transfer",
      "destination": {"type": "ncco", "url": ["https://developer.nexmo.com/ncco/tts.json"]}}'

View full source

Run your code

Save this file to your machine and run it:

$ bash transfer-a-call.sh

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

$ npm install nexmo dotenv
Initialize your dependencies

Create a file named app.js and add the following code:

Copy to Clipboard
const Nexmo = require('nexmo');

const nexmo = new Nexmo({
  apiKey: NEXMO_API_KEY,
  apiSecret: NEXMO_API_SECRET,
  applicationId: NEXMO_APPLICATION_ID,
  privateKey: NEXMO_PRIVATE_KEY
}, {debug: true});

View full source

Write the code

Add the following to app.js:

Copy to Clipboard
nexmo.calls.update(CALL_UUID, {action: 'transfer', destination: {"type": "ncco", "url": ["https://developer.nexmo.com/ncco/tts.json"]}}, (err, res) => {
  if(err) { console.error(err); }
  else {
      console.log(res);
  }
});

View full source

Run your code

Save this file to your machine and run it:

$ node app.js

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

Add the following to build.gradle:

compile 'com.nexmo:client:4.0.0'
Initialize your dependencies

Create a class named TransferCall and add the following code to the main method:

Copy to Clipboard
NexmoClient client = new NexmoClient.Builder()
        .applicationId(NEXMO_APPLICATION_ID)
        .privateKeyPath(NEXMO_PRIVATE_KEY_PATH)
        .build();

View full source

Write the code

Add the following to the main method of the TransferCall class:

Copy to Clipboard
client.getVoiceClient().transferCall(UUID, NCCO_URL);

View full source

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.nexmo.quickstart.voice with the package containing TransferCall:

gradle run -Pmain=com.nexmo.quickstart.voice.TransferCall

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

$ composer require nexmo/client
Initialize your dependencies

Create a file named index.php and add the following code:

Copy to Clipboard
$keypair = new \Nexmo\Client\Credentials\Keypair(
    file_get_contents(NEXMO_APPLICATION_PRIVATE_KEY_PATH),
    NEXMO_APPLICATION_ID
);

$client = new \Nexmo\Client($keypair);

View full source

Write the code

Add the following to index.php:

Copy to Clipboard
$client->calls[UUID]->put(new \Nexmo\Call\Transfer("https://developer.nexmo.com/ncco/transfer.json"));

View full source

Run your code

Save this file to your machine and run it:

$ php index.php

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

$ pip install nexmo
Initialize your dependencies

Create a file named transfer-a-call.py and add the following code:

Copy to Clipboard
import nexmo
from pprint import pprint

client = nexmo.Client(
    application_id=APPLICATION_ID,
    private_key=APPLICATION_PRIVATE_KEY_PATH,
)

View full source

Write the code

Add the following to transfer-a-call.py:

Copy to Clipboard
dest = {"type": "ncco", "url": ["https://developer.nexmo.com/ncco/tts.json"]}
response = client.update_call(UUID, action="transfer", destination=dest)
pprint(response)

View full source

Run your code

Save this file to your machine and run it:

$ python3 transfer-a-call.py

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

$ gem install nexmo
Initialize your dependencies

Create a file named transfer-a-call.rb and add the following code:

Copy to Clipboard
require 'nexmo'

client = Nexmo::Client.new(
    application_id: NEXMO_APPLICATION_ID,
    private_key: File.read(NEXMO_APPLICATION_PRIVATE_KEY_PATH)
)

View full source

Write the code

Add the following to transfer-a-call.rb:

Copy to Clipboard
ncco = {"type": "ncco", "url": ["https://developer.nexmo.com/ncco/transfer.json"]}

response = client.calls.transfer(UUID, destination: ncco)

puts response.inspect

View full source

Run your code

Save this file to your machine and run it:

$ ruby transfer-a-call.rb

Try it out

You will need to:

  1. Set up a call and obtain the call UUID. You could use the 'connect an inbound call' building block to do this.
  2. Run the example code to transfer the call.
  3. Control will be transferred to a new NCCO, and you will hear a text-to-speech message to confirm this.