Twilio's JavaScript Voice SDK
npm install @twilio/voice-sdk@twilio/voice-sdk
=================
 
Twilio's Voice SDK allows you to add real-time voice and PSTN calling to your web apps.
* API Docs
* More Docs
* Quickstart
* Reference Components
* Changelog
Installation
------------
We recommend using npm to add the Voice SDK as a dependency.
```
npm install @twilio/voice-sdk --save
Using this method, you can import the Voice SDK using ES Module or TypeScript syntax:
`js
import { Device } from '@twilio/voice-sdk';
`
Or using CommonJS:
`js`
const Device = require('@twilio/voice-sdk').Device;
Although we recommend using npm to add the Voice SDK as a dependency, you can also get the Twilio Voice SDK code
from GitHub and include it in your project directly. To do so, navigate to
"Tags" and find the most recent release, or
the particular release version you'd like to use.
> Note: releases tagged with "-rc" are "Release Candidate" versions
> and are still being tested. Unless you specifically know that you want to use a release candidate
> version, you should not use a release with "-rc" in the name.
Download either the zip or the tar.gz and then extract the files. For example, if you downloaded the2.0.0
tarball for the release, you could then extract the files with the tar command:
``
tar -xvzf twilio-voice.js-2.0.0.tar.gz
cd twilio-voice.js-2.0.0
Once you've extracted the folder, the twilio.js and twilio.min.js files that/dist
you can include in your project will be in the directory. twilio.min.js is the
minified version of the code.
You can copy either the twilio.js or the twilio.min.js file into your project and
then provide a link to it in your html. For example:
``
Using this method, you can access the SDK through the browser global:
``
const Device = Twilio.Device;
Testing
-------
Running unit tests requires no setup aside from installation (above). You can run unit tests via:
``
npm run test:unit
Integration tests require some set up:
1. Create a TwiML App with the friendly name Integration Tests, using the TwiML code below.
`xml`
2. Create a second TwiML App with the friendly name STIR/SHAKEN, using the TwiML code below. Be sure to use your Twilio number for the Dial verb.
`xml`
3. Cypress requires env vars to be prefixed with CYPRESS_. Make a copy the example.env file with the name .env and populate the file with your credentials.
4. Start the relay server
``
npm run test:relay-server
5. Integration tests run via Cypress, and can be run via:
``
npm run test:integration:chrome
npm run test:integration:firefox
Content Security Policy (CSP)
----------------------------
Use the following policy directives to enable CSP that is compatible with twilio-voice.js.
``
script-src https://media.twiliocdn.com https://sdk.twilio.com
media-src mediastream: https://media.twiliocdn.com https://sdk.twilio.com
connect-src https://eventgw.twilio.com wss://voice-js.roaming.twilio.com https://media.twiliocdn.com https://sdk.twilio.com
If you are providing a non-default value for Device.ConnectOptions.edge parameter, you need to add the Signaling URI wss://voice-js.{edgeId}.twilio.com in your connect-src directive where edgeId is the Edge ID as defined in this page. See examples below.
If Device.ConnectOptions.edge is ashburn
``
connect-src https://eventgw.twilio.com https://media.twiliocdn.com https://sdk.twilio.com wss://voice-js.ashburn.twilio.com
If Device.ConnectOptions.edge is ['ashburn', 'sydney', 'roaming']
``
connect-src https://eventgw.twilio.com https://media.twiliocdn.com https://sdk.twilio.com wss://voice-js.ashburn.twilio.com wss://voice-js.sydney.twilio.com wss://voice-js.roaming.twilio.com
If you are providing a home region grant into your Twilio access token, you need to add the insights endpoint in your connect-src directive using eventgw.{homeRegion}.twilio.com format. Below is an example if your home region grant is sg1.
```
connect-src https://eventgw.sg1.twilio.com wss://voice-js.roaming.twilio.com https://media.twiliocdn.com https://sdk.twilio.com
License
-------
See LICENSE.md