Skip to main content

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​

  1. 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 to gpt-4o-mini-tts and set Mode to Audio Speech - /audio/speech, then click Add Model. Health checks need a voice, so set health_check_voice in the deployment's model info.

    model_list:
    - model_name: gpt-4o-mini-tts
    spanway_params:
    model: openai/gpt-4o-mini-tts
    api_key: os.environ/OPENAI_API_KEY
    model_info:
    mode: audio_speech
    health_check_voice: alloy
  2. Create a key in Virtual Keys (/ui/api-keys) and pick the model under Models.

  3. 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
  4. 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.

  5. 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​

FieldMeaning
inputText to speak.
voiceVoice name. Names are provider specific, and the gateway forwards them as given.
response_formatmp3, opus, aac, flac, wav or pcm where supported.
speedPlayback speed.
instructionsStyle 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​

StatusWhat it meansFix
400 BadRequestErrorinput or voice is missing, or the voice name is unknown to the provider.Use a voice name the provider lists.
400 on response_formatThe provider does not offer that format.Pick one of the formats the provider supports.
401 AuthenticationErrorThe key is missing, wrong or expired.Use a live key.
403 PermissionDeniedErrorThe 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 RateLimitErrorBudget or rate limit reached.Raise the limit.
Health check fails with a voice errorhealth_check_voice is not set on the deployment.Add it to the deployment's model info.

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.