TokenRa
中文Get API Key

Audio · OpenAI

GPT-4o mini TTS API Docs: Parameters and Examples

GPT-4o mini TTS is OpenAI’s text-to-speech model, served on TokenRa behind the standard /v1/audio/speech endpoint. Send JSON with the text and a voice and the response body is the audio file itself. Steer the tone with instructions, or set stream_format to sse to receive audio chunks as they are generated. Text input costs $0.9 per million tokens and generated audio $31.5 per million tokens, roughly $0.03 to $0.04 per minute of speech.

Model ID
gpt-4o-mini-tts
Provider
OpenAI
Mode
Synchronous or SSE stream
Input price
$0.9 / 1M tokens
Output price
$31.5 / 1M audio tokens
Max input
4 096 characters

Endpoint

API endpoint

POSThttps://tokenra.io/v1/audio/speech
Send application/json. Returns the audio file in the response body, or an SSE stream when stream_format is sse.

Parameters

Request parameters

ParameterTypeRequiredDefaultDescription
modelstringYes—Must be gpt-4o-mini-tts.
inputstringYes—The text to speak, up to 4 096 characters. Longer text returns 400; split it and join the audio.
voicestringYes—One of the voices listed below. A missing or unknown voice returns 400.
instructionsstringNo—Free-text direction for tone, pace, accent or emotion, e.g. “Speak in a calm, warm tone.”
response_formatstringNomp3One of mp3, opus, aac, wav, pcm.
speednumberNo1Speed from 0.25 to 4.0. Values outside the range return 400.
stream_formatstringNoaudioSet to sse to stream Server-Sent Events instead of one audio body. See Streaming below.

Output

Audio formats and voices

ValueContent-TypeUse when
mp3audio/mpegDefault. Plays everywhere.
opusaudio/opusLow-latency streaming and web audio.
aacaudio/aacMobile playback and video editors.
wavaudio/wavUncompressed audio for editing.
pcmaudio/pcmRaw 24 kHz 16-bit mono samples with no header.

Voices

Thirteen voices are available: alloy, ash, ballad, coral, echo, fable, nova, onyx, sage, shimmer, verse, marin, cedar. An unknown name returns 400.

Examples

Code examples

cURL

cURL
curl https://tokenra.io/v1/audio/speech \
  -H "Authorization: Bearer $TOKENRA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini-tts",
    "input": "Today is a wonderful day to build something people love!",
    "voice": "coral",
    "instructions": "Speak in a cheerful and positive tone.",
    "response_format": "mp3"
  }' \
  --output speech.mp3

Python (openai SDK)

Python
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_TOKENRA_API_KEY",
    base_url="https://tokenra.io/v1",
)

with client.audio.speech.with_streaming_response.create(
    model="gpt-4o-mini-tts",
    voice="coral",
    input="Today is a wonderful day to build something people love!",
    instructions="Speak in a cheerful and positive tone.",
    response_format="mp3",
) as response:
    response.stream_to_file("speech.mp3")

JavaScript (fetch)

JavaScript
import { writeFile } from "node:fs/promises";

const res = await fetch("https://tokenra.io/v1/audio/speech", {
  method: "POST",
  headers: {
    Authorization: "Bearer " + process.env.TOKENRA_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "gpt-4o-mini-tts",
    voice: "alloy",
    input: "Hello from TokenRa.",
    response_format: "mp3",
  }),
});
if (!res.ok) throw new Error(res.status + " " + (await res.text()));
await writeFile("speech.mp3", Buffer.from(await res.arrayBuffer()));

Streaming (SSE)

cURL
curl -N https://tokenra.io/v1/audio/speech \
  -H "Authorization: Bearer $TOKENRA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini-tts",
    "input": "Streaming speech from TokenRa.",
    "voice": "alloy",
    "stream_format": "sse"
  }'

Each speech.audio.delta event carries a base64 audio chunk in the audio field. The stream ends with a speech.audio.done event whose usage object holds input_tokens, output_tokens and total_tokens.

Billing

Billing

Every request is metered on two items:

  • Input text: $0.9 per million tokens.
  • Generated audio: $31.5 per million audio tokens. A normal call counts 1 000 tokens per minute of audio, rounded up to the next second. With stream_format set to sse, the token count the upstream reports in the final event is billed instead.
Example: a sentence of 9 input tokens that produces 4 seconds of speech uses 67 audio tokens, so the cost is 9 × $0.9/M + 67 × $31.5/M ≈ $0.0021.

Errors

Error codes

HTTPTypical messageCause
400input is required, voice is requiredA required field is missing.
400invalid voice: ...Unknown voice name.
400speed must be between 0.25 and 4.00The speed value is out of range.
400input exceeds maximum length of 4096 charactersInput is longer than 4 096 characters.
400invalid response_format: ...Unsupported output format.
401Invalid tokenAPI key missing or revoked.
403—Balance too low, or the token has no access to gpt-4o-mini-tts.
503model_not_foundNo available channel for the model in your group.

FAQ

Frequently asked questions

How is billing calculated?

Input text is $0.9 per million tokens and generated audio is $31.5 per million audio tokens. One minute of speech is about 1 000 audio tokens, so it costs roughly $0.03 to $0.04.

Which audio formats can I request?

Set response_format to mp3 (default), opus, aac, wav or pcm.

How do I control tone and style?

Pass a plain-language sentence in instructions, for example “Speak slowly and warmly.” Pair it with a voice such as coral or sage.

How long can the input be?

Up to 4 096 characters per request. For longer text, split it at sentence boundaries, send several requests and join the audio.

Can I stream the audio?

Yes. Set stream_format to sse and read the Server-Sent Events as they arrive.

Why is the response body empty in Postman?

The response is binary audio, not text, so the body panel shows nothing. Use Send and Download (or Save Response) to write it to an .mp3 file.