can I share my sip_trunk_id dynamically in dispatc...
# ask-ai
c
can I share my sip_trunk_id dynamically in dispatch instead of setting it as an env variable?
t
Thanks for reaching out, Subham. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can share your
sip_trunk_id
dynamically in dispatch rules instead of setting it as an environment variable. This is done by associating one or more `sip_trunk_id`s with a SIP dispatch rule when you create or update it. For instance, if you're using the Python SDK, you can dynamically assign a
sip_trunk_id
while creating a dispatch rule like this:
Copy code
dispatch_rule = await lkapi.sip.create_dispatch_rule(
    api.CreateSIPDispatchRuleRequest(
        dispatch_rule=api.SIPDispatchRuleInfo(
            name="my-dynamic-rule",
            rule=api.SIPDispatchRule(
                dispatch_rule_direct=api.SIPDispatchRuleDirect(room_name="meeting-room")
            )
        ),
        trunk_ids=[trunk_id]  # Dynamic assignment of Trunk ID
    )
)
And if you ever need to update the trunk ID association later on, you can easily do that using the
UpdateSIPDispatchRuleRequest
API, allowing you to modify the associated trunks dynamically. Please don’t hesitate to reach out if you’d like me to walk you through any part of this or if you need more details. I’m here to help! Sources: telephony/survey_caller/make_survey_calls.py | SIP APIs | LiveKit Docs | livekit/livekit_sip.pb.go | livekit-protocol/livekit/protocol/sip.py
c
what is the default way of sip_trunk_id in dispatch?
t
I’m happy to help explain how the
sip_trunk_id
works in dispatch rules! Typically, you can leave the
trunk_ids
field empty. When it’s empty, the dispatch rule will match all SIP trunks, so it applies to incoming calls from any trunk. If you do provide specific
trunk_ids
, the rule will then only apply to those particular trunks, which overrides the default behavior. Please feel free to reach out if you’d like me to walk you through this further or if you have any other questions! Sources: SIP APIs | LiveKit Docs | sip/sip.go
c
So, basically I don't need to provide trunk_id in the dispatch function which is let's say in another service. But if my agent deployed on livekit cloud has a configured dispatch, it will work correctly?
t
Absolutely! Your LiveKit Cloud agent with a configured dispatch will work just fine even if you don’t explicitly provide a
trunk_id
in the dispatch function. When the
trunk_ids
field is left out in a SIP dispatch rule, it means the rule will match calls from all inbound trunks, so it applies universally. As long as your agent is set up correctly in the dispatch rule, it will handle incoming SIP calls just as you’d expect, no matter which trunk is used. If you have any more questions or need a bit more help, feel free to reach out—I’m here for you! Sources: SIP dispatch rule | LiveKit Docs | pkg/service/ioservice_sip.go | Accepting incoming calls | LiveKit Docs
c
The context of my conversation is inbound calls
t
When it comes to inbound calls, if the
trunk_id
isn’t provided, LiveKit kindly looks for dispatch rules that don’t specify any `trunk_ids`—these are what we call wildcard rules. These rules are designed to match calls from all inbound trunks, so the system can still manage calls smoothly even if there isn’t a specific trunk association. If there isn’t a matching wildcard rule, the call will unfortunately be rejected. To avoid this, it’s a good idea to have at least one dispatch rule set up without
trunk_ids
for your inbound calls. Please feel free to reach out if you’d like me to explain anything further or if you need a hand with this! Sources: sip/sip.go | SIP dispatch rule | LiveKit Docs | pkg/service/ioservice_sip.go
c
See when i do
lk dispatch
for an outbound call , do I need trunk id in my env during that time?
t
Absolutely! When making outbound calls using
lk dispatch
, it's important to include the
trunk_id
in your environment. This helps because the
sip_trunk_id
is a required parameter when creating a SIP participant for outbound calls, and it tells the system which SIP provider and credentials to use. If you have any questions or need a hand with setting this up, please don’t hesitate to reach out—I’m here to help! Sources: telephony/survey_caller/make_survey_calls.py | examples/warm-transfer/README.md | protobufs/livekit_sip.proto
c
how do I change the sip trunk assigned to an agent in cloud?
t
I’m happy to help you change the SIP trunk associated with an agent in LiveKit Cloud! While you can’t assign a SIP trunk directly to an agent, you can update the SIP dispatch rules to route calls from specific trunks to the rooms where your agent is available. Here’s a friendly step-by-step guide to get you started: 1. First, make sure your agent is properly configured and registered with a recognizable
agent_name
. 2. Next, create or update a SIP dispatch rule to include your agent in the
room_config.agents
list. 3. Then, link the dispatch rule to the specific SIP trunks by setting the
trunk_ids
field in the rule configuration. 4. Finally, apply the updated rule using the CLI command (
lk sip dispatch update
) or through the API. If you’re working with outbound calls, just remember that the agent needs to specify the
sip_trunk_id
dynamically when creating a SIP participant for the call. Please feel free to reach out if you’d like me to walk you through detailed steps or provide examples tailored to your situation—I’m here to help! Sources: livekit/livekit_sip.pb.go | Accepting incoming calls | LiveKit Docs | cmd/lk/sip.go | packages/livekit-server-sdk/src/SipClient.ts | SIP APIs | LiveKit Docs
c
What I meant is if my if call lk dispatch with my agent name, can I change the sip trunk it uses or it is obtained from env variables?
t
I understand you're working with
lk dispatch
and using an agent name. By default, the SIP trunk isn’t dynamically changeable because the trunk ID is taken from an environment variable (like
SIP_OUTBOUND_TRUNK_ID
) and applied to all calls. But here’s some good news—you can actually enable dynamic trunk selection! This involves tweaking the agent so it reads the
sip_trunk_id
from the dispatch metadata. Doing this lets you choose a different trunk for each dispatch. The agent would parse this metadata in its entrypoint function and use the trunk ID you provide when setting up the SIP participant. If you’d like, I’m more than happy to walk you through the detailed steps or clarify anything further. Just let me know! Sources: telephony/survey_caller/make_survey_calls.py | agent.py | Dispatching agents | LiveKit Docs | Agent dispatch | LiveKit Docs | cmd/lk/sip.go
c
Got it thanks!
t
I'm so glad I could help! If anything else comes up or you have more questions down the road, please don't hesitate to get in touch. Wishing you a wonderful day ahead!