Vanilla JS Frontend SDK
API reference for the Layercode Vanilla JS Frontend SDK.
LayercodeClient
The LayercodeClient
is the core client for all JavaScript frontend SDKs, providing audio recording, playback, and real-time communication with the Layercode pipeline.
Usage Example
Constructor Options
Options for the LayercodeClient.
The ID of your Layercode pipeline.
The session ID to resume a previous session (optional).
The endpoint to authorize the session (should return a client_session_key
and session_id
).
Optional metadata to send with the session authorization request.
Callback when the client connects. Receives an object: { sessionId: string | null }
.
Callback when the client disconnects.
Callback when an error occurs. Receives an Error
object.
Callback for custom data messages from the server.
Callback for changes in the user’s microphone amplitude (number, 0-1).
Callback for changes in the agent’s audio amplitude (number, 0-1).
Callback when the client’s status changes. Receives a string: "disconnected" | "connecting" | "connected" | "error"
.
Methods
connect(): Promise<void> Connects to the Layercode pipeline, authorizes the session, and starts audio capture and playback.
disconnect(): Promise<void> Disconnects from the Layercode pipeline, stops audio capture and playback, and closes the WebSocket.
Turn-taking (Push-to-Talk)
Layercode supports both automatic and push-to-talk turn-taking. For push-to-talk, use these methods to signal when the user starts and stops speaking:
triggerUserTurnStarted(): Promise<void> Signals that the user has started speaking (for push-to-talk mode). Interrupts any agent audio playback.
triggerUserTurnFinished(): Promise<void> Signals that the user has finished speaking (for push-to-talk mode).
Events & Callbacks
- onConnect: Called when the connection is established. Receives
{ sessionId }
. - onDisconnect: Called when the connection is closed.
- onError: Called on any error (authorization, WebSocket, audio, etc).
- onDataMessage: Called when a custom data message is received from the server (see
response.data
events from your backend). - onUserAmplitudeChange: Called with the user’s microphone amplitude (0-1).
- onAgentAmplitudeChange: Called with the agent’s audio amplitude (0-1).
- onStatusChange: Called when the status changes (
"disconnected"
,"connecting"
,"connected"
,"error"
).
Notes & Best Practices
- The SDK manages microphone access, audio streaming, and playback automatically.
- The
metadata
option allows you to set custom data which is then passed to your backend webhook (useful for user/session tracking). - The
sessionId
can be used to resume a previous session, or omitted to start a new one.
Authorizing Sessions
To connect a client (browser) to your Layercode voice pipeline, you must first authorize the session. The SDK will automatically send a POST request to the path (or url if your backend is on a different domain) passed in the authorizeSessionEndpoint
option. In this endpoint, you will need to call the Layercode REST API to generate a client_session_key
and session_id
(if it’s a new session).
authorizeSessionEndpoint
to the full URL (e.g., https://your-backend.com/api/authorize
).Why is this required?
Your Layercode API key should never be exposed to the frontend. Instead, your backend acts as a secure proxy: it receives the frontend’s request, then calls the Layercode authorization API using your secret API key, and finally returns the client_session_key
to the frontend.
This also allows you to authenticate your user, and set any additional metadata that you want passed to your backend webhook.
How it works:
-
Frontend: The SDK automatically sends a POST request to your
authorizeSessionEndpoint
with a request body. -
Your Backend: Your backend receives this request, then makes a POST request to the Layercode REST API
/v1/pipelines/authorize_session
endpoint, including yourLAYERCODE_API_KEY
as a Bearer token in the headers. -
Layercode: Layercode responds with a
client_session_key
(and asession_id
), which your backend returns to the frontend. -
Frontend: The SDK uses the
client_session_key
to establish a secure WebSocket connection to Layercode.
Example backend authorization endpoint code:
For a Python backend example see the FastAPI backend guide.