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:

  • Build apps that scale with the web technologies that you are already using
  • Control the flow of inbound and outbound calls in JSON with Nexmo Call Control Objects (NCCO)
  • Record and store inbound or outbound calls
  • Create conference calls
  • Send text-to-speech messages in 23 languages with different genders and accents

Contents

In this document you can learn about:

Concepts

  • Authentication with JWTs - interaction with the Voice API are authenticated using JWTs (JSON Web Tokens). The Nexmo libraries handle JWT generation using a unique Nexmo Voice Application ID and a Private Key. For more information see authenticating your applications.

  • Nexmo Voice Applications - Nexmo Voice Applications represent a one-to-one mapping with the application that you are building. They contain configuration such virtual numbers and webhook callback URLs. You can create Nexmo Voice Applications using the Nexmo CLI or the Application API.

  • NCCOs - Nexmo Call Control Objects are a set of actions that instruct Nexmo how to control the call to your Nexmo application. For example, you can connect a call, send synthesized speech using talk, stream audio, or record a call. They are represented in JSON form as an Array of objects. For more information see the NCCO Reference.

  • Numbers - The key concepts of using phone numbers in the nexmo voice API.

  • Webhooks - HTTP requests are made to your application web server so that you can act upon them. For example, an incoming call will send a webhook.

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

Write the code

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

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://developer.nexmo.com/ncco/tts.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

Install dependencies

Initialize your dependencies

Write the code

Add the following to app.js:

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

Install dependencies

Initialize your dependencies

Write the code

Add the following to OutboundTextToSpeech.java:

nexmo.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 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

Install dependencies

Initialize your dependencies

Write the code

Add the following to VoiceController.cs:


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

Install dependencies

Initialize your dependencies

Write the code

Add the following to index.php:

$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

Install dependencies

Initialize your dependencies

Write the code

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

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

Install dependencies

Initialize your dependencies

Write the code

Add the following to outbound_tts_call.rb:

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

  • Call Flow: The various stages of a call and how they interact.
  • Legs & Conversations: When a phone call is made or received by Nexmo it is added to a conversation. A single conversation contains one or more phone calls (sometimes referred to as legs).
  • DTMF: Capture user input by detecting DTMF tones (button presses) during a call.
  • Endpoints: When connecting a call, you can connect to another phone number, a sip endpoint or a websocket. These are known as endpoints.
  • Lex connector: You can use the Lex Connector to connect a Nexmo voice call to an AWS Lex  bot and then have an audio conversation with the bot.
  • NCCO: To tell Nexmo how to handle a phone call, you must provide Nexmo an Nexmo Call Control Objects (NCCO) when a call is placed or answered. There are various actions available, such as talk, input and record.
  • Numbers: Numbers are a key part of using the Nexmo voice API. This guide covers number formatting, outgoing caller IDs and incoming call numbers.
  • Recording: Recording audio input from a caller or recording the conversation between two callers.
  • Text to Speech: Using our Text-To-Speech engine, you can play machine-generated speech to your callers
  • Websockets: You can connect the audio of a call to a websocket to work with it in real time.

Building Blocks

Tutorials

Reference