LayercodeClient
TheLayercodeClient
is the core client for all JavaScript frontend SDKs, providing audio recording, playback, and real-time communication with the Layercode agent.
Usage Example
Constructor Options
Options for the LayercodeClient.
The ID of your Layercode agent.
The conversation ID to resume a previous conversation (optional).
The endpoint to authorize the session (should return a
client_session_key
and session_id
).
Note: From Mon Sep 1, 12:00 UTC, the response will include conversation_id
instead of session_id
(see REST API page).Optional metadata to send with the session authorization request.
Callback when the client connects. Receives an object:
{ conversationId: 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 agent, authorizes the session, and starts audio capture and playback.
disconnect(): Promise<void> Disconnects from the Layercode agent, 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
{ conversationId }
. - 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"
).
Callback when the active input/output device changes in the browser. Useful for handling device disconnects and switches.
Callback when VAD detects user speech start/stop. Receives a boolean.
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
conversationId
can be used to resume a previous conversation, or omitted to start a new one.
Authorizing Sessions
To connect a client (browser) to your Layercode voice agent, 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 theauthorizeSessionEndpoint
option. In this endpoint, you will need to call the Layercode REST API to generate a client_session_key
and conversation_id
(if it’s a new conversation).
If your backend is on a different domain, set
authorizeSessionEndpoint
to the full URL (e.g., https://your-backend.com/api/authorize
).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/agents/web/authorize_session
endpoint, including yourLAYERCODE_API_KEY
as a Bearer token in the headers. -
Layercode:
Layercode responds with a
client_session_key
(and aconversation_id
), which your backend returns to the frontend. -
Frontend:
The SDK uses the
client_session_key
to establish a secure WebSocket connection to Layercode.