My livekit agent works in console mode and not in ...
# ask-ai
w
My livekit agent works in console mode and not in dev mode and when I try to run the dispatch rule or the inbound trunk python files I get a 401 error but everything is already set up
t
Thanks for reaching out, Keynar. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! A 401 error usually means there’s an authentication hiccup. For your Python files dealing with dispatch rules or inbound trunks, here are a few things to double-check: • Make sure the
auth_username
and
auth_password
in your
SIPInboundTrunkInfo
setup are spot on. These should exactly match the credentials from your SIP trunking service. • Try using the
list_inbound_trunk
method in the Python
SipService
to peek at your current trunk configuration and verify those authentication details. • If you need to tweak anything, you can update the credentials with the
update_inbound_trunk_fields
method—no need to recreate the trunk from scratch. If you want, I’m here to help walk you through these steps or troubleshoot further. Just let me know! Sources: livekit-protocol/livekit/protocol/analytics.pyi | sip/sip_test.go | SIP troubleshooting guide | LiveKit Docs
w
here's the dispatch rule python file : import asyncio import os from livekit import api from dotenv import load_dotenv async def main(): # Load environment variables load_dotenv() # Create an instance of the LiveKitAPI # LiveKit automatically reads the environment variables for the API key and secret lkapi = api.LiveKitAPI() # Create a SIP Dispatch Rule object that requests a private/individual room for each call _# The room_prefix is 'call-' to indicate that this is part of a telephony system_ rule = api.SIPDispatchRule( _dispatch_rule_individual_=api.SIPDispatchRuleIndividual( _room_prefix_="call-", ) ) # Create a Room Agent object that will be dispatched to the room agent = api.RoomAgentDispatch( _agent_name_="inbound-agent", ) # Create a Room Configuration object that contains the Room Agent object room_config = api.RoomConfiguration( agents=[agent] ) # Create a SIP Dispatch Rule request with the rule and room configuration request = api.CreateSIPDispatchRuleRequest( rule=rule, _room_config_=room_config ) _try_: # Create the SIP Dispatch Rule with LiveKit dispatch = await lkapi.sip.create_sip_dispatch_rule(request) print(f"Successfully created dispatch rule: {dispatch.rule.sip_dispatch_rule_id}") except Exception as e: print(f"Error creating dispatch rule: {e}") _finally_: # Close the LiveKit API connection await lkapi.aclose() # Run the main function if name == "__main__": asyncio.run(main())
t
Thanks so much for sharing the Python file! The code looks well put together for creating a SIP Dispatch Rule. That said, the 401 error usually points to an authentication hiccup. Could you double-check a few things for me? • Are your API key and secret correctly set in your environment variables? • Is the LiveKit server URL in your environment configuration the right one? • Have you made sure the API key has the permissions needed for SIP operations? Once you confirm these, we’ll be in a better spot to figure out what’s going on. I’m here to help!
w
How can I make sure the API key has the permissions needed for SIP operations?
t
Hey there! To make sure your API key has the right permissions for SIP operations, you’ll want to check that the JSON Web Token (JWT) includes these SIP grants: • Admin Permission: This one’s essential for managing SIP trunks and dispatch rules. Your token should have
"sip": { "admin": true }
. • Call Permission: Needed for making outbound SIP calls. Make sure the token includes
"sip": { "call": true }
. Here’s a quick example of how you can generate a token with these permissions:
Copy code
from livekit.api import AccessToken, SIPGrants

token = (
    AccessToken(api_key="your_api_key", api_secret="your_secret")
    .with_sip_grants(SIPGrants(admin=True, call=True))
    .to_jwt()
)
And if you want to double-check the token’s claims, you can use a
TokenVerifier
like this:
Copy code
from livekit.api import TokenVerifier

verifier = TokenVerifier(api_key="your_api_key", api_secret="your_secret")
claims = verifier.verify(token)
assert claims.sip.admin == True  # Verify admin permission
If you hit any bumps or have questions along the way, just give me a shout—I’m here to help! Sources: Authentication | LiveKit Docs | SIP APIs | LiveKit Docs | pkg/service/auth.go | livekit-api/tests/test_access_token.py
w
How do I do that ? I did not see any of that in the docs ?
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
w
How and where can I make sure the API key has the permissions needed for SIP operations?
yo answer