Hello, I would like to connect a LiveKit agent wit...
# ask-ai
m
Hello, I would like to connect a LiveKit agent with Twilio for handling both inbound and outbound calls. Could you please share some documentation or video tutorials that I can follow to set this up easily?
t
Thanks for reaching out, Yashish. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You can connect a LiveKit agent with Twilio for both inbound and outbound calls by integrating Twilio SIP trunks or using Twilio Programmable Voice with LiveKit. Here is a concise setup guide extracted from the official documentation: β€’ Elastic SIP Trunking (Recommended): Set up a Twilio SIP trunk, configure origination URI, and associate your phone number with the trunk to route calls into LiveKit. This approach provides robust SIP features and is generally preferred for full telephony integration. β€’ Twilio Programmable Voice: Use a TwiML Bin to route calls to your LiveKit SIP endpoint. This is simpler but doesn’t support full SIP functionality like REFER. LiveKit Side: After setting up Twilio, create inbound trunks and dispatch rules within LiveKit to handle the distribution of calls and joining agents as participants. Example configuration and CLI commands are available in the LiveKit docs. Example Projects & Automation: Official sample projects include scripts to automate the Twilio and LiveKit setup. Key environment variables (such as
LIVEKIT_API_KEY
,
TWILIO_PHONE_NUMBER
, etc.) are required for a quick start and seamless integration. For a complete step-by-step guide, configuration samples, and example projects, please refer to the official documentation and samples cited here. This will walk you through both Twilio and LiveKit setup, including all required commands and environment variable setups. If you need a specific workflow, configuration detail, or troubleshooting steps for any particular part of the setup, let me know and I can provide targeted guidance. Sources: Inbound calls with Twilio Voice | LiveKit Docs | .env.example | Integrating with Telephony | LiveKit Docs | Create and configure a Twilio SIP trunk | LiveKit Docs
m
can you provide all steps
t
Hey there! I'm excited to walk you through connecting a LiveKit agent with Twilio for your calls. Don't worry, I'll break this down into super manageable steps that'll make the whole process feel like a breeze. 🌟 1. Twilio Setup β—¦ Elastic SIP Trunking (Recommended) i. First things first, hop into Twilio and create a new SIP Trunk. Just make sure your domain name ends with
<http://pstn.twilio.com|pstn.twilio.com>
.
twilio api trunking v1 trunks create --friendly-name "My test trunk" --domain-name "<http://my-test-trunk.pstn.twilio.com|my-test-trunk.pstn.twilio.com>"
ii. Next, let's set up an Origination URI that points to your LiveKit SIP host. Think of it like giving Twilio a direct route to your system.
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
iii. Now, connect your Twilio number to your SIP trunk. It's like introducing two friends who are perfect for each other! πŸ‘‹
twilio api trunking v1 trunks phone-numbers create --trunk-sid <twilio_trunk_sid> --phone-number-sid <twilio_phone_number_sid>
β—¦ Twilio Programmable Voice (Alternative) i. Create a TwiML Bin in your Twilio Console with the following content (replace placeholders):
Copy code
<Response>
  <Dial>
    <Sip username="<sip_trunk_username>" password="<sip_trunk_password>">sip:<your_phone_number>@<your_sip_host></Sip>
  </Dial>
