creamy-tiger-14805
09/11/2025, 6:15 AMtall-airline-16756
09/11/2025, 6:15 AMtall-airline-16756
09/11/2025, 6:15 AMcreamy-tiger-14805
09/11/2025, 6:18 AMsession_started = asyncio.create_task(
session.start(
agent=agent,
room=ctx.room,
room_input_options=RoomInputOptions(
# LiveKit Cloud enhanced noise cancellation
# - If self-hosting, omit this parameter
# - For telephony applications, use `BVCTelephony` for best results
noise_cancellation=noise_cancellation.BVCTelephony(),
),
)
)
try:
await ctx.api.sip.create_sip_participant(
api.CreateSIPParticipantRequest(
room_name=ctx.room.name,
sip_trunk_id=os.getenv("SIP_OUTBOUND_TRUNK_ID"),
sip_call_to=phone_number,
participant_identity=participant_identity,
wait_until_answered=True,
)
)
await session_started
participant = await ctx.wait_for_participant(identity=participant_identity)
<http://logger.info|logger.info>(f"participant joined: {participant.identity}")
agent.set_participant(participant)
# await lkapi.aclose()
except Exception as e:
logger.error(f"Failed to create SIP participant: {e}")
ctx.shutdown()
# await lkapi.aclose()
return
tall-airline-16756
09/11/2025, 6:18 AMfrom livekit.api import LiveKitAPI, CreateRoomRequest
lkapi = LiveKitAPI()
# Create a room with metadata
room = await lkapi.room.create_room(
CreateRoomRequest(
name="my-room",
metadata='{"room_type": "webinar", "host": "user123"}' # JSON string or any string data
)
)
• Update Metadata for an Existing Room: from livekit.api import LiveKitAPI, UpdateRoomMetadataRequest
lkapi = LiveKitAPI()
# Update room metadata
updated_room = await lkapi.room.update_room_metadata(
UpdateRoomMetadataRequest(
room="my-room",
metadata='{"status": "active", "phase": "started"}'
)
)
If you’d like, I’m here to help you integrate this smoothly into your existing code—just let me know! Sources: livekit-protocol/livekit/protocol/room.pyi | Docs::Home | Docs::Hometall-airline-16756
09/11/2025, 6:19 AMfrom livekit.api import LiveKitAPI, CreateRoomRequest
lkapi = LiveKitAPI()
# Create a room with metadata
room = await lkapi.room.create_room(
CreateRoomRequest(
name="my-room",
metadata='{"room_type": "webinar", "host": "user123"}' # JSON string or any string data
)
)
• Update Metadata for an Existing Room: from livekit.api import LiveKitAPI, UpdateRoomMetadataRequest
lkapi = LiveKitAPI()
# Update room metadata
updated_room = await lkapi.room.update_room_metadata(
UpdateRoomMetadataRequest(
room="my-room",
metadata='{"status": "active", "phase": "started"}'
)
)
If you’d like, I’m here to help you smoothly integrate this into your current code—just let me know!creamy-tiger-14805
09/11/2025, 6:27 AMtall-airline-16756
09/11/2025, 6:27 AMfrom livekit.api import LiveKitAPI, CreateRoomRequest
lkapi = LiveKitAPI()
# Create a room with metadata
room = await lkapi.room.create_room(
CreateRoomRequest(
name="my-room",
metadata='{\"room_name\": \"my-room\", \"room_id\": \"12345\"}'
)
)
• Update Metadata for an Existing Room: from livekit.api import LiveKitAPI, UpdateRoomMetadataRequest
lkapi = LiveKitAPI()
# Update room metadata
updated_room = await lkapi.room.update_room_metadata(
UpdateRoomMetadataRequest(
room="my-room",
metadata='{\"room_name\": \"my-room\", \"room_id\": \"12345\"}'
)
)
This approach will make sure your metadata includes both the room name and room ID. If you have any questions or need a hand with anything else, please don’t hesitate to reach out—I’m here to help!creamy-tiger-14805
09/11/2025, 6:29 AMsession_started = asyncio.create_task(
session.start(
agent=agent,
room=ctx.room,
room_input_options=RoomInputOptions(
# LiveKit Cloud enhanced noise cancellation
# - If self-hosting, omit this parameter
# - For telephony applications, use `BVCTelephony` for best results
noise_cancellation=noise_cancellation.BVCTelephony(),
),
)
)
try:
await ctx.api.sip.create_sip_participant(
api.CreateSIPParticipantRequest(
room_name=ctx.room.name,
sip_trunk_id=os.getenv("SIP_OUTBOUND_TRUNK_ID"),
sip_call_to=phone_number,
participant_identity=participant_identity,
wait_until_answered=True,
)
)
await session_started
participant = await ctx.wait_for_participant(identity=participant_identity)
<http://logger.info|logger.info>(f"participant joined: {participant.identity}")
agent.set_participant(participant)
# await lkapi.aclose()
except Exception as e:
logger.error(f"Failed to create SIP participant: {e}")
ctx.shutdown()
# await lkapi.aclose()
return
tall-airline-16756
09/11/2025, 6:30 AMfrom livekit.api import LiveKitAPI, UpdateRoomMetadataRequest
lkapi = LiveKitAPI()
# Update room metadata
await lkapi.room.update_room_metadata(
UpdateRoomMetadataRequest(
room=ctx.room.name, # Use the room name from your context
metadata='{\"room_name\": \"' + ctx.room.name + '\", \"room_id\": \"' + ctx.room.id + '\"}'
)
)
This example assumes you have access to the room name (ctx.room.name
) and room ID (ctx.room.id
) in your context. If you run into any questions or need a hand with anything else, just let me know—I’m here to help!creamy-tiger-14805
09/11/2025, 6:45 AMtall-airline-16756
09/11/2025, 6:46 AMcreamy-tiger-14805
09/11/2025, 6:46 AMtall-airline-16756
09/11/2025, 6:47 AMfrom livekit import rtc
import asyncio
import logging
async def main():
logging.basicConfig(level=<http://logging.INFO|logging.INFO>)
room = rtc.Room()
# Event: A new participant joins the room
@room.on("participant_connected")
def on_participant_connected(participant: rtc.RemoteParticipant):
<http://logging.info|logging.info>(f"Participant connected: {participant.identity}")
# Event: A participant leaves the room
@room.on("participant_disconnected")
def on_participant_disconnected(participant: rtc.RemoteParticipant):
<http://logging.info|logging.info>(f"Participant disconnected: {participant.identity}")
# Event: A remote participant publishes a track
@room.on("track_published")
def on_track_published(publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant):
<http://logging.info|logging.info>(f"Track published: {publication.sid} by {participant.identity}")
# Event: Room metadata is updated
@room.on("room_metadata_changed")
def on_room_metadata_changed(old_metadata: str, new_metadata: str):
<http://logging.info|logging.info>(f"Room metadata changed: {old_metadata} -> {new_metadata}")
# Finally, connect to the room using a URL and token
await room.connect("<wss://your-livekit-server>", "your-access-token")
<http://logging.info|logging.info>(f"Connected to room: {room.name}")
# Keep the application running to listen for events
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())
This example walks you through listening for important events like participant connections, track publications, and metadata updates. Just swap out "<wss://your-livekit-server>"
and "your-access-token"
with your own LiveKit server URL and access token. If you'd like, I’m here to help you tailor this example to fit your specific needs—just let me know! Sources: examples/rpc.pytall-airline-16756
09/11/2025, 6:47 AMfrom livekit import rtc
import asyncio
import logging
async def main():
logging.basicConfig(level=<http://logging.INFO|logging.INFO>)
room = rtc.Room()
# Event: A new participant joins the room
@room.on("participant_connected")
def on_participant_connected(participant: rtc.RemoteParticipant):
<http://logging.info|logging.info>(f"Participant connected: {participant.identity}")
# Event: A participant leaves the room
@room.on("participant_disconnected")
def on_participant_disconnected(participant: rtc.RemoteParticipant):
<http://logging.info|logging.info>(f"Participant disconnected: {participant.identity}")
# Event: A remote participant publishes a track
@room.on("track_published")
def on_track_published(publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant):
<http://logging.info|logging.info>(f"Track published: {publication.sid} by {participant.identity}")
# Event: Room metadata is updated
@room.on("room_metadata_changed")
def on_room_metadata_changed(old_metadata: str, new_metadata: str):
<http://logging.info|logging.info>(f"Room metadata changed: {old_metadata} -> {new_metadata}")
# Finally, connect to the room using a URL and token
await room.connect("<wss://your-livekit-server>", "your-access-token")
<http://logging.info|logging.info>(f"Connected to room: {room.name}")
# Keep the application running to listen for events
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())
This example shows you how to listen for important events like participants joining or leaving, track publications, and changes to room metadata. Just remember to swap out "<wss://your-livekit-server>"
and "your-access-token"
with your actual LiveKit server URL and access token. If you’d like any help tailoring this example to fit your needs, feel free to ask — I’m here to help!creamy-tiger-14805
09/11/2025, 7:16 AMtall-airline-16756
09/11/2025, 7:17 AMListRooms
and ListParticipants
, focus on active rooms only. To keep track of room details after the session is complete, it’s a good idea to save the important data externally during or before the room is in use. If you’d like, I’m here to help guide you through setting this up—just let me know! Sources: packages/livekit-server-sdk/src/RoomServiceClient.ts | protobufs/livekit_room.prototall-airline-16756
09/11/2025, 7:17 AMListRooms
and ListParticipants
, only show information about rooms that are currently active. To keep track of room details after the session is over, it’s best to save the important data externally while the room is still active or even before it starts. If you’d like, I’m here to help guide you through setting that up—just let me know!creamy-tiger-14805
09/11/2025, 11:51 AM"room": ctx.room.name,
can i also get the room_id for the same?tall-airline-16756
09/11/2025, 11:52 AMsid
property like this: from livekit import rtc
async def main():
room = rtc.Room()
await room.connect(URL, TOKEN)
room_sid = await room.sid # Retrieve the room ID
print(f"Room SID: {room_sid}")
asyncio.run(main())
• Server-Side (Management API): If you want to fetch room details from the server, the list_rooms
method will do the trick: from livekit import api
import asyncio
async def main():
lkapi = api.LiveKitAPI("<https://my-project.livekit.cloud>")
rooms = await lkapi.room.list_rooms(api.ListRoomsRequest())
for room in rooms.rooms:
print(f"Room SID: {room.sid}")
print(f"Room name: {room.name}")
await lkapi.aclose()
asyncio.run(main())
If you have any questions or need a hand with implementing this, just let me know—I’m here to help! Sources: livekit-rtc/livekit/rtc/_proto/room_pb2.pyitall-airline-16756
09/11/2025, 11:52 AMsid
property like this: room_sid = await room.sid
print(f"Room SID: {room_sid}")
• Server-side (management API): If you want to fetch room details from the server, just use the list_rooms
method: rooms = await lkapi.room.list_rooms(api.ListRoomsRequest())
for room in rooms.rooms:
print(f"Room SID: {room.sid}")
If you’d like, I’m here to help you walk through setting up either of these methods—just let me know!creamy-tiger-14805
09/11/2025, 12:02 PMtall-airline-16756
09/11/2025, 12:03 PMtall-airline-16756
09/11/2025, 12:03 PMcreamy-tiger-14805
09/11/2025, 12:04 PMtall-airline-16756
09/11/2025, 12:05 PMtall-airline-16756
09/11/2025, 12:05 PMcreamy-tiger-14805
09/11/2025, 12:09 PMtall-airline-16756
09/11/2025, 12:09 PM