This guide shows you how to implement the Layercode Webhook SSE API with Cloudflare Workers using Hono. You’ll learn how to set up a webhook endpoint that receives transcribed messages from the Layercode voice agent and streams the agent’s responses back to the frontend, to be turned into speech and spoken back to the user. You can test your backend using the Layercode dashboard playground or by following the Build a Web Voice Agent guide. Example code: layercodedev/example-backend-honoDocumentation Index
Fetch the complete documentation index at: https://docs.layercode.com/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites
- Node.js 18+
- Hono (Cloudflare Workers compatible)
- A Layercode account and agent (sign up here)
- An API key for your LLM provider (we use Google Gemini in this example)
Setup
.env (automatically read by wrangler) environment variables. You’ll need to add:
GOOGLE_GENERATIVE_AI_API_KEY- Your Google Gemini API keyLAYERCODE_API_KEY- Your Layercode API key, found in the Layercode dashboardLAYERCODE_WEBHOOK_SECRET- Your Layercode agent’s webhook secret, found in the Layercode dashboard (go to your agent, click Connect your backend and copy the webhook secret shown)
Create Your Hono Handler
Here’s a simplified example of the core functionality needed to implement the Layercode webhook endpoint:3. How It Works
- /agent endpoint: Receives POST requests from Layercode with the user’s transcribed message, session, and turn info. The webhook request is verified as coming from Layercode.
- Session management: Keeps track of conversation history per session (in-memory for demo; use a store for production).
- LLM call: Calls Gemini 2.5 Flash Lite with the system prompt, message history, and user’s new transcribed message.
- SSE streaming: As soon as the LLM starts generating a response, the backend streams the output back as SSE messages to Layercode, which converts it to speech and delivers it to the frontend for playback in realtime.
- /authorize endpoint: 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(and optionally aconversation_id) to the frontend. This key is required for the frontend to establish a secure WebSocket connection to Layercode.