delightful-microphone-72472
12/04/2025, 2:01 PMtall-airline-16756
12/04/2025, 2:01 PMtall-airline-16756
12/04/2025, 2:01 PMfrom livekit import api
token = api.AccessToken(api_key="your-key", api_secret="your-secret")\
.with_identity("user-identity")\
.with_grants(api.VideoGrants(room_join=True, room="my-room"))\
.to_jwt()
# return `token` from an API endpoint (e.g., /session or /getToken)
2) Backend (optional): Python agent that can also join the room
• If you want a server-side participant, use the agents framework:
from livekit.agents import JobContext, WorkerOptions, cli
from livekit import rtc
async def entrypoint(ctx: JobContext):
await ctx.connect(auto_subscribe=rtc.AutoSubscribe.AUDIO_ONLY)
room = ctx.room
# agent logic here
if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
3) Frontend: request token and connect
• Frontend requests the token from your backend, then connects with the LiveKit client (example using React component or Room.connect):
// fetch token from backend
const res = await fetch('/getToken');
const { token } = await res.json();
// connect using livekit-client or @livekit/components-react
import { Room } from 'livekit-client';
const room = new Room();
await room.connect(process.env.NEXT_PUBLIC_LIVEKIT_URL, token);
Or with @livekit/components-react:
<LiveKitRoom serverUrl={process.env.NEXT_PUBLIC_LIVEKIT_URL} token={token} connect={true}/>
Key points to ensure:
• Return a signed JWT from your Python backend to the frontend.
• Use the same room name in token grants and when connecting.
• Frontend uses the LiveKit client SDK to connect with the token.
• If you need the backend to also participate, use the Python agent to join the same room.
Would you like a full example project for backend token endpoint + React frontend, or help adapting this to your stack (Flask/FastAPI/Next.js)?
Sources: OpenAI and LiveKit partner to turn Advanced Voice into an API | Generating tokens | LiveKit docs | examples/voice_agents | livekit/livekit_composite | DeepWiki