Text To Speech
Text-To-Speech WebSocket
WSS
The Text-to-Speech WebSocket API is designed to generate audio from partial text input while ensuring consistency throughout the generated audio. Although highly flexible, the WebSocket API isn’t a one-size-fits-all solution. It’s well-suited for scenarios where:
Request Fields:
Request Fields:
Response Fields:
Response: No response message.
Response: Connection closes.
Error Response Fields:
- The input text is being streamed or generated in chunks.
- Real-time audio generation is required with low latency.
- You need to send text incrementally as it becomes available.
- The entire input text is available upfront. Given that the generations are partial, some buffering is involved, which could potentially result in slightly higher latency compared to a standard HTTP request.
- You want to quickly experiment or prototype. Working with WebSockets can be harder and more complex than using a standard HTTP API, which might slow down rapid development and testing.
Endpoint
Connection URL
Authentication
Requires API key authentication viax-api-key query parameter or in the initial connection message.
Query Parameters
Message Types
Initialize Connection
After establishing the WebSocket connection, you must send an initialization message. Request:
Response:
Send Text
Send text chunks for audio generation. Request:
Response:
Clear Buffer
Clear the current text buffer. Request:Close Connection
Close the WebSocket connection gracefully. Request:Error Responses
If an error occurs, you’ll receive:Example Usage
Best Practices
- Always initialize: Send
initConnectionimmediately after opening the connection - Handle errors: Check for error messages in responses
- Flush when done: Use
flush: truewhen sending the last text chunk to ensure all audio is generated - Close gracefully: Send
closeConnectionbefore closing the WebSocket - Buffer audio: Collect audio chunks and play them sequentially for smooth playback
