REST API (Authorize Client Session)
API reference for the Layercode REST API.
Authorize Client Session
To connect a client (browser or mobile app) to a Layercode voice pipeline, you must first authorize the session. This is done by calling the Layercode REST API endpoint below from your backend.
How the authorization flow works:
When using a Layercode frontend SDK (such as @layercode/react-sdk
or @layercode/js-sdk
), the SDK will automatically make a POST request to the authorizeSessionEndpoint
URL that you specify in your frontend code.
This authorizeSessionEndpoint
should be an endpoint on your own backend (not Layercode’s). Your backend receives this request from the frontend, then securely calls the Layercode REST API (https://api.layercode.com/v1/pipelines/authorize_session
) using your LAYERCODE_API_KEY
. Your backend then returns the client_session_key
to the frontend.
Once your frontend receives the client_session_key
, it can connect to the Layercode WebSocket API to start streaming audio.
Your Layercode API key should never be exposed to the frontend. Always call this endpoint from your backend, then return the client_session_key
to your
frontend.
Endpoint
Headers
Bearer token using your LAYERCODE_API_KEY
.
Must be application/json
.
Request Body
The ID of the Layercode pipeline the client should connect to.
(Optional) The session ID to resume an existing session. If not provided, a new session will be created.
Response
The key your frontend uses to connect to the Layercode WebSocket API.
The unique session ID for this conversation.
Example Request
Example Response
Error Responses
Error message describing the problem.
Possible error cases:
400
– Invalid or missing bearer token, invalid pipeline ID, missing or invalid session ID.402
– Insufficient balance for the organization.
Example error response:
Example: Backend Endpoint (Next.js)
Here’s how you might implement an authorization endpoint in your backend (Next.js example):
For other backend frameworks (Express, FastAPI, etc.), the logic is the same: receive a request from your frontend, call the Layercode authorize_session
endpoint
with your API key, and return the client_session_key
to your frontend.
Once your frontend receives the client_session_key
, it can connect to the Layercode WebSocket API to start streaming audio.