I have a livekit server deployed locally and I am ...
# ask-ai
a
I have a livekit server deployed locally and I am able to run my agent in the command prompt, i now need to connect this with telephony I have a twilio number how can I move forward
t
Thanks for reaching out, Bipin. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — great to hear your LiveKit server and agent are running locally. You’re asking how to connect your Twilio number so phone calls reach your agent. Below I’ll walk through the practical next steps and questions that will help you get this working. Quick checklist of what you need to do (high level) • Create a Twilio SIP domain/trunk that points to a public SIP endpoint for your LiveKit server. • Configure an inbound SIP trunk in LiveKit so LiveKit accepts calls from Twilio. • Create a dispatch rule so incoming calls are routed to a room and your agent is dispatched. • Ensure your agent is named to match the dispatch rule and that your LiveKit SIP endpoint is publicly reachable (or tunneled). • Test by calling your Twilio number. Before I give step-by-step commands, two quick clarifying questions so I provide the exact commands you’ll need: 1. Is your LiveKit server reachable from the public internet (public IP/DNS) or are you running strictly on localhost behind NAT? 2. Are you using plain SIP (UDP/TCP on 5060) or SIP over TLS (recommended for production)? If your server is behind NAT and not publicly reachable, tell me whether you can: • Add a public DNS / port-forward to expose SIP, or • Use a secure tunnel (ngrok / Cloudflared) for SIP/TLS during testing. If you confirm those, here are the concrete steps you can follow. Step-by-step (assumes public reachable LiveKit SIP endpoint) 1. Create a SIP domain in Twilio • In the Twilio Console: Programmable Voice → SIP Domains → Create new SIP Domain. • Set the “URI / Inbound Registrar URL” to your LiveKit SIP hostname (for example
<http://sip.your-domain.com|sip.your-domain.com>
). • Enable SIP REFER / PSTN transfer options if you plan to transfer calls between IVRs or other SIP endpoints. 1. Configure an inbound SIP trunk in LiveKit • Use the LiveKit CLI or API to create an inbound trunk that lists your Twilio phone number(s). Example CLI (adjust to your environment):
Copy code
lk sip inbound create --name "Twilio-Inbound" --numbers "+1234567890"
Replace the phone number with your Twilio number. • Ensure LiveKit’s SIP service is listening on the correct ports and protocol (UDP/TCP/TLS) and that any firewall/NAT forwards those ports. 1. Create a dispatch rule to route calls into a room and start your agent • Create a dispatch rule JSON that routes calls to rooms with a prefix (for example
call-
) and specifies the agent name to start. Example structure: { "dispatch_rule": { "rule": { "dispatchRuleIndividual": { "roomPrefix": "call-" } }, "roomConfig": { "agents": [ { "agentName": "my-agent" } ] } } } • Apply the rule with the LiveKit CLI or API:
Copy code
lk sip dispatch create dispatch-rule.json
1. Ensure your agent’s name matches the dispatch rule • In your agent code, name the agent so LiveKit can dispatch it by name:
Copy code
@server.rtc_session(agent_name="my-agent")
  async def my_agent(ctx: JobContext):
      # agent logic
This ensures the dispatch rule will start this agent for incoming calls. 1. (Optional) Outbound calling from agent via Twilio • Create an outbound trunk in LiveKit pointing to Twilio’s SIP URI and use the agent API to create a SIP participant to place outbound calls: ◦ Use LiveKit’s
create_sip_participant
with the outbound trunk ID and the phone number to dial. 1. Security & PCI considerations • Don’t collect card numbers directly in call recordings or logs. If you need payment, either transfer the call to a PCI-compliant IVR or use secure payment links. • If you need to avoid recording, start the agent or room with
record=False
. 1. Test • Run your agent in dev mode:
Copy code
python agent.py dev
• Call your Twilio number. The call should be routed via Twilio → SIP → LiveKit inbound trunk → dispatch rule → room → your agent should answer. If you want, I can: • Provide exact CLI/API commands for your environment once you tell me whether your server is publicly reachable and whether you want SIP over TLS or plain SIP. • Walk you through creating the Twilio SIP domain step-by-step. • Show a complete example dispatch-rule.json tailored to your agent name. Which option would you like next? Also please confirm whether your LiveKit server is publicly reachable or behind NAT so I give the correct commands. Sources: Agents telephony integration | LiveKit Documentation | Handling payments with PCI compliance | LiveKit | Creating SIP Inbound Trunks and Dispatch Rules with Python SDK | Outbound Calling Script | LiveKit Documentation | Simple Call Answering Agent | LiveKit Documentation