Authorize Client Session
To connect a client (browser or mobile app) to a Layercode voice agent, 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/agents/web/authorize_session
) using your LAYERCODE_API_KEY
. Your backend then returns the client_session_key
to the frontend.
Scheduled change: Monday 1 September at 12:00 UTC — the response body will return
conversation_id
instead of session_id
. Until then, you will continue to receive session_id
. Plan your upgrade accordingly.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 agent the client should connect to.
(Optional) The conversation ID to resume an existing conversation. If not provided, a new conversation will be created.
Response
The key your frontend uses to connect to the Layercode WebSocket API.
The unique conversation ID.
Optional configuration for this session used by the frontend SDK. When present, it can include:
transcription.trigger
, transcription.automatic
, transcription.can_interrupt
, and VAD settings such as vad.enabled
, vad.gate_audio
, vad.buffer_frames
, vad.model
, vad.positive_speech_threshold
, vad.negative_speech_threshold
, vad.redemption_frames
, vad.min_speech_frames
, vad.pre_speech_pad_frames
, vad.frame_samples
.Example Request
Example Response
Error Responses
Error message describing the problem.
400
– Invalid or missing bearer token, invalid agent ID, missing or invalid conversation ID.402
– Insufficient balance for the organization.
Example: Backend Endpoint (Next.js)
Here’s how you might implement an authorization endpoint in your backend (Next.js example):Next.js app/api/authorize/route.ts
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.Agents
List Agents
Bearer token using your
LAYERCODE_API_KEY
.Response
Returns all agents.Each agent object includes
id
, name
, type
, agent_template_id
, created_at
, updated_at
, and assigned_phone_numbers
(array of phone number assignments with phone_number
, twilio_sid
, friendly_name
, assigned_at
).Example
Create Agent From Template
Bearer token using your
LAYERCODE_API_KEY
.Must be
application/json
.Optional template ID to initialize the agent configuration. If omitted, the default recommended template is used.
Response
Returns the newly created agent record, including configuration and webhook secret.Unique identifier for the agent.
Human-friendly name assigned by Layercode.
Agent type (currently
voice
).Full pipeline configuration cloned from the template.
Secret used to validate incoming webhooks.
ID of the template used to create the agent.
Get Agent Details
Bearer token using your
LAYERCODE_API_KEY
.Response
Returns the agent.Agent ID.
Agent display name.
Current pipeline configuration.
Array of phone number assignments for this agent.
Update Agent Configuration
Bearer token using your
LAYERCODE_API_KEY
.Must be
application/json
.URL for production webhooks. When provided,
demo_mode
is automatically disabled.Response
Returns the updated agent record with the new configuration.Sessions
Get Session Details
Bearer token using your
LAYERCODE_API_KEY
.Response
Returns JSON with details about the session, transcript, and recording status.Connection ID for the session.
ID of the agent.
ISO timestamp when the connection started.
ISO timestamp when the connection ended (if ended).
Total connection duration in milliseconds.
Custom metadata associated with the session.
Caller phone number (Twilio), if applicable.
Caller country code (Twilio), if applicable.
Agent phone number (Twilio), if applicable.
Agent phone number country code (Twilio), if applicable.
IP address of the connection.
Country code derived from IP address when available.
Total seconds of user speech.
Total seconds of generated speech.
Processing latency in milliseconds.
Array of transcript entries. Each entry includes:
timestamp
, user_message
, assistant_message
, latency_ms
.One of
not_available
, in_progress
, completed
.If
recording_status
is completed
, a URL to download the WAV recording for this session connection.Example
Download Session Recording
Bearer token using your
LAYERCODE_API_KEY
.Recordings are generated after a session completes. If a recording is still processing, the details endpoint will return
recording_status: “in_progress”
.Once your frontend receives the
client_session_key
, it can connect to the Layercode WebSocket API to start streaming audio.Calls
Initiate Outbound Call
The phone number assigned to your Layercode Agent that will make the call.
Remember: the from_phone_number must be a number already assigned to your Laycode Agent in the dashboard.
The phone number to call (e.g., your mobile number for testing).
Response
The unique conversation ID.
A Session (associated with the returned conversation_id) will be created shortly after once Twilio initiates the call)
Example Request
Example Response
Error Responses
Error message describing the problem.
400
– Invalid or missing bearer token, missing or request body, invalid from_phone_number (i.e. not assigned to the agent specified in the url).429
– Account session concurrency limit reached.402
– Insufficient balance for the organization.
Twilio Voice
TwiML Webhook
Use this endpoint as the Voice webhook in your Twilio phone number configuration. Layercode validates the incoming request, authorizes a session, and returns TwiML that connects the call to your agent’s WebSocket stream.Signature supplied by Twilio for request verification. Required when you have stored Twilio credentials in Layercode.
Call direction reported by Twilio (e.g.,
inbound
or outbound-api
).Caller phone number.
Caller country code supplied by Twilio.
Phone number assigned to your agent.
Destination country code supplied by Twilio.
Response
Returns TwiML that streams the call to the Layercode Twilio WebSocket endpoint.The response Streaming URL is generated dynamically for each request. Do not cache or reuse the client session key.