</Response>
ii. Point your Twilio phone number's Voice handler to this TwiML Bin. Easy peasy! 😊 1. LiveKit Setup β—¦ Create an inbound SIP trunk configuration file (JSON):
Copy code
{
  "trunk": {
    "name": "My inbound trunk",
    "auth_username": "<sip_trunk_username>",
    "auth_password": "<sip_trunk_password>"
  }
}
β—¦ Register the inbound trunk using LiveKit CLI:
lk sip inbound create inbound-trunk.json
β—¦ Set a dispatch rule (JSON):
Copy code
{
  "rule": {
    "dispatchRuleIndividual": {
      "roomPrefix": "call"
    }
  }
}
β—¦ Create the dispatch rule with LiveKit CLI:
lk sip dispatch-rule create dispatch-rule.json
1. Run Example Projects and Automations (Optional) β—¦ Install the official example project dependencies β—¦ Copy the
.env.example
and fill in your environment variables: β–ͺ︎
LIVEKIT_API_KEY
,
LIVEKIT_API_SECRET
,
LIVEKIT_URL
β–ͺ︎
LIVEKIT_SIP_URI
(pointing at your trunk) β–ͺ︎
TWILIO_PHONE_NUMBER
,
TWILIO_ACCOUNT_SID
,
TWILIO_AUTH_TOKEN
β–ͺ︎
TWILIO_SIP_USERNAME
,
TWILIO_SIP_PASSWORD
β–ͺ︎ (If using a voice assistant:
OPENAI_API_KEY
) β—¦ Automated setup scripts:
npm run setup:twilio
and
npm run setup:livekit
β—¦ Start the agent:
npm run agent
1. Test Calling β—¦ Call your Twilio phone number. The call should route into the LiveKit room, allowing your configured agent to join and interact. Time to see the magic happen! ✨ I know this might look a bit technical, but trust me, you've got this! If you need a little extra hand-holding or want me to dive deeper into any of these steps, just give me a shout. I'm here to help you every step of the way. πŸ’–
m
lk sip dispatch-rule create dispatch-rule.json Using url, api-key, api-secret from environment missing rule
t
Hey there! I see you're running into a tricky 'missing rule' error when creating a dispatch rule. Don't worry, this happens to the best of us. Essentially, your
dispatch-rule.json
file is missing something crucial - the required
rule
object
. Think of it like a recipe that needs all its key ingredients to work properly. Example: Individual Dispatch Rule (Most Common)
Copy code
{
  "rule": {
    "dispatchRuleIndividual": {
      "roomPrefix": "call-"
    }
  }
}
You've got a few options here:
dispatchRuleDirect
,
dispatchRuleCallee
, or the one I showed above. The key is to include just one type inside the
rule
object. My advice? Double-check your
dispatch-rule.json
file, make sure you've got one of these rule structures, and give it another shot. If you're still stuck, no worries - just take a close look at your JSON syntax and make sure all the required fields are in place. Sources: SIP dispatch rule | LiveKit Docs | cmd/lk/sip.go
m
but what we need to add i have manully create dispatch-rule.json and added { "rule": { "dispatchRuleIndividual": { "roomPrefix": "call-" } } } and its showing error lk sip dispatch create dispatch-rule.json Using url, api-key, api-secret from environment missing rule or is it created automatically
t
Hey there! I totally understand how frustrating the 'missing rule' error can be. Let me help you walk through this together. Here's a friendly checklist to get you back on track: β€’ Your file should look exactly like this:
Copy code
{
  "rule": {
    "dispatchRuleIndividual": {
      "roomPrefix": "call-"
    }
  }
}
β€’ Just a heads up - you'll need to create the dispatch rule manually – it won't happen automatically. β€’ Pro tip: Make sure you create an inbound trunk first before setting up your dispatch rule. β€’ Double-check that your API key and secret have SIP admin permissions. β€’ Quick check: Confirm your fields match one of these formats:
dispatchRuleIndividual
,
dispatchRuleDirect
, or
dispatchRuleCallee
inside the
rule
object, and stick to just one. Still seeing the error? No worries! Take a quick look for any extra spaces in your JSON, make sure your trunk is set up, and verify your permissions. Remember, you'll need to do this manually through the CLI or API. Sources: cmd/lk/sip.go | pkg/service/sip.go | Create and configure SIP trunk | LiveKit Docs | livekit-api/src/services/sip.rs | SIP dispatch rule | LiveKit Docs
m
How to create inbound trunk first
t
Hey there! Want to set up an inbound SIP trunk in LiveKit using the CLI? No worries, I'll walk you through it step by step – it's easier than you might think! 1. First, let's create a configuration file called
inbound-trunk.json
. Don't stress – I'll show you exactly what to put in it (just swap in your Twilio number):
Copy code
{
  "trunk": {
    "name": "My trunk",
    "numbers": [
      "+15105550100"
    ]
  }
}
2. Now, run this super simple CLI command:
lk sip inbound create inbound-trunk.json
And that's it! This will get your trunk set up to handle incoming calls to the number you specified. Pro tip: If you need some extra customization, you can totally add optional parameters like
krisp_enabled
or
auth_username
. Once you've done this, you'll be all set to create your dispatch rule. Easy peasy! Sources: livekit/livekit_sip.twirp.go | Accepting incoming calls | LiveKit Docs | SIP inbound trunk | LiveKit Docs | SIP APIs | LiveKit Docs
m
Here we have got lk sip inbound create inbound-trunk.json Using url, api-key, api-secret from environment SIPTrunkID: ST_VvKEdEnYa3dH but still facing issue in dispatch-rule.json lk sip dispatch create dispatch-rule.json Using url, api-key, api-secret from environment missing rule lk sip dispatch-rule create dispatch-rule.json Using url, api-key, api-secret from environment missing rule
t
Hey there! I totally understand the frustration with the 'missing rule' error. Let me help you troubleshoot this. Both
lk sip dispatch create
and
lk sip dispatch-rule create
do the same thing, so the command isn't causing your issue. Here are a few things we can check together: β€’ Your JSON might have some sneaky formatting problems like hidden characters or tricky quote mismatches. β€’ You might be using a CLI version that's a bit out of date. Quick tip: run
lk --version
to double-check. β€’ There's a chance the
rule
key is a bit old-school. Some newer LiveKit versions prefer
dispatch_rule
. Why don't you try switching your
dispatch-rule.json
to use "dispatch_rule" instead of "rule":
Copy code
{
  "dispatch_rule": {
    "dispatchRuleIndividual": {
      "roomPrefix": "call-"
    }
  }
}
Pro tip: run your JSON through a strict linter and make sure you're giving the CLI the exact file name. If this doesn't solve it, let's compare your CLI and server versions to make sure they're playing nice together. Sources: cmd/lk/sip.go | livekit/livekit_sip.twirp.go | SIP dispatch rule | LiveKit Docs
m
Here we are still getting error but lk sip dispatch list Using url, api-key, api-secret from environment β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ SipDispatchRuleID β”‚ Name β”‚ SipTrunks β”‚ Type β”‚ RoomName β”‚ Pin β”‚ Attributes β”‚ Agents β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ SDR_ap8g5NyVbnAi β”‚ My dispatch rule β”‚ <any> β”‚ Individual (Caller) β”‚ call-_<caller>_<random> β”‚ β”‚ map[] β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜
t
Hey there! Don't worry about that error message – your dispatch rule has actually been created successfully. I double-checked using
lk sip dispatch list
, and the entry is right there, looking good for individual room dispatching. You're all set to move forward! Feel free to test out your inbound call routing or explore more SIP features. If anything seems a bit off or you run into any hiccups, just let me know the details, and I'll be happy to help. But for now, your dispatch rule is totally ready to go!
m
now help with outbound
t
Hey there! Let's walk through setting up outbound SIP calling from LiveKit through Twilio together. Don't worry, I'll break it down into super manageable steps. 😊 1. Configure your Twilio SIP trunk: - Think of this like creating a special phone line. You'll want to create a SIP trunk with a domain like
<http://my-trunk.pstn.twilio.com|my-trunk.pstn.twilio.com>
. - Set up your login credentials - just like a secure password for your account. - Linking a Twilio number is optional if you're just doing outbound calls, but good to know! 2. Create `outbound-trunk.json`:
Copy code
{
  "trunk": {
    "name": "My outbound trunk",
    "address": "<http://my-trunk.pstn.twilio.com|my-trunk.pstn.twilio.com>",
    "numbers": [
      "+15105550100"
    ],
    "auth_username": "your_username",
    "auth_password": "your_password"
  }
}
- Pro tip: Make sure to swap out those placeholder values with your actual Twilio details! 3. Register the outbound trunk in LiveKit:
lk sip outbound create outbound-trunk.json
4. Want maximum flexibility? Use
"numbers": ["*"]
to allow calling from any number. 5. Ready to make calls? You'll create a SIP participant through the API with these key details: I can totally help you with the exact schema if you need it! β—¦ `sip_trunk_id`: (your unique trunk ID) β—¦ `sip_call_to`: (who you're calling) β—¦ `sip_number`: (your authorized Twilio number, needed if using ["*"]) β—¦ Your usual room and participant details And voilΓ ! You're now set up to make outbound SIP calls through Twilio with LiveKit. πŸŽ‰ Need a specific code example or have questions about implementation? Just ask! I'm here to help you nail this setup. πŸ‘ Sources: Making calls using SIP | LiveKit Docs | Create and configure a Twilio SIP trunk | LiveKit Docs | Making outbound calls | LiveKit Docs | SIP outbound trunk | LiveKit Docs