Text to speech
/v1/audio/speech turns text into audio in the OpenAI format. The gateway serves OpenAI, Azure OpenAI, Vertex AI, Gemini, ElevenLabs, Deepgram, Amazon Polly and other speech providers behind the same request, returns the audio bytes, and prices the call per character or per token according to the price map.
Scenario
A customer notification service reads order updates aloud over the phone. The admin registers a speech model with a default voice for health checks, the service calls the gateway with the text and the voice, and the audio bytes come back ready to play. The service's key has a budget, so a runaway job cannot spend more than planned.
Steps
-
Register the model. In the Admin UI open Models + Endpoints (
/ui/models-and-endpoints), click Add Model, pick the Provider, choose the model, set the Public Model Name togpt-4o-mini-ttsand set Mode to Audio Speech - /audio/speech, then click Add Model. Health checks need a voice, so sethealth_check_voicein the deployment's model info.model_list:- model_name: gpt-4o-mini-ttsspanway_params:model: openai/gpt-4o-mini-ttsapi_key: os.environ/OPENAI_API_KEYmodel_info:mode: audio_speechhealth_check_voice: alloy -
Create a key in Virtual Keys (
/ui/api-keys) and pick the model under Models. -
Generate speech and save the audio.
curl -X POST http://localhost:4000/v1/audio/speech \-H "Authorization: Bearer $SPANWAY_API_KEY" \-H "Content-Type: application/json" \-d '{"model": "gpt-4o-mini-tts","input": "Your order has shipped and will arrive on Thursday.","voice": "alloy","response_format": "mp3"}' \--output speech.mp3 -
Try it in the browser. Open Playground (
/ui/playground), set Endpoint Type to/v1/audio/speech, pick the model and a Voice, type the text and send. An audio player appears in the conversation area. -
Check the cost in Logs (
/ui/logs).
Expected result
The response body is the audio data with a matching Content-Type, for example audio/mpeg for mp3. The file plays the sentence in the chosen voice, and the Logs row shows the character count and the cost.
Request fields
| Field | Meaning |
|---|---|
input | Text to speak. |
voice | Voice name. Names are provider specific, and the gateway forwards them as given. |
response_format | mp3, opus, aac, flac, wav or pcm where supported. |
speed | Playback speed. |
instructions | Style guidance for models that support it. |
Streaming returns audio chunks as they are produced.
Gateway features
Speech requests are authenticated, budgeted and logged like any other call. The input text is screened by pre_call guardrails, and the request size is limited by general_settings.max_request_size_mb. See health checks for the voice setting health checks use.
Errors
| Status | What it means | Fix |
|---|---|---|
400 BadRequestError | input or voice is missing, or the voice name is unknown to the provider. | Use a voice name the provider lists. |
400 on response_format | The provider does not offer that format. | Pick one of the formats the provider supports. |
401 AuthenticationError | The key is missing, wrong or expired. | Use a live key. |
403 PermissionDeniedError | The key may not use the model, or a guardrail blocked the text. | Add the model to the key, or read the guardrail result in Logs. |
429 RateLimitError | Budget or rate limit reached. | Raise the limit. |
| Health check fails with a voice error | health_check_voice is not set on the deployment. | Add it to the deployment's model info. |
Related
- Audio transcription for speech to text.
- Realtime API for live voice sessions.
- Health checks.
Providers
The gateway's endpoint support matrix marks 13 provider integrations as supporting this endpoint:
AIHubMix (aihubmix), AWS - Polly, Azure, Azure AI, Azure Text, Custom OpenAI (custom_openai), Eden AI, ElevenLabs, Oobabooga (oobabooga), OpenAI, RunwayML, Text Completion OpenAI, Google - Vertex AI.