Voice API Overview

The Nexmo Voice API is the easiest way to build high-quality voice applications in the Cloud. With the Voice API you can:

Contents

In this document you can learn about:

Concepts

Getting Started

Voice Playground

In the Nexmo Dashboard, you can try out the Voice API interactively in the Voice Playground. Once you are signed up for a Nexmo account, you can go to Voice Playground in the Dashboard (Voice ‣ Voice Playground).

More details are available in this blog post: Meet Voice Playground, Your Testing Sandbox for Nexmo Voice Apps

Using the API

The primary way that you'll interact with the Nexmo voice platform is via the public API. To place an outbound call, you make a POST request to https://api.nexmo.com/v1/calls.

To make this easier, we provide client libraries in various languages that take care of authentication and creating the correct request body for you. Choose your language below to get started.

Prerequisites

Create an application

A Nexmo application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Nexmo concepts guide.

Install the CLI

$ npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Nexmo application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Nexmo needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

$ nexmo app:create "Outbound Call Building Block" https://developer.nexmo.com/ncco/tts.json http://demo.ngrok.io/webhooks/events --keyfile private.key

Write the code

Add the following to make-an-outbound-call.sh:

Copy to Clipboard
curl -X POST https://api.nexmo.com/v1/calls\
  -H "Authorization: Bearer "$JWT\
  -H "Content-Type: application/json"\
  -d '{"to":[{"type": "phone","number": "'$TO_NUMBER'"}],
      "from": {"type": "phone","number": "'$NEXMO_NUMBER'"},
      "answer_url":["https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json"]}'

View full source

Run your code

Save this file to your machine and run it:

$ bash make-an-outbound-call.sh

Prerequisites

Create an application

A Nexmo application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Nexmo concepts guide.

Install the CLI

$ npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Nexmo application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Nexmo needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

$ nexmo app:create "Outbound Call Building Block" https://developer.nexmo.com/ncco/tts.json http://demo.ngrok.io/webhooks/events --keyfile private.key
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_APPLICATION_PRIVATE_KEY_PATH
})

View full source

Write the code

Add the following to app.js:

Copy to Clipboard
nexmo.calls.create({
  to: [{
    type: 'phone',
    number: TO_NUMBER
  }],
  from: {
    type: 'phone',
    number: NEXMO_NUMBER
  },
  answer_url: ['https://developer.nexmo.com/ncco/tts.json']
})

View full source

Run your code

Save this file to your machine and run it:

$ node app.js

Prerequisites

Create an application

A Nexmo application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Nexmo concepts guide.

Install the CLI

$ npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Nexmo application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Nexmo needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

$ nexmo app:create "Outbound Call Building Block" https://developer.nexmo.com/ncco/tts.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies

Add the following to build.gradle:

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

Create a class named OutboundTextToSpeech 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 OutboundTextToSpeech class:

Copy to Clipboard
client.getVoiceClient().createCall(new Call(TO_NUMBER, NEXMO_NUMBER, ANSWER_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 OutboundTextToSpeech:

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

Prerequisites

Create an application

A Nexmo application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Nexmo concepts guide.

Install the CLI

$ npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Nexmo application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Nexmo needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

$ nexmo app:create "Outbound Call Building Block" https://developer.nexmo.com/ncco/tts.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies

$ Install-Package Nexmo.Csharp.Client
Initialize your dependencies

Create a file named VoiceController.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 VoiceController.cs:

Copy to Clipboard

var results = Client.Call.Do(new Call.CallCommand
{
    to = new[]
    {
        new Call.Endpoint {
            type = "phone",
            number = TO_NUMBER
        }
    },
    from = new Call.Endpoint
    {
        type = "phone",
        number = NEXMO_NUMBER
    },
    answer_url = new[]
    {
        "https://developer.nexmo.com/ncco/tts.json"
    }

View full source

Run your code

Save this file to your machine and run it:

$ Run using your IDE

Prerequisites

Create an application

A Nexmo application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Nexmo concepts guide.

Install the CLI

$ npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Nexmo application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Nexmo needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

$ nexmo app:create "Outbound Call Building Block" https://developer.nexmo.com/ncco/tts.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies

$ composer require nexmo/client slim/slim:^3.8
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
$call = $client->calls()->create([
    'to' => [[
        'type' => 'phone',
        'number' => TO_NUMBER
    ]],
    'from' => [
        'type' => 'phone',
        'number' => NEXMO_NUMBER
    ],
    'answer_url' => ['https://developer.nexmo.com/ncco/tts.json'],
]);

View full source

Run your code

Save this file to your machine and run it:

$ php -t . -S localhost:3000

Prerequisites

Create an application

A Nexmo application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Nexmo concepts guide.

Install the CLI

$ npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Nexmo application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Nexmo needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

$ nexmo app:create "Outbound Call Building Block" https://developer.nexmo.com/ncco/tts.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies

$ pip install nexmo
Initialize your dependencies

Create a file named make-an-outbound-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 make-an-outbound-call.py:

Copy to Clipboard
response = client.create_call({
  'to': [{'type': 'phone', 'number': TO_NUMBER}],
  'from': {'type': 'phone', 'number': NEXMO_NUMBER},
  'answer_url': ['https://developer.nexmo.com/ncco/tts.json']
})

pprint(response)

View full source

Run your code

Save this file to your machine and run it:

$ python3 make-an-outbound-call.py

Prerequisites

Create an application

A Nexmo application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Nexmo concepts guide.

Install the CLI

$ npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Nexmo application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Nexmo needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

$ nexmo app:create "Outbound Call Building Block" https://developer.nexmo.com/ncco/tts.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies

$ gem install nexmo
Initialize your dependencies

Create a file named outbound_tts_call.rb and add the following code:

Copy to Clipboard
require 'nexmo'

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

View full source

Write the code

Add the following to outbound_tts_call.rb:

Copy to Clipboard
response = client.calls.create(
  to: [{
    type: 'phone',
    number: RECIPIENT_NUMBER
  }],
  from: {
    type: 'phone',
    number: NEXMO_NUMBER
  },
  answer_url: [
    'https://developer.nexmo.com/ncco/tts.json'
  ]
)

View full source

Run your code

Save this file to your machine and run it:

$ ruby outbound_tts_call.rb

Guides

Building Blocks

Tutorials

Reference