FastAPI
Implement the Layercode Webhook SSE API in your FastAPI backend.
This guide shows you how to implement the Layercode Webhook SSE API in a Python backend using FastAPI. You’ll learn how to set up a webhook endpoint that receives transcribed messages from the Layercode voice pipeline 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-fastapi
Prerequisites
- Python 3.8+
- FastAPI and Uvicorn for serving your API
- A Layercode account and pipeline (sign up here)
- (Optional) An API key for your LLM provider (we recommend Google Gemini)
Setup
pip:
uv:
Edit your .env environment variables. You’ll need to add:
GOOGLE_GENERATIVE_AI_API_KEY
- Your Google AI API keyLAYERCODE_WEBHOOK_SECRET
- Your Layercode pipeline’s webhook secret, found in the Layercode dashboard (go to your pipeline, click Edit in the Your Backend Box and copy the webhook secret shown)LAYERCODE_API_KEY
- Your Layercode API key found in the Layercode dashboard settings
Create Your FastAPI Webhook Endpoint
Here’s an example of a our Layercode webhook endpoint, which generates responses using Google Gemini and streams them back to the frontend as SSE events. See the GitHub repo for the full example.
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.
- LLM call: Calls Google Gemini Flash 2.0 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 asession_id
) to the frontend. This key is required for the frontend to establish a secure WebSocket connection to Layercode.
See the GitHub repo for the full example.
Running Your Backend
Start your FastAPI server with Uvicorn:
Configure the Layercode Webhook endpoint
In the Layercode dashboard, go to your pipeline settings. Under Your Backend, click edit, and here you can set the URL of the webhook endpoint.
If running this example locally, setup a tunnel (we recommend cloudflared which is free for dev) to your localhost so the Layercode webhook can reach your backend. Follow our tunnelling guide.
Test Your Voice Agent
There are two ways to test your voice agent:
- Use the Layercode playground tab, found in the pipeline in the Layercode dashboard.
- Follow one of our Frontend Guides to build a Web Voice Agent that uses this backend.