This repo is a customization of Twilio Voice JavaScript SDK Quickstart for Node.js to support the following added features:
- Region Selection
- Join/Leave Voice Queue (with timeout countdown)
- Basic IVR Menu for Queue Selection
You will need to update the .env file with the TWILIO_<region>_TWIML_[APP_SID|API_KEY|SECRET]values for each region you want to support. For example, if you only want to support the default US region, you will need to add the following to your .env file:
TWILIO_US_TWIML_APP_SID=APXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
TWILIO_US_API_KEY=SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
TWILIO_US_API_SECRET=XXXXXXXXXXXXXXXXXThis is in addition to the standard TWILIO_ACCOUNT_SIDand TWILIO_CALLER_ID values. No need to set TWILIO_SECRET (aka auth token), as it's only used by commented-out code relating to unfinished conference logic.
A TwiML App SID is needed for the outgoingApplicationSid in the Voice Grant - and will be invoked for outbound calls from the Voice Client. The API Key and Secret are needed to create an Access Token. See Create an Access Token for Voice. TwiML Apps and API Keys only exist in the Twilio Region in which they are defined, and as such we need to define them for each region we want to support. See Using the Twilio REST API in a non-US Region.
In public\quickstart.js you may want to update the queueTimeout variable.
callQueueButton.onclick = (e) => {
e.preventDefault();
var queue = queueInput.value;
// Max timeout for <Dial><Queue> is 600 seconds
var queueTimeout = 600;
makeOutgoingCallToQueue(queue, queueTimeout);
};Once agent (aka voice client) is dialed into the voice queue, Programmable Voice will wait for a configurable timeout (default 30s), before disconnecting. We opted for the maximum timeout of 600s here, but you may want to adjust this value to suit your needs.
When viewing products via the Console, use the arrow to pin the region that you need. This will give you an area in Console exclusively containing that region’s pinned products - allowing you to segregate all things US1 from say IE1. NOTE: This also applies to viewing logs via the Monitor tab.
For Phone Numbers, by default, your non-US1 regions will show as "Routed to United States (US1)" for the Voice handling. To route to your newly selected region, you need to select each number, ensure the Voice handler endpoint is correct, and then click the Re-Route button. This will tell Twilio to handle all calls to that number via the Twilio Region you have selected.
Refer to Set a phone number's inbound processing Region using the Console for more detail.
All of this can be scripted using the REST APIs too, but bear in mind that everything you do via the REST API needs to use a regional URL. See Using the Twilio REST API in a non-US Region
e.g. https://api.dublin.ie1.twilio.com/2010-04-01/Accounts/<AccountSid>/Queues
Refer to Set a phone number's inbound processing Region using the REST API for all the details.
- Joining an Empty Queue in IE1 Fires an Accepted Event
- When joining an empty queue in IE1, the
acceptedevent is fired - which leads to the timeout countdown logic terminating. In US1, theacceptedevent is not fired until an enqueued call is bridged to the voice client call, and so the timeout countdown logic continues as expected. Functionally, this is not an issue as the calls are still successfully bridged in both regions, but it is a bit confusing.
- When joining an empty queue in IE1, the
-
Transfer to Voice Queue
- Basic cold transfers could be achieved by executing new
<Dial>TwiML on the customer call SID. That might be to dial another PSTN number, a SIP address, or another Voice Queue (if transferring internally). All of these would terminate the call with the current voice client.
- Basic cold transfers could be achieved by executing new
-
Conference
- For 3+ participant calls (e.g. warm transfers) - this could be done also when an enqueued call is bridged to a queued voice client (similar to what Flex/Taskrouter does upon task acceptance). i.e. execute
<Dial><Conference>on the customer call SID first, then<Dial>in the voice client via a new call.
- For 3+ participant calls (e.g. warm transfers) - this could be done also when an enqueued call is bridged to a queued voice client (similar to what Flex/Taskrouter does upon task acceptance). i.e. execute
Below is the original README.md file from the Twilio Voice JavaScript SDK Quickstart for Node.js repository. There may be some configuration items that are superceded by the regional configuration items above.
This template is part of Twilio CodeExchange. If you encounter any issues with this code, please open an issue at github.com/twilio-labs/code-exchange/issues.
This application should give you a ready-made starting point for writing your own voice apps with Twilio Voice JavaScript SDK 2.0 (Formerly known as Twilio Client).
This application is built in Node.
Implementations in other languages:
| .NET | Java | Python | PHP | Ruby |
|---|---|---|---|---|
| Done | Done | Done | Done | Done |
- Nodejs version 14.0 or above.
- ngrok - this is used to expose your local development server to the internet. For more information, read this Twilio blog post.
- A WebRTC enabled browser (Google Chrome or Mozilla Firefox are recommended). Edge and Internet Explorer will not work for testing.
-
Create a TwiML Application in the Twilio Console. Once you create the TwiML Application, click on it in your list of TwiML Apps to find the TwiML App SID. You will need this SID for your
.envfile. Note: You will need to configure the Voice "REQUEST URL" in your TwiML App later.- For detailed instructions with screenshots, see the Create a TwiML App.md file
-
Purchase a Voice phone number. You will need this phone number in E.164 format for your
.envfile.- For detailed instructions with screenshots, see the Buy a Phone Number.md file
-
Create an API Key in the Twilio Console. Keep the API Key SID and the API Secret in a safe place, since you will need them for your
.envfile. Your API KEY is needed to create an Access Token.- For detailed instructions with screenshots, see the Create an API Key.md file
Before we begin local development, we need to collect all the config values we need to run the application.
| Config Value | Description |
|---|---|
TWILIO_ACCOUNT_SID |
Your primary Twilio account identifier - find this in the console here. |
TWILIO_TWIML_APP_SID |
The SID of the TwiML App you created in step 1 above. Find the SID in the console here. |
TWILIO_CALLER_ID |
Your Twilio phone number in E.164 format - you can find your number here |
TWILIO_API_KEY / TWILIO_API_SECRET |
The TWILIO_API_KEY is the API Key SID you created in step 3 above, and the TWILIO_API_SECRET is the secret associated with that key. |
-
First clone this repository and cd into it:
git clone https://github.com/TwilioDevEd/voice-javascript-sdk-quickstart-node.git cd voice-javascript-sdk-quickstart-node -
Create a configuration file for your application by copying the
.env.exampleand edit the.envfile with the configuration values from above.cp .env.example .env
-
Install the dependencies.
npm install
-
Copy the
twilio.min.jsfile from yournode_modulesto yourpublicdirectory. Run the following from the root directory of your project:cp node_modules/@twilio/voice-sdk/dist/twilio.min.js public
Note: In order to keep this quickstart as simple as possible, this step is used to avoid any need for build tools like Webpack.
-
Launch local development web server.
npm start
-
Navigate to http://localhost:3000 in your browser.
-
Expose your application to the wider internet using
ngrok. This step is crucial for the app to work as expected.ngrok http 3000
-
ngrokwill assign a unique URL to your tunnel. It might be something likehttps://asdf456.ngrok.io. You will need this to configure your TwiML app in the next step. -
Configure your TwiML app
- In the Twilio Console, navigate to Programmable Voice > TwiML > TwiML Apps
- Select the TwiML App you created earlier
- On your TwiML App's information page, find the 'Voice Configuration' section.
- Change the Request URL to your ngrok url with
/voiceappended to the end. (E.g:https://asdf456.ngrok.io/voice) Note: You must use the https URL, otherwise some browsers will block microphone access. - Click the 'Save' button.
You should now be ready to make and receive calls from your browser.
When you navigate to localhost:3000, you should see the web application containing a 'Start up the Device' button. Click this button to initialize a Twilio.Device.
When the Twilio.Device is initialized, you will be assigned a random "client name", which will appear in the 'Device Info' column on the left side of the page. This client name is used as the identity field when generating an Access Token for the Twilio.Device, and is also used to route SDK-to-SDK calls to the correct Twilio.Device.
- Under 'Make a Call', enter a phone number in E.164 format and press the 'Call' button
Open two browser windows to localhost:3000 and click 'Start up the Device' button in both windows. You should see a different client name in each window.
Enter one client's name in the other client's 'Make a Call' input and press the 'Call' button.
You will first need to configure your Twilio Voice Phone Number to use the TwiML App we created earlier. This tells Twilio how to handle an incoming call directed to your Twilio Voice Number.
- Log in to your Twilio Console
- Navigate to your Active Numbers list
- Click on the number you purchased earlier
- Scroll down to find the 'Voice & Fax' section and look for 'CONFIGURE WITH'
- Select 'TwiML' App
- Under 'TWIML APP', choose the TwiML App you created earlier.
- Click the 'Save' button at the bottom of the browser window.
You can now call your Twilio Voice Phone Number from your cell or landline phone.
Note: Since this is a quickstart with limited functionality, incoming calls will only be routed to your most recently-created Twilio.Device.
If you see "Unknown Audio Output Device 1" in the "Ringtone" or "Speaker" devices lists, click the button below the boxes (Seeing "Unknown" Devices?) to have your browser identify your input and output devices.
If you have Docker already installed on your machine, you can use our docker-compose.yml to setup your project.
- Make sure you have the project cloned.
- Setup the
.envfile as outlined in the Local Development steps. - Run
docker-compose up. - Follow the steps in Local Development on how to expose your port to Twilio using a tool like ngrok and configure the remaining parts of your application.
In addition to trying out this application locally, you can deploy it to a variety of host services. Heroku is one option, linked below.
Please be aware that some of these services may charge you for the usage and/or might make the source code for this application visible to the public. When in doubt, research the respective hosting service first.
| Service | |
|---|---|
| Heroku |
- The CodeExchange repository can be found here.
This template is open source and welcomes contributions. All contributions are subject to our Code of Conduct.
No warranty expressed or implied. Software is as is.






