how to pass context variables from the front-end
# ask-ai
r
how to pass context variables from the front-end
t
Thanks for reaching out, Ivan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you pass context variables from the front-end to a LiveKit agent! Here are some friendly ways you can do this: • Job Metadata: You can pass static info like user details right when you initialize the agent. This metadata will be available in the agent's entrypoint. • Participant Attributes: On the front-end, you can set attributes when connecting to a room, and these will be accessible on the backend. • RPC (Remote Procedure Call): This is a great way to send data dynamically from the front-end to the agent during an active session. • UserData Pattern: Using structured data classes helps manage session state and pass it along to the agent during initialization. Just a heads-up: all these methods involve sending data from the front-end to the backend, since direct front-end-to-agent communication isn’t supported. If you want, I’d be happy to walk you through detailed examples of any of these approaches—just let me know! Sources: External data and RAG | LiveKit Docs | Tool definition and use | LiveKit Docs | Web and mobile frontends | LiveKit Docs | context-readmes/USERDATA_GUIDE.md
r
give me an example of "job metadata" pass from the front-end to backend
t
Here’s a friendly example to help you pass "job metadata" from the front-end to the backend: 1. Collect Data on the Front-End:
const userData = { name: "Alice", age: 30, city: "Madrid" };
You can send this data to your backend using an API call or WebSocket. 2. Send Data to Backend: Try using a POST request to send the data:
Copy code
fetch('/start-agent', {
 method: 'POST',
 headers: { 'Content-Type': 'application/json' },
 body: JSON.stringify(userData)
});
3. Backend Receives and Passes Metadata: On the backend, you’ll want to include the received data as metadata when starting the agent job:
Copy code
async def start_agent_job(request):
    user_data = await request.json()
    metadata = {
        "name": user_data["name"],
        "age": user_data["age"],
        "city": user_data["city"]
    }

    await livekit.start_agent_job(metadata=metadata)
4. Access Metadata in the Agent: Then, in the agent’s entrypoint, you can easily retrieve the metadata:
Copy code
async def entrypoint(ctx: JobContext):
    user_name = ctx.metadata.get("name")
    user_age = ctx.metadata.get("age")
    user_city = ctx.metadata.get("city")

    print(f"User: {user_name}, Age: {user_age}, City: {user_city}")
This method helps make sure your front-end data smoothly reaches the backend and is included as metadata for the agent session. If anything’s unclear or you want to dive deeper, just let me know—I’m here to help!
r
is there a similar front-end example in livekit example repositories?
t
I'm glad you're exploring LiveKit repositories! They include some great examples that gently guide you through passing metadata from the front-end to the backend, such as: • Room Metadata: The livestream example is a wonderful way to see how front-end applications can send metadata to backend APIs, which then create rooms with that metadata. • Participant Metadata: You'll find helpful examples in the Unity client SDK and React components that show how to set and access participant metadata. • Backend API Metadata Updates: The realtime-room-monitor example nicely defines types for updating room metadata via backend APIs. Although there isn’t a single, complete example specifically for passing job metadata from the front-end to the backend, you can definitely combine these patterns to make it work smoothly. Please feel free to reach out if you'd like some guidance designing a specific implementation or if you'd like me to share links to these examples—I’m here to help! Sources: packages/react/src/hooks/useRoomInfo.ts | agent-console/lib/types.ts | Agent dispatch | LiveKit Docs | Job lifecycle | LiveKit Docs | src/lib/controller.ts