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 and Svelte.
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 .env
file. Note: You will need to configure the Voice "REQUEST URL" in your TwiML App later.
Purchase a Voice phone number. You will need this phone number in E.164 format for your .env
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 .env
file. Your API KEY is needed to create an Access Token.
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.example
and edit the .env
file with the configuration values from above.
cp .env.example .env
Install the dependencies.
npm install
Copy the twilio.min.js
file from your node_modules
to your public
directory. 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
ngrok
will assign a unique URL to your tunnel.
It might be something like https://asdf456.ngrok.io
. You will need this to configure your TwiML app in the next step.
Configure your TwiML app
/voice
appended to the end. (E.g: https://asdf456.ngrok.io/voice
) Note: You must use the https URL, otherwise some browsers will block
microphone access.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
.
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.
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.
.env
file as outlined in the Local Development steps.docker-compose up
.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 |
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.