Make an outbound call

This code snippet makes an outbound call and plays a text-to-speech message when the call is answered.

Prerequisites

Create an Application

Check out Getting Started with Vonage CLI for more information on how to install and configure the CLI.

Use the following command to create a Vonage application:

vonage apps create 'Your application' ✅ Creating Application Saving private key ... Done! Application created Name: Your application Application ID: 00000000-0000-0000-0000-000000000000 Improve AI: Off Private/Public Key: Set Capabilities: None Enabled

Once you have the application, you then need to add the voice capabilities; make sure you replace the webhook URLs with your own:

vonage apps capabilities update 00000000-0000-0000-0000-000000000000 voice \ --voice-answer-url='https://example.com/webhooks/voice/answer' \ --voice-event-url='https://example.com/webhooks/voice/event' \ --voice-fallback-url='https://example.com/webhooks/voice/fallback' ✅ Fetching Application ✅ Adding voice capability to application 00000000-0000-0000-0000-000000000000 Name: Your application Application ID: 00000000-0000-0000-0000-000000000000 Improve AI: Off Private/Public Key: Set Capabilities: VOICE: Uses Signed callbacks: On Conversation TTL: 41 hours Leg Persistence Time: 6 days Event URL: [POST] https://example.com/webhooks/voice/event Answer URL: [POST] https://example.com/webhooks/voice/answer Fallback URL: [POST] https://example.com/webhooks/voice/fallback

The application is then created and has the required capabilities. Creating an application and adding application capabilities are covered in detail in the application overview.

Rent a Number

You can rent a number using the Vonage CLI. The following command searches for an available number in the United States:

vonage numbers search US ✅ Searching for numbers There is 1 number available for purchase in United States Number Type Features Monthly Cost Setup Cost ----------- ------ --------------- ------------ ---------- 16127779311 Mobile MMS, SMS, VOICE €0.90 €0.00 Use vonage numbers buy to purchase.

Use this command to purchase a number:

vonage numbers buy US 16127779311 ✅ Searching for numbers Are you sure you want to purchase the number 16127779311 for €0.90? [y/n] y ✅ Purchasing number Number 16127779311 purchased Number: 16127779311 Country: 🇺🇸 United States Type: Mobile Features: MMS, SMS, VOICE Monthly Cost: €0.90 Setup Cost: €0.00 Linked Application ID: Not linked to any application Voice Callback: Not Set Voice Callback Value: Not Set Voice Status Callback: Not Set

Specify an alternative two-character country code to purchase a number in another country.

Now that you have both an application and a number, you need to link them together.

vonage apps numbers link 00000000-0000-0000-0000-000000000000 16127779311 ✅ Fetching Application Fetching Owned numbers [===============================================] 1/1 100% Number linked Number: 16127779311 Country: 🇺🇸 United States Type: Toll-free Features: MMS, SMS, VOICE Monthly Cost: Not Set Setup Cost: Not Set Linked Application ID: 00000000-0000-0000-0000-000000000000 Voice Callback: app Voice Callback Value: 00000000-0000-0000-0000-000000000000 Voice Status Callback: Not Set

Example

Replace the following variables in the example code:

KeyDescription
VONAGE_VIRTUAL_NUMBER

Your Vonage Number. E.g. 447700900000

VOICE_TO_NUMBER

The recipient number to call, e.g. 447700900002.

VOICE_ANSWER_URL

The answer URL. For example https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json.

Prerequisites

Execute the following command at your terminal prompt to create the JWT for authentication:

Write the code

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

View full source

Run your code

Save this file to your machine and run it:

bash make-an-outbound-call.sh

Try it out

When you run the code the TO_NUMBER will be called and a text-to-speech message will be heard if the call is answered.

Further Reading

  • Voice Notifications - In this guide, you will learn how to contact a list of people by phone, convey a message, and see who confirmed that they had received the message. These voice-based critical alerts are more persistent than a text message, making your message more likely to be noticed. Additionally, with the recipient confirmation, you can be sure that your message made it through.
  • Conference Calling - This guide explains the two concepts Vonage associates with a call, a leg and a conversation.
  • 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.