Back to blog

How to Generate Audio with a Text-to-Speech API

A text-to-speech API tutorial: synthesize speech with ElevenLabs, OpenAI, Gemini, and Qwen voices through one OpenAI-compatible endpoint, choose voices and formats, steer delivery with instructions, and track the cost of every clip.

A glowing waveform and speaker on a circuit board, representing a text-to-speech API

Text-to-speech quality has jumped, but the APIs haven't converged: ElevenLabs has its own SDK and voice IDs, Gemini returns raw PCM, OpenAI expects its own client. Comparing voices across providers means three integrations before you've heard a single sentence.

LLM Gateway puts every speech model behind one OpenAI-compatible text-to-speech API: POST /v1/audio/speech. One key, one request shape, and the full catalog of TTS models — browse them on the models page with the audio filter.

Generate your first clip

1curl -X POST "https://api.llmgateway.io/v1/audio/speech" \2  -H "Authorization: Bearer $LLM_GATEWAY_API_KEY" \3  -H "Content-Type: application/json" \4  -d '{5    "model": "gemini-2.5-flash-preview-tts",6    "input": "Hello, welcome to LLM Gateway!",7    "voice": "Kore"8  }' \9  --output speech.wav

The response body is the audio file itself — no base64 decoding step.

Use your existing OpenAI SDK

1import OpenAI from "openai";2import { writeFileSync } from "fs";3
4const openai = new OpenAI({5  apiKey: process.env.LLM_GATEWAY_API_KEY,6  baseURL: "https://api.llmgateway.io/v1",7});8
9const response = await openai.audio.speech.create({10  model: "eleven-multilingual-v2",11  voice: "Sarah",12  input: "Hello, welcome to LLM Gateway!",13});14
15writeFileSync("speech.mp3", Buffer.from(await response.arrayBuffer()));

Swapping providers is a change to model and voice — the request stays identical, which makes A/B testing voices across providers a loop instead of a project.

Voices, formats, and delivery style

Every provider ships its own voice roster, and the gateway defaults sensibly per model family (Kore on Gemini, alloy on OpenAI, Sarah on ElevenLabs). Three parameters shape the output:

  • voice — a prebuilt voice name; ElevenLabs also accepts raw voice IDs directly.
  • response_formatmp3, wav, pcm, opus, aac, or flac depending on the model. Gemini models produce PCM, which the gateway wraps in a WAV container by default; formats like mp3 are available on OpenAI models.
  • instructions — a style directive prepended to the input, like "Say cheerfully" or "Speak slowly, like narrating a documentary".
1curl -X POST "https://api.llmgateway.io/v1/audio/speech" \2  -H "Authorization: Bearer $LLM_GATEWAY_API_KEY" \3  -H "Content-Type: application/json" \4  -d '{5    "model": "gpt-4o-mini-tts",6    "input": "Your order has shipped and will arrive Tuesday.",7    "voice": "nova",8    "instructions": "Warm, upbeat customer-support tone",9    "response_format": "mp3"10  }' \11  --output update.mp3

The full per-model matrix of voices and formats lives in the speech generation docs.

Going the other way: transcription

The same gateway also exposes POST /v1/audio/transcriptions for speech-to-text, with multipart/form-data file upload or a URL, optional diarization, and key-term boosting. See the transcription docs — combined with TTS you can build full voice loops through one API.

What does a clip cost?

Billing varies by model family: some models bill on token usage reported by the provider, others on input character count. Either way the gateway logs each request with its exact cost, visible per request on the Activity page and aggregated in your usage dashboards. Per-model pricing is on the models page, and API key spend limits cap the blast radius of batch narration jobs.

Frequently Asked Questions

Which text-to-speech models are available through the API?

Everything on the models page with the audio filter — spanning ElevenLabs, OpenAI, Google, and Alibaba voices — through one endpoint. The catalog updates as providers ship new models.

Can I stream the audio as it generates?

Not yet. The endpoint returns the complete audio file in a single response; there is no chunked or SSE streaming output for now.

How do I pick a voice without writing code?

Use the Audio Studio in Lounge — it generates speech from up to three models side by side with per-model voice, format, and speed controls.


Get started: