React Frontend SDK
Connect your React application to Layercode pipelines and build web and mobile voice AI applications.
useLayercodePipeline Hook
The useLayercodePipeline
hook provides a simple way to connect your React app to a Layercode pipeline, handling audio streaming, playback, and real-time communication.
Hook Options
The ID of your Layercode pipeline.
The endpoint to authorize the session (should return a client_session_key
and session_id
).
The session ID to resume a previous session (optional).
Any metadata included here will be passed along to your backend with all webhooks.
Callback when the connection is established. Receives an object: { sessionId: string | null }
.
Callback when the connection is closed.
Callback when an error occurs. Receives an Error
object.
Callback for custom data messages from the server (see response.data
events from your backend).
Return Values
The useLayercodePipeline
hook returns an object with the following properties:
State
The connection status. One of "initializing"
, "disconnected"
, "connecting"
, "connected"
, or "error"
.
Real-time amplitude of the user’s microphone input (0-1). Useful for animating UI when the user is speaking.
Real-time amplitude of the agent’s audio output (0-1). Useful for animating UI when the agent is speaking.
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(): void Signals that the user has started speaking (for push-to-talk mode). Interrupts any agent audio playback.
triggerUserTurnFinished(): void Signals that the user has finished speaking (for push-to-talk mode).
Notes & Best Practices
- The hook 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.