Twilio’s WebSocket offers an alternative transport protocol to Real-time Transport Protocol (RTP). The WebSocket carries Twilio’s bidirectional Media Streams that allow third parties to send and receive audio from Twilio. While we recommend WebRTC over WebSockets, by placing Daily Bots infrastructure close to Twilio’s Voice servers, the underlying issues of TCP are mitigated.

Alternatively, Twilio Voice users can connect to third-party over SIP. In this case, by using Twilio’s WebSockets, we have removed the additional latency hops to perform SIP interconnect.

Configuration on Daily Bots

You have two options to configure your Daily Bots: either through the dashboard or via the REST API.

Dashboard

To get started, create a Twilio Bot Configuration on the dashboard.

As part of the Bot set up, config needs to contain the LLM prompt, LLM settings and the corresponding text-to-speech voice settings.

This will return a Bot ID, a unique identifier that encapsulates the unique config for a Daily Bot that you can map to on Twilio via the TwiML.

REST API

Alternatively, you can use the REST API to create a Twilio Bot Configuration. This approach is particularly useful for automation and programmatic bot management.

First, create a new Twilio bot configuration by sending a POST request to the /twilio-config endpoint:

curl -X POST https://api.daily.co/v1/twilio-config \
-H "Authorization: Bearer $DAILY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
  "config": [
    {
      "options": [
        {
          "name": "params",
          "value": {
            "stop_secs": 0.3
          }
        }
      ],
      "service": "vad"
    },
    {
      "options": [
        {
          "name": "voice",
          "value": "79a125e8-cd45-4c13-8a67-188112f4dd22"
        },
        {
          "name": "model",
          "value": "sonic-english"
        },
        {
          "name": "language",
          "value": "en"
        }
      ],
      "service": "tts"
    },
    {
      "options": [
        {
          "name": "model",
          "value": "claude-3-5-sonnet-latest"
        },
        {
          "name": "initial_messages",
          "value": [
            {
              "role": "user",
              "content": [
                {
                  "text": "You are a helpful assistant.",
                  "type": "text"
                }
              ]
            }
          ]
        }
      ],
      "service": "llm"
    }
  ],
  "services": {
    "llm": "anthropic",
    "stt": "deepgram",
    "tts": "cartesia"
  },
  "bot_profile": "twilio_ws_voice_2024_09",
  "max_duration": 300
}'

The API will respond with configuration details including a twiml field that contains the TwiML needed for your Twilio configuration:

{
  "id": "93975006-3cbe-4706-933d-35f33e321e51",
  "wssUrl": "wss://ws-twilio.staging.daily.co/ws",
  "domain": "pc-4542100a391f179218887fc136188b2c",
  "twiml": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response><Connect><Stream url=\"wss://ws-twilio.staging.daily.co/ws\"><Parameter name=\"DailyDomain\" value=\"pc-b7cf7f48d0585b363ccfc7a7c1df7f23\" /><Parameter name=\"DailyBotsId\" value=\"a4840aeb-4cf3-4046-9855-de7159bbf788\" /></Stream></Connect><Pause length=\"40\"/></Response>",
  "botConfig": {
    // ... configuration details
  }
}

Copy the twiml value from the response - you’ll need this for the Twilio console setup described in the next section.

You can also manage your configurations using additional endpoints. See the API reference for more information.

Configuration on Twilio

Now head over to the Twilio console to create a TwiML Bin. TwiML Bins are Twilio-hosted instructions for your Twilio phone number.

In your TwiML Bin, you’ll see a text area called TWIML. In this text area, paste the TwiML created for your Daily Bot. You can find this TwiML in the Daily Bots dashboard > Phones > Twilio bot configuration.

The TwiML contains the Stream URL, i.e., the WebSocket gateway for Daily’s Twilio Transport and Daily-specific <Parameters> that are used to identify your account and your unique Bot ID.

Once the TwiML bin is stored, you can assign this TwiML to your phone number in the Twilio console.

You are now ready to test dial-in and dial-out with Daily Bots and your Twilio phone number using Twilio’s WebSocket Transport.

To dial in, just dial the Twilio phone number and it will connect you to the bot. To dial out, you can call the Twilio REST API with To, From and the TwiML Bin url. For example the curl command is:

curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Calls.json" \
--data-urlencode "Url=$TWIML_BIN_URL" \
--data-urlencode "To=+15558675310" \
--data-urlencode "From=+15552223214" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

or use the twilio-cli:

twilio api:core:calls:create --from="+15552223214" --to="+15558675310" --url="$TWIML_BIN_URL"