Menu

Expand
Rate this page:

Twilio Voice JavaScript SDK Serverless Quickstart

The Twilio Client JavaScript SDK has been renamed to Twilio Voice JavaScript SDK.

This quickstart is for using Twilio on the client side in the browser, or front-end. If you are looking to use Twilio with JavaScript on the server side, please head over to our Node.js documentation.

This Quickstart uses Twilio's Voice JavaScript SDK version 1.14. We are currently working to migrate this serverless Quickstart to version 2.0.

If you are looking for Voice JavaScript SDK version 2.0 Quickstarts, choose your preferred language/framework below:

Ready to add voice communications to your front-end web applications? When you complete this Quickstart, you’ll have a fully running application that can make and receive phone calls from the comfort of your own web browser.

There are a few steps we’re going to need to complete to get you up and running.

Install the Twilio CLI

The Twilio Command Line Interface, CLI, allows you to interact with the Twilio API from your terminal. It allows you to manage and configure all of your Twilio tools, which is what we are going to be using it for in our case.

The suggested way to install twilio-cli on macOS is to use Homebrew. If you don’t already have it installed, visit the Homebrew site for installation instructions and then return here.

Once you have installed Homebrew, run the following command to install twilio-cli:

brew tap twilio/brew && brew install twilio

The suggested way to install twilio-cli is by using Scoop, a command-line installer for Windows. If you don’t already have it installed, visit the Scoop site for installation instructions and then return here.

Note PowerShell will need to be run as an administrator to avoid common permission issues when installing via Scoop.

  1. Add the twilio-cli Bucket:
    scoop bucket add twilio-scoop https://github.com/twilio/scoop-twilio-cli
  2. Install the app:
    scoop install twilio​

twilio-cli can be installed using the Advanced Package Tool (apt) on most distributions such as Debian, Ubuntu, and Mint.

To do so, run the following commands in your terminal:

wget -qO- https://twilio-cli-prod.s3.amazonaws.com/twilio_pub.asc \
  | sudo apt-key add -
sudo touch /etc/apt/sources.list.d/twilio.list
echo 'deb https://twilio-cli-prod.s3.amazonaws.com/apt/ /' \
  | sudo tee /etc/apt/sources.list.d/twilio.list
sudo apt update
sudo apt install -y twilio

For other installation methods, see the Twilio CLI Quickstart.

Install the Serverless Plugin

The Twilio CLI supports plugins, which give you additional control. We’re going to install the serverless plugin which allows you to seamlessly deploy functions from your local machine to our Serverless framework.

twilio plugins:install @twilio-labs/plugin-serverless

Create your Application using a Function Template

The serverless plugin allows you to initialize an application from a template. The template we are going to use has everything we need, a front-end application, a Twilio Function to generate AccessTokens, a Twilio Function to handle incoming calls and route them to your browser, an admin section to wire up your account, and much more!

We’ll take a look in more detail after we get things up and running. But for now, let’s create our app!

twilio serverless:init example-client-app --template="voice-client-javascript"

This will create a new folder called example-client-app that will contain all of our code.

cd example-client-app

Deploy your Application

The serverless plugin allows you to deploy code from your local machine to the Twilio Serverless platform. Once deployed you’ll be able to access your hosted application.

twilio serverless:deploy

This command will create a Service for you that will house your hosted development environment. The command will output all the Twilio Functions and Assets that have been deployed to your dev environment.

Note that the URLs are unique to your instance.

You should run this command when you want to make changes to your hosted live development environment.

Initialize your Environment

When you deployed your application, you were presented with a list of URLs. Make note of the prefix, this is custom to your deployment.

  1. Open the admin page in your browser. It is the URL that ends with /admin/index.html
  2. The password is default (You can change this, we'll do that later)
  3. Click the button on the admin page to initialize your environment

The initialization process will create and wire up all the necessary tools to make browser based calling work. You definitely could’ve done this yourself, but this is a Quickstart, and we want you to get started quickly 😉

This page will now host a checklist that will validate that your environment is working properly. It also provides handy links to get to the items that were automatically initialized for you.

You should note that there is a failing check, and that’s because we haven’t yet changed the default password. We’ll do that here shortly, but first let’s explore your new application.

Explore your application

Your example application is now up and running at /index.html. Open it up in your browser.

You’ll see that an Access Token is requested, and you should see a notification when it receives one. It will state your "Twilio.Device is ready!"

Go ahead and use the interface to give yourself a call. You should be asked to grant access to your microphone and speakers.

Note that the example application is using the user name of the_user_id. The Twilio Function we are using to generate the Access Token is not using any sort of authentication check. This is something that you will need to implement with your user management system.

Modify your Application

We should definitely change that admin password. On your local machine edit the file named .env.

There is an entry for ADMIN_PASSWORD, change that to something other than default.

ADMIN_PASSWORD=12345

Obviously, though, don’t use that password (that’s the same one as my luggage.)

Now make sure your file is saved and then deploy.

twilio serverless:deploy

And after it is deployed revisit your /admin/index.html page, use your new password, and you will see that your checks are now all green. You did it!

Anytime you make a change to your example application, remember to save and re-deploy.

Learn more

Now that you’ve seen things working, we’d like to invite you to explore how it’s working.

First head over to your local folder and take a look at the two client side files assets/index.html and assets/quickstart.js. Note that there is a script tag including a CDN hosted file twilio.js. This is the Twilio Voice JavaScript SDK and you can see it in use in the heavily documented assets/quickstart.js.

In the quickstart.js file, you’ll notice that the Device is initialized with a token. It’s pointing to a serverless Function that can be found at /functions/voice-token.js. This file mints an Access Token for you based on a REST API Key and Secret that was generated during initialization. You’ll see that it also contains a VoiceGrant that enables incoming and outgoing calls through a specific TwiML Application.

If you head back over to your admin page, /admin/index.html, you’ll see a link to a TwiML Application that was created for you. A TwiML Application allows you to specify what happens when a call is placed and when one is received. TwiML is the Twilio Markup Language that instructs Twilio what to do with your call in that moment.

We provide this TwiML via a Function that can be found in your functions/client-voice-twiml-app.js. We wired up your TwiML application to run this function on Incoming and Outgoing Voice calls. This function can dial either a client in your same application or a number. When making the outbound call it will use your CALLER_ID.

Continue your exploration

Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

Thank you for your feedback!

Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

Sending your feedback...
🎉 Thank you for your feedback!
Something went wrong. Please try again.

Thanks for your feedback!

Refer us and get $10 in 3 simple steps!

Step 1

Get link

Get a free personal referral link here

Step 2

Give $10

Your user signs up and upgrade using link

Step 3

Get $10

1,250 free SMSes
OR 1,000 free voice mins
OR 12,000 chats
OR more