JavaScript SDK reference
This page documents the public API for the Speech Engine JavaScript SDK (@elevenlabs/elevenlabs-js).
Getting a Speech Engine resource
Retrieve a SpeechEngineResource by its engine ID. The returned object provides methods to attach to an existing HTTP server, start a standalone server, or create individual sessions.
SpeechEngineResource
Properties
attach
Attach to an existing Node.js HTTP server and begin accepting Speech Engine connections at the given path. Use this when you already have an HTTP server (e.g. Express, Fastify, or a plain http.createServer()) and want to add Speech Engine alongside your existing routes.
Handles WebSocket upgrades, path routing, and request verification automatically. Returns a SpeechEngineAttachment whose close() method stops accepting connections without affecting the HTTP server.
A shortcut is available directly on the client, combining get() and attach() into a single call:
verifyRequest
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 attach() or SpeechEngineServer, verification is handled automatically.
Returns: Promise<boolean> — true if the request is valid.
createSession
Wrap an accepted WebSocket in a SpeechEngineSession. Use this for custom server integration or manual WebSocket handling.
Returns: SpeechEngineSession
SpeechEngineServer
A standalone WebSocket server that accepts Speech Engine connections without requiring an existing HTTP server. Use this when your server’s only purpose is handling Speech Engine connections.
For integration with an existing HTTP server (e.g. Express, Fastify), use engine.attach() instead.
Constructor options
start
Start the standalone WebSocket server on the configured port. Verifies each incoming connection against the ElevenLabs API using the configured API key.
stop
Stop the WebSocket server and close all active connections.
handleConnection
Wrap an existing WebSocket in a SpeechEngineSession with the server’s callbacks wired up. Use this when you manage your own WebSocket server and want to wrap individual connections.
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’s abort signal is fired, 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.
sendResponse
Send an LLM response back to the Speech Engine API for text-to-speech synthesis. Must be called inside an onTranscript 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:
close
Close the session and the underlying WebSocket connection.
SpeechEngineAttachment
Returned by engine.attach(). Controls the lifecycle of the WebSocket server without affecting the HTTP server it was attached to.
close
Stop accepting new connections, remove the upgrade listener from the HTTP server, and close the underlying WebSocket server.
Callbacks
The callback object passed to attach() or SpeechEngineServer. All callbacks are optional.
The onTranscript handler receives an AbortSignal that fires when the user interrupts mid-response.
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:
TranscriptMessage
A single message in the conversation history. The full transcript is passed to onTranscript on every turn.
Wire protocol
For reference, these are the JSON messages exchanged over the WebSocket connection. The SDK handles serialization and deserialization automatically.