Transfer a call with inline NCCO

A code snippet that shows how to transfer control of the current call to control using inline 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.

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
Initialize your dependencies

Create a file named transfer-call-with-ncco.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 transfer-call-with-ncco.js:

Copy to Clipboard
nexmo.calls.update(CALL_UUID, {
  action: 'transfer',
  destination: {
    "type": "ncco",
    "ncco": [
      "action": 'talk',
      "text": 'This is a transfer action using an inline NCCO'
    ]
  }
}, (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 transfer-call-with-ncco.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

Install-Package Nexmo.Csharp.Client
Initialize your dependencies

Create a file named VoiceSender.cs and add the following code:

Copy to Clipboard
var client = new Client(creds: new Nexmo.Api.Request.Credentials
{
    ApiKey = "NEXMO_API_KEY",
    ApiSecret = "NEXMO_API_SECRET",
    ApplicationId = "NEXMO_APPLICATION_ID",
    ApplicationKey = "NEXMO_APPLICATION_PRIVATE_KEY"
});

View full source

Write the code

Add the following to VoiceSender.cs:

Copy to Clipboard
var talkAction = new TalkAction() { Text = "This is a transfer action using an inline NCCO" };
var ncco = new Ncco(talkAction);
var response = client.Call.Edit(UUID,
    new Call.CallEditCommand()
    {
        Action = "transfer",
        Destination = new Call.Destination()
        {
            Type = "ncco",
            Ncco = ncco
        }
    });

View full source

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
try {
    $client->calls[UUID]->put([
        'action' => 'transfer',
        'destination' => [
            'type' => 'ncco',
            'ncco' => [[
                'action' => 'talk',
                'text' => 'This call was transferred'
            ]],
        ]
    ]);
} catch (\Nexmo\Client\Exception\Request $e) {
    error_log("Client error: " . $e->getMessage());
    exit(1);
} catch (\Nexmo\Client\Exception\Server $e) {
    error_log("Server error: " . $e->getMessage());
    exit(1);
}

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

gem install nexmo
Initialize your dependencies

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

Copy to Clipboard
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-ncco.rb:

Copy to Clipboard
ncco = {
  "type": 'ncco',
  "ncco": [
    "action": 'talk',
    "text": 'This is a transfer action using an inline NCCO'
  ]
}

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-ncco.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' code snippet 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.