Starting Sessions
This guide will show you how to start a session and connect to it. At a high level, here’s how this process works:
- Your backend starts a session by calling an authenticated API endpoint and returning session credentials to your frontend.
- Your frontend connects to the session using the session credentials.
1. Start a session from your backend
In this section, you’ll set up your backend so that it can start sessions by calling Jay’s
startSession
API endpoint.
1a. Define your Jay API key as an environment variable
Create a JAY_API_KEY
environment variable in your local environment (e.g., a .env
file) and/or
in your production environment (e.g., Vercel, AWS, etc.). In a .env
file, you might include:
You can retrieve your API key from Jay’s dashboard.
1b. Create an API endpoint
Create an API endpoint in your backend for starting the session. We strongly recommend using authentication in your endpoint to protect your Jay API key and prevent unauthorized users from starting sessions.
Copy and paste the code below into the API endpoint:
In the code above, make sure to replace <YOUR_AGENT_ID>
with your agent ID, which you can either retrieve from the file containing your agent (e.g. agent/main.py
) or from Jay’s
dashboard.
Optional: You can include custom data in the payload, which will be available when the session is configured:
The custom data will be available in the custom_data
input parameter of the configure_session
function.
Note: The custom_data
field must be JSON serializable.
2. Connect to the session from your frontend
In your frontend, you’ll fetch the session credentials from the backend endpoint that you defined in step 1. Then, you’ll connect to the session using these credentials. We’ll use LiveKit to handle the WebRTC connection.
2a. Install LiveKit dependencies
Install the following dependencies:
2b. Connect to the session
The code snippet below is a simple React example that shows how to fetch the session credentials from your backend, connect to the session using these credentials, and display basic loading and connection states.
In the code above, make sure to replace <YOUR_BACKEND_ENDPOINT>
with your backend endpoint URL
from step 1.
Here are a couple relevant references from LiveKit’s documentation:
LiveKitRoom
: Provides the LiveKit room context to all its child components. It provides the room state as a React context to all child components, so you don’t have to pass it yourself.RoomAudioRenderer
: A drop-in solution for adding audio to your app. It takes care of handling audio tracks and makes sure that microphones and screen share are audible.