async def start_livekit_room_session(data): ro...
# ask-ai
b
async def start_livekit_room_session(data): room = rtc.Room() token = api.AccessToken().with_identity("pronunciation_analysis_task_bot").with_grants(api.VideoGrants(room_join=True, room=data["roomName"])).to_jwt() @room.on("track_subscribed") def on_track_subscribed(track, _, participant): if track.kind == rtc.TrackKind.KIND_AUDIO: logger.info("inside audio track_subscribed") asyncio.create_task(forward_audio_to_pronunciation(rtc.AudioStream(track))) # we are extracting the audio object from here later we will format it and send to azure and deepgram logger.info("before room connect") await room.connect(config["livekit.url"], token) logger.info("after room connect") in this code we join the room using livekit api, but the api key and access token are from the environemnt, how do we join using them by manually setting api key and access token in the code
t
Thanks for reaching out, Mohammed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! To help you manually set the API key and access token in your code for joining a LiveKit room, you'll need to generate the JWT access token explicitly using your API key and secret. Let me walk you through the steps: 1. Generate the Token:
Copy code
from livekit import api, rtc

API_KEY = "your-livekit-api-key"
API_SECRET = "your-livekit-api-secret"
ROOM_NAME = "your-room-name"
PARTICIPANT_IDENTITY = "your-identity"

token = (
    api.AccessToken(API_KEY, API_SECRET)
    .with_identity(PARTICIPANT_IDENTITY)
    .with_grants(api.VideoGrants(room_join=True, room=ROOM_NAME))
    .to_jwt()
)
2. Connect to the Room:
Copy code
room = rtc.Room()
await room.connect("<wss://your-livekit-instance.livekit.cloud>", token)
Just a friendly reminder to replace those placeholders with your actual API key, secret, room name, and participant identity to make sure everything works smoothly. Security Note: It's super important to never expose your API secret in frontend or client-side code. Always generate tokens securely on your backend or through a serverless function to keep everything safe. Sources: examples/agent_dispatch.py | Authentication | LiveKit Docs | .env.example | terminal-client/client.py | Generating tokens | LiveKit Docs