Make a Phone Call From Your Command Line With the Twilio CLI

March 11, 2021
Written by
Reviewed by

phonecallcommandline

Twilio Programmable Voice allows you to make and receive voice calls in your software application. In this tutorial, you’ll see a demonstration of how to use Programmable Voice to make a voice call directly from the Twilio CLI

Prerequisites

  • A free Twilio account (register here and receive $10 in Twilio credit!)
  • Node.js installed on your machine

Set up your environment

Your first step is to make sure you have the Twilio CLI installed on your computer. You can use npm or another package manager to install it. From your terminal or command prompt window, run the following command:

npm install twilio-cli -g

Once the CLI is installed, you can login by running the command:

twilio login

This will prompt you for your Account SID and your Auth Token, both of which are available on the dashboard of the Twilio Console.

Screenshot of terminal showing twilio login command

After logging in, you’re ready to get started!

Buy a Twilio phone number from the CLI

You can do a lot with the Twilio CLI. To explore, from your terminal, run the following command:

twilio

This will generate a list of all the topics available in the Twilio CLI.

Now, run the command twilio api:core to see all the core API tools available through the CLI. It’s these tools you’ll be focusing on in this article.

To buy a phone number, you’ll have to complete two steps. First, view a list of available phone numbers (feel free to change the country code to your specific country):

twilio api:core:available-phone-numbers:local:list  --country-code "US"

This will produce a long list of phone numbers. You can narrow this list by defining an area code as well:

twilio api:core:available-phone-numbers:local:list  --country-code "US" --area-code "503"

Select a preferred number from this list. To purchase it, copy the number and paste it into the following command:

twilio api:core:incoming-phone-numbers:create --phone-number "<PASTE-YOUR-NUMBER>"

Congratulations, you have a new Twilio phone number!

Create a TwiML Bin

TwiML is Twilio’s markup language, an XML document that’s been extended with tags defined by Twilio. When your Twilio phone number makes a call, Twilio looks up the URL associated with your number and sends a request to that URL. If your URL contains TwiML, Twilio will follow the instructions found in the document.

You can host TwiML directly through Twilio, in a TwiML Bin. Navigate to the TwiML Bins section of the Console.

Click the + button to create a new bin. This will direct you to a new page where you can configure your bin.

Screenshot of TwiML Bin configuration page

Give your bin any friendly name of your choosing, such as “cli-call”. Then copy and paste the following TwiML into the TwiML field, replacing anything there already:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say>Ahoy, friend!</Say>
</Response>

In a few moments, you’ll use the CLI to associate this TwiML with your new phone number. At that point, when your phone number makes a call, the receiver will hear “Ahoy, friend!” upon connection.

Scroll down and press the button that says Create. After clicking the Create button, the page will refresh and at the top of the page, you will see an SID and URL under the Properties heading. Copy the URL to the clipboard.

Make a phone call through the CLI

Head back to your terminal, you’re ready to make a call!

Run the following command, taking care to make the following changes:

  • Replace the --from value with your new Twilio phone number
  • Replace the --to value with your personal phone number
  • Replace the --url value with the URL you just copied from your TwiML bin
twilio api:core:calls:create \
--from "+15551235555" \
--to "+15554565555" \
--url "https://handler.twilio.com/twiml/XXXXXXXXX"

Be sure to use E.164 format for both numbers.

Your personal phone will ring in just a few moments. When you answer the call, you’ll hear “Ahoy, friend!”.

Great work, you’ve just learned how to make a phone call directly through your CLI. That means you can let your partner know you’re running late from work with just a few strokes of the keyboard. Is it easier than just sending a text? Who knows! But it sure is more fun.

Ashley is a JavaScript Editor for the Twilio blog. To work with her and bring your technical stories to Twilio, find her at @ahl389 on Twitter. If you can’t find her there, she’s probably on a patio somewhere having a cup of coffee (or glass of wine, depending on the time).