Create and configure SIP trunk

Step-by-step instructions for creating inbound and outbound SIP trunks using Telnyx or Twilio.

The general steps for creating a SIP trunk for LiveKit telephony apps are as follows (exact steps vary from provider to provider):

  1. Purchase a phone number from your provider.
  2. Create a SIP trunk for your phone number.
  3. Associate your LiveKit SIP URI with your SIP trunk.
  4. Associate a phone number with your SIP trunk.

After you create a SIP trunk with your provider, you can create inbound and outbound LiveKit trunks to accept and make phone calls using an AI voice agent.

note

If you're using LiveKit Cloud as your SIP server and you're signed in, your SIP URI is automatically included in the code blocks where appropriate.

Use the following steps for either Twilio or Telnyx to create and configure a SIP trunk.

Creating a Twilio SIP trunk using the CLI

Create a Twilio SIP trunk for incoming or outgoing calls, or both, using the following steps using the CLI. To use the Twilio console, see Configure a SIP trunk using the Twilio or Telnyx UI.

Prerequisites

Step 1. Create a SIP trunk

The domain name for your SIP trunk must end in pstn.twilio.com. For example to create a trunk named My test trunk with the domain name my-test-trunk.pstn.twilio.com, run the following command:

twilio api trunking v1 trunks create \
--friendly-name "My test trunk" \
--domain-name "my-test-trunk.pstn.twilio.com"

The output includes the trunk SID. Copy it for use in the following steps.

Step 2: Configure your trunk

Configure the trunk for inbound calls or outbound calls or both. To create a SIP trunk for both inbound and outbound calls, run all the commands in both tabs:

For inbound trunks, configure an origination URI. If you're using LiveKit Cloud and are signed in, your <your SIP host> is filled in below:

twilio api trunking v1 trunks origination-urls create \
--trunk-sid <twilio_trunk_sid> \
--friendly-name "LiveKit SIP URI" \
--sip-url "sip:<your SIP host>" \
--weight 1 --priority 1 --enabled

Step 3: Associate phone number and trunk

The Twilio trunk SID and phone number SID are included in the output of previous steps. If you didn't copy the SIDs, you can list them using the following commands:

  • To list phone numbers: twilio phone-numbers list
  • To list trunks: twilio api trunking v1 trunks list
twilio api trunking v1 trunks phone-numbers create \
--trunk-sid <twilio_trunk_sid> \
--phone-number-sid <twilio_phone_number_sid>

Creating a Telnyx SIP trunk using the API

You can use curl command to make calls to the Telnyx API V2. The commands in the steps below use the example phone number, +15105550100. To use the Telnyx console, see Configure a SIP trunk using the Twilio or Telnyx UI.

Prerequisite

Purchase a Telnyx phone number.

Step 1: Create an environment variable for API key

If you don't have a key a Telnyx API V2 key, see the Telnyx guide to create one.

export TELNYX_API_KEY="<your_api_v2_key>"

Step 2: Create an FQDN connection

The following inbound and outbound commands include the required configuration settings if you plan on using only an inbound or outbound trunk for your LiveKit telephony app. However, by default, an FQDN connection creates both an inbound and outbound trunk.

  1. Creating an FQDN connection. Depending on your use case, select Inbound, Outbound, or Inbound and outbound to accept calls, make calls, or both:

    Set the caller's number format to +E.164 for inbound calls (this identifies the caller's number with a leading +):

    curl -L 'https://api.telnyx.com/v2/fqdn_connections' \
    -H 'Content-Type: application/json' \
    -H 'Accept: application/json' \
    -H "Authorization: Bearer $TELNYX_API_KEY" \
    -d '{
    "active": true,
    "anchorsite_override": "Latency",
    "connection_name": "My LiveKit trunk",
    "transport_protocol": "UDP",
    "inbound": {
    "ani_number_format": "+E.164",
    "dnis_number_format": "+e164"
    }
    }'
  2. Copy the FQDN connection ID from the output:

    {
    "data": {
    "id":"<connection_id>",
    ...
    }
    }
  3. Create an FQDN with your SIP URI and your FQDN connection ID:

    curl -L 'https://api.telnyx.com/v2/fqdns' \
    -H 'Content-Type: application/json' \
    -H 'Accept: application/json' \
    -H "Authorization: Bearer $TELNYX_API_KEY" \
    -d '{
    "connection_id": "<connection_id>",
    "fqdn": "<your SIP host>",
    "port": 5060,
    "dns_record_type": "a"
    }'

Step 3: Associate phone number and trunk

  1. Get the phone number ID for phone number 5105550100:

    curl -L -g 'https://api.telnyx.com/v2/phone_numbers?filter[phone_number]=5105550100' \
    -H 'Accept: application/json' \
    -H "Authorization: Bearer $TELNYX_API_KEY"

    Copy the phone number ID from the output:

    {
    "meta": {
    "total_pages": 1,
    "total_results": 1,
    "page_number": 1,
    "page_size": 100
    },
    "data": [
    {
    "id": "<phone_number_id>",
    ...
    }
    ]
    }
  2. Add the FQDN connection to the phone number:

    curl -L -X PATCH 'https://api.telnyx.com/v2/phone_numbers/<phone_number_id>' \
    -H 'Content-Type: application/json' \
    -H 'Accept: application/json' \
    -H "Authorization: Bearer $TELNYX_API_KEY" \
    -d '{
    "id": "<phone_number_id>",
    "connection_id": "<connection_id>"
    }'

Configure a SIP trunk using the Twilio or Telnyx UI

  1. Sign in to the Twilio console.

  2. Purchase a phone number.

  3. Create SIP Trunk on Twilio:

    • Select Elastic SIP Trunking » Manage » Trunks.
    • Create a SIP trunk.
    tip

    Using your Twilio API key, you can skip the next two steps by using this snippet to set your origination and termination URLs automatically.

  4. For inbound calls:

    • Navigate to Voice » Manage » Origination connection policy, and create an Origination Connection Policy
    • Select the policy you just created and set the Origination SIP URI to sip:<your SIP URI>. For example, if your SIP URI is 1abcdefgh2i.sip.livekit.cloud then enter sip:1abcdefgh2i.sip.livekit.cloud.
  5. For outbound calls, configure termination and authentication:

    • Navigate to Elastic SIP Trunking » Manage » Trunks.
    • Copy the Termination SIP URI to use in a following step.
    • Configure Authentication: Select Elastic SIP Trunking » Manage » Credential lists and create a new credential list with a username and password.

Next steps