I want to not use await for the following requests...
# ask-ai
s
I want to not use await for the following requests using python sdk. api.LiveKitAPI.sip.create_sip_participant() api.LiveKitAPI.agent_dispatch.create_dispatch() Is there a way to do that?
t
Thanks for reaching out, Anuj. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You’re asking whether
api.LiveKitAPI.sip.create_sip_participant()
and
api.LiveKitAPI.agent_dispatch.create_dispatch()
can be called without
await
using the Python SDK. Those Python SDK methods are asynchronous and expect to run inside an async event loop — there is no built-in synchronous wrapper that avoids
await
. If you need to call them from synchronous code, you can run the coroutine on an event loop. Options: • Use
asyncio.run()
from top-level sync code:
Copy code
import asyncio

async def call():
    await api.LiveKitAPI.sip.create_sip_participant(...)

asyncio.run(call())
• Run the coroutine on an existing loop (e.g., in a thread where an event loop is running) using `asyncio.get_event_loop().run_until_complete(...)`:
Copy code
loop = asyncio.get_event_loop()
loop.run_until_complete(api.LiveKitAPI.sip.create_sip_participant(...))
• If your app is primarily synchronous and you must avoid touching the main thread event loop, run an event loop in a separate thread and submit coroutines to it (e.g., with
loop.call_soon_threadsafe
+
asyncio.run_coroutine_threadsafe
). If you want, tell me whether you’re in a script, web framework, or threaded app and I’ll show the exact pattern/code best suited for your environment. Sources: SIP APIs | LiveKit docs | Make outbound calls | LiveKit docs | Agents telephony integration | LiveKit docs | Creating SIP Inbound Trunks and Dispatch Rules with Python SDK
s
I need to keep it synchronous. Is there a way to do it without the python sdk?
t
You can do this without the async Python SDK by calling LiveKit’s HTTP/Twirp endpoints synchronously. I’m responding to your request to keep the flow fully synchronous and avoid
await
. What you can do • Call the Twirp endpoints directly (HTTP POST) for: ◦ Create SIP participant: /twirp/livekit.SIP/CreateSIPParticipant ◦ Create dispatch rule: /twirp/livekit.SIP/CreateSIPDispatchRule • Use any synchronous HTTP client (e.g., Python
requests
, curl, Go http client). What you need • A JWT signed with your API key/secret that includes the
sip
grants (
call
for creating participants,
admin
for dispatch rules) and
video.roomJoin
as needed. • Content-Type: application/json and Authorization: Bearer <JWT> headers. Example (synchronous Python with requests)
Copy code
import requests
url = "https://your-livekit-server/twirp/livekit.SIP/CreateSIPParticipant"
headers = {
  "Authorization": "Bearer YOUR_JWT",
  "Content-Type": "application/json",
}
payload = {
  "sip_trunk_id": "ST_xxx",
  "sip_call_to": "+15105550123",
  "room_name": "my-room",
  "participant_identity": "sip-caller",
  "wait_until_answered": True
}
resp = requests.post(url, json=payload, headers=headers)
print(resp.status_code, resp.text)
Example (curl)
Copy code
curl -X POST https://your-livekit-server/twirp/livekit.SIP/CreateSIPParticipant \
  -H "Authorization: Bearer <YOUR_JWT>" \
  -H "Content-Type: application/json" \
  -d '{"sip_trunk_id":"ST_xxx","sip_call_to":"+15105550123","room_name":"my-room","participant_identity":"sip-caller","wait_until_answered":true}'
If you’d like, I can: • Provide the exact JSON schema for each endpoint. • Show how to generate the JWT in synchronous Python. • Give a synchronous example for CreateSIPDispatchRule. Which of those would help you most? Sources: SIP APIs | LiveKit docs | sip | livekit/livekit_composite | DeepWiki