Skip to contentSkip to navigationSkip to topbar
Page tools
Useful for sharing or LLM

On this page
Looking for more inspiration?Visit the

Network Traversal Service


To help deploy reliable Web Real-Time Communication (WebRTC)(link takes you to an external page) apps, Twilio offers a global Session Traversal Utilities for NAT (STUN) and Traversal Using Relays around NAT (TURN) service: the Network Traversal Service (NTS). NTS improves your user's connection to your WebRTC and Voice over Internet Protocol (VoIP)(link takes you to an external page) apps. This service works independent of Twilio Regions and routes calls to the nearest geographic server.

If you're new to STUN/TURN and ICE, see the Frequently Asked Questions.


Use the Network Traversal Service in a WebRTC app

list-post-example page anchor

To use Network Traversal Service in a WebRTC app, request a token and pass it to your RTCPeerConnection constructor. The NTS token requires your Twilio Account SID and Auth Token. To keep your Twilio account credentials safe, you should only make this request from your server, not the client browser.

  1. Request a Network Traversal Service Token from your web server.

  2. Pass the token to your WebRTC app.

    Generate NTS TokenLink to code sample: Generate NTS Token
    1
    // Download the helper library from https://www.twilio.com/docs/node/install
    2
    const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
    3
    4
    // Find your Account SID and Auth Token at twilio.com/console
    5
    // and set the environment variables. See http://twil.io/secure
    6
    const accountSid = process.env.TWILIO_ACCOUNT_SID;
    7
    const authToken = process.env.TWILIO_AUTH_TOKEN;
    8
    const client = twilio(accountSid, authToken);
    9
    10
    async function createToken() {
    11
    const token = await client.tokens.create();
    12
    13
    console.log(token.accountSid);
    14
    }
    15
    16
    createToken();

    Response

    Note about this response
    1
    {
    2
    "username": "dc2d2894d5a9023620c467b0e71cfa6a35457e6679785ed6ae9856fe5bdfa269",
    3
    "ice_servers": [
    4
    {
    5
    "urls": "stun:global.stun.twilio.com:3478"
    6
    },
    7
    {
    8
    "username": "dc2d2894d5a9023620c467b0e71cfa6a35457e6679785ed6ae9856fe5bdfa269",
    9
    "credential": "tE2DajzSJwnsSbc123",
    10
    "urls": "turn:global.turn.twilio.com:3478?transport=udp"
    11
    },
    12
    {
    13
    "username": "dc2d2894d5a9023620c467b0e71cfa6a35457e6679785ed6ae9856fe5bdfa269",
    14
    "credential": "tE2DajzSJwnsSbc123",
    15
    "urls": "turn:global.turn.twilio.com:3478?transport=tcp"
    16
    },
    17
    {
    18
    "username": "dc2d2894d5a9023620c467b0e71cfa6a35457e6679785ed6ae9856fe5bdfa269",
    19
    "credential": "tE2DajzSJwnsSbc123",
    20
    "urls": "turn:global.turn.twilio.com:443?transport=tcp"
    21
    }
    22
    ],
    23
    "date_updated": "Fri, 01 May 2020 01:42:57 +0000",
    24
    "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    25
    "ttl": "86400",
    26
    "date_created": "Fri, 01 May 2020 01:42:57 +0000",
    27
    "password": "tE2DajzSJwnsSbc123"
    28
    }
  3. Include the ice_servers property in the RTCConfiguration object you pass to the RTCPeerConnection constructor when setting up a call. The ICE_SERVERS contains the contents of the ice_servers property returned as a response in the previous example.

    1
    // Here's an example in javaScript
    2
    myIceServers = ICE_SERVERS;
    3
    var configuration = { iceServers: myIceServers };
    4
    var pc = new RTCPeerConnection(configuration);

    If the browser you're using supports Trickle ICE(link takes you to an external page), pass any ICE candidates to the connected peer from the RTCPeerConnection onIceCandidate event:

    1
    // Here's an example in javaScript
    2
    pc.onicecandidate = function (evt) {
    3
    if (evt.candidate) {
    4
    // Send the candidate to the other party via your signaling channel
    5
    }
    6
    };

From this point, exchange Session Description Protocol (SDP) answers as you normally would.

If you need more help, see the FAQ or contact Twilio Support(link takes you to an external page).


Use the Network Traversal Service with the Twilio Programmable Voice SDKs

voice-sdk page anchor

To add voice-over-IP (VoIP) calling directly into your web and native mobile apps, use the Programmable Voice SDKs. The Programmable Voice SDKs build on top of WebRTC and can leverage STUN and TURN to traverse restrictive networks.

To learn how to use the Network Traversal Service tokens with Programmable Voice SDKs, see the Programmable Voice SDK network connectivity requirements.


Use the Network Traversal Service in a VoIP client

voip-client page anchor

To use Network Traversal Service with a VoIP Client, follow these steps:

  1. Retrieve a Network Traversal Service Token.
    To use Network Traversal Service in a VoIP client, retrieve a new ephemeral token through an HTTP request from your VoIP client or another app every 24 hours.
  2. From the returned data, extract the STUN and TURN URLs from the returned data, along with the username and credential values.
  3. Configure your VoIP client with these values.

Unless you lower the duration, Network Traversal Service Tokens last 24 hours. To lower the token expiration period, set the Ttl parameter with your token request. To learn more, see the Network Traversal Service Token documentation.