Skip to content

Latest commit

 

History

History
151 lines (85 loc) · 4.49 KB

server-side.md

File metadata and controls

151 lines (85 loc) · 4.49 KB

Server-Side and Twilio Account Setup

1. Set up your Twilio Console


Prepare your Twilio Console by pinning the following products:

  • Voice
  • Phone Numbers
  • TwiML Bins
  • Functions

2. Buy a Twilio Phone Number



You'll use this for your outbound callerId. You can also use this as your inbound phone number.


3. Set up the server side requirements


3.1. Create a TwiML endpoint for outbound calls


<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say>Hello<Say>
  <Hangup />
</Response>
  • Copy the URL for this endpoint.

Pro Tip: Keep it simple (no <Dial yet) to help isolate any issues that may arise.

Note: The Voice JavaScript SDK Quickstarts show examples of using your own server to provide TwiML instructions to Twilio.


3.2. Create a TwiML App


  • Create a TwiML App in the Twilio Console and set the Voice Configuration URL to your TwiML endpoint's URL.

  • Copy the SID of this TwiML App. Paste it in a note file/sticky note.

For more detailed instructions, see the Node.js Quickstart Repo.


3.3. Generate an API Key and Secret


For more detailed instructions, see the Node.js Quickstart Repo.


3.4. Create and deploy an AccessToken endpoint


- Copy the contents of the `accessTokenEndpoint.js` file in this repo and deploy a public Twilio Function. You will need to include the **TwiML App SID**, **API Key and Secret**, and your **Twilio Account SID** in the Twilio Function's Environment Variables in order to generate AccessTokens. 
  • Copy the URL for this endpoint.

This example assumes that you'll pass the end user's identity as a query parameter when you make the request for the AccessToken.

For examples of creating AccessTokens in a different programming language, check out the Quickstarts.


3.5. Fetch an AccessToken


Before you can use the Voice JavaScript SDK, you'll need to fetch an AccessToken from your server-side endpoint. A good time to do this is when you'd make other API calls (i.e. once your "Phone" component has been mounted on the DOM).

**Pro Tip:** Before trying to use the SDK, debug your AccessToken on [jwt.io](https://jwt.io/) and verify that it contains all of the necessary information. [See the Access Tokens page for more information.](https://www.twilio.com/docs/iam/access-tokens#create-an-access-token-for-voice)

**Note on Regional:** If you're trying to use the JS SDK in a non-US region, read the [Build a web app using the JavaScript Voice SDK page ](https://www.twilio.com/docs/global-infrastructure/use-the-programmable-voice-javascript-sdk-with-a-non-us-twilio-region)first! 

4. Install, import, instantiate the JS SDK


4.1. Install the JavaScript SDK


  • Run the following command in your terminal in the root of your project.
npm install @twilio/voice-sdk –save

4.2. Import the Device class


  • Import the Device class into your JavaScript project:
import { Device } from '@twilio/voice-sdk';

4.3. Instantiate the Device


  • Create a new Device instance with your AccessToken. Add an 'error' event listener.
const device = new Device(accessToken);
device.addListener('error', (twilioError) = {
  console.log(`Twilio Device error: ${twilioError.message}`);
})

Note: Do not create more than one Device instance. Keep track of this one in a variable or in the component's state or your state management tool.


5. Move on to the "Make Outgoing Calls" guide