Webhook SSE API
Webhook SSE API
When using the Hosted Backend on every complete user conversation turn, the server sends a webhook to the webhook URL provided in the Pipeline configuration (set in the Layercode Dashboard).
Webhook Request Payload
The webhook request body contains a JSON object with the following fields:
Field | Type | Description |
---|---|---|
text | string | The transcribed text from the user’s speech |
connection_id | string | A unique identifier for the current connection. This changes each time the user reconnects to the same session |
session_id | string | A unique identifier for the current session |
type | string | The type of webhook event. Can be “MESSAGE” for regular turns or “SESSION_START” for session initialization |
turn_id | string | A unique identifier for the current conversation turn |
Example webhook request JSON payload:
Verifying the webhook request
To confirm that webhook requests are from Layercode you should always verify the webhook request.
Webhook SSE Response Format
Your response should be a stream of SSE events.
The SSE stream format is JSON based. Each JSON message below must be sent as a separate SSE event in the format below. Each SSE event consists of a data:
field, followed by a stringified JSON object and two newlines (\n\n
).
Event Types
Text to speech
Send any number of response.tts
type events to the client containing text, which the Layercode voice pipeline will convert to speech and send to the client as response.audio
events. The text will be processed in sentence chunks for natural speech delivery.
JSON data to forward to the client
Your webhook SSE can return response.data type events, which will be forwarded directly to the browser client. This is ideal for updating UI and state in the browser. If you want to pass text or json deltas instead of full objects, you can simply pass a json object like { "delta": "text delta..." }
and accumulate and render the delta in the client browser.
Note: Multiple response.tts
and response.data
messages can be sent over a single SSE response (e.g. so the agent can say “I’m just getting your results”, do a tool call, return the results in response.data, and then speak a summary of the results).
Ending the SSE response
Once the webhook SSE response is ready to be closed, you must send the following final event: