Python SDK reference
This page documents the public API for the Speech Engine Python SDK (elevenlabs).
Getting a Speech Engine resource
Retrieve a SpeechEngineResource by its engine ID. The returned object provides methods to start a server, verify requests, or create individual sessions.
SpeechEngineResource
Properties
serve
Start a standalone WebSocket server. Blocks until stopped.
Disabling authentication
By default serve() verifies the X-Elevenlabs-Speech-Engine-Authorization header on every incoming connection. If your server sits behind an infrastructure layer that already restricts incoming traffic to ElevenLabs (typically an IP allowlist scoped to ElevenLabs’ egress ranges), you can skip JWT verification by passing disable_auth=True:
When auth is disabled the server accepts any client that can reach it and emits a UserWarning on startup.
Only use disable_auth=True if you have an IP allowlist, custom header values or equivalent
network-level restriction in front of the server. Without one, anyone on the internet can open a
session and consume your compute and downstream LLM quota.
verify_request
Verify that an incoming request originates from the ElevenLabs Speech Engine API. Checks the X-Elevenlabs-Speech-Engine-Authorization header for a valid JWT signed with the SHA-256 hash of your API key.
Only needed when managing the WebSocket upgrade yourself. When using serve(), verification is handled automatically (unless disable_auth=True was set).
Returns: bool — True if the request is valid.
create_session
Wrap an accepted WebSocket in a SpeechEngineSession. Use this for custom server integration (e.g. FastAPI, Starlette, or manual WebSocket handling).
Returns: SpeechEngineSession
SpeechEngineSession
Wraps a single WebSocket connection. Each connection represents one conversation. The session emits events for transcripts and lifecycle changes, and provides methods to send LLM responses back.
When a new transcript arrives, the previous transcript handler is cancelled automatically, interrupting any in-flight LLM call.
Properties
on
Register a handler for an event. Returns the session for chaining.
off
Remove a previously registered handler.
once
Register a handler that fires once then removes itself.
send_response
Send an LLM response back to the Speech Engine API for text-to-speech synthesis. Must be called inside an on_transcript handler. Calling it outside of a handler emits a warning and returns without sending.
The SDK auto-detects and extracts text from the following LLM stream formats:
run
Run the receive loop until the WebSocket closes. This is the main entry point after constructing a session manually via create_session().
close
Close the session and the underlying WebSocket connection.
Callbacks
The keyword arguments passed to serve(). All callbacks are optional. Handlers can be synchronous or asynchronous (coroutine) functions.
Events
When using session.on() directly instead of callbacks, these are the event names and their handler signatures.
Event name constants are available for type-safe usage:
ConversationMessage
A single message in the conversation history. The full transcript is passed to on_transcript on every turn.
Wire protocol
For reference, these are the JSON messages exchanged over the WebSocket connection. The SDK handles serialization and deserialization automatically.