POST /v1/audio/speech and get the spoken audio back in the format you ask for. The examples use a client created as in Installation.
import { writeFile } from "node:fs/promises";
const audio = await client.generateSpeech({
model: "openai/gpt-4o-mini-tts",
input: "Hello world",
voice: "alloy",
format: "mp3",
});
await writeFile("hello.mp3", Buffer.from(await audio.arrayBuffer()));
audio = client.generate_speech({
"model": "openai/gpt-4o-mini-tts",
"input": "Hello world",
"voice": "alloy",
"format": "mp3",
})
with open("hello.mp3", "wb") as file:
file.write(audio)
audio, err := client.CreateSpeech(context.Background(), map[string]interface{}{
"model": "openai/gpt-4o-mini-tts",
"input": "Hello world",
"voice": "alloy",
"format": "mp3",
})
if err != nil {
panic(err)
}
fmt.Printf("%T\n", audio) // check the concrete type before writing it to a file
var audio = await client.CreateSpeech(new Dictionary<string, object>
{
["model"] = "openai/gpt-4o-mini-tts",
["input"] = "Hello world",
["voice"] = "alloy",
["format"] = "mp3",
});
$audio = $client->createSpeech([
'model' => 'openai/gpt-4o-mini-tts',
'input' => 'Hello world',
'voice' => 'alloy',
'format' => 'mp3',
]);
audio = client.generate_speech(
model: 'openai/gpt-4o-mini-tts',
input: 'Hello world',
voice: 'alloy',
format: 'mp3'
)
String body = """
{
"model": "openai/gpt-4o-mini-tts",
"input": "Hello world",
"voice": "alloy",
"format": "mp3"
}
""";
byte[] audio = client.rawClient().requestBytes("POST", "/audio/speech", null, null, body);
java.nio.file.Files.write(java.nio.file.Path.of("hello.mp3"), audio);
Method per language
| Language | Method | Returns |
|---|---|---|
| TypeScript | client.generateSpeech(request) | A Blob |
| Python | client.generate_speech(request) or client.audio.speech.create(request) | bytes |
| Go | client.CreateSpeech(ctx, req) | interface{}; check the concrete type before writing it to a file |
| C# | client.CreateSpeech(request) | object?; check the concrete type before writing it to a file |
| PHP | $client->createSpeech($request) | The audio; check the value’s type before writing it to a file |
| Ruby | client.generate_speech(...), alias create_speech | The audio; check the value’s class before writing it to a file |
| Java | No speech method. client.rawClient().requestBytes("POST", "/audio/speech", ...) posts the JSON body | byte[] |
post() helper parses responses as JSON, so it can’t return audio.
Parameters
Field names are the JSON body fields in every SDK. Useformat: the API also accepts response_format, but the TypeScript request type rejects it.
| Field | Notes |
|---|---|
model | Required. A speech model ID. |
input | Required. Text to speak. Most providers accept up to 4,096 characters; ElevenLabs models allow 5,000 to 40,000 depending on the model. |
voice | A provider-normalised voice alias (recommended), the provider’s own voice value, or an object with id or name. If the voice isn’t valid for the routed provider, the API returns 400 with param: "voice". |
format | mp3, wav, ogg, aac, flac, opus or pcm. |
speed | Playback speed, 0.25 to 4. |
instructions | Style guidance, where the model supports it. |
config | Provider-specific settings; see below. |
provider | Provider routing preferences. |
Set a provider’s own voice
Pass the provider’s exact voice underconfig. The same nested object works in every SDK’s request map.
// ElevenLabs: pick an exact voice ID
await client.generateSpeech({
model: "eleven-labs/eleven-v3",
input: "Hello from ElevenLabs",
voice: "rachel",
config: {
elevenlabs: {
voice_id: "21m00Tcm4TlvDq8ikWAM",
},
},
});
// Google: pick an exact voice name
await client.generateSpeech({
model: "google/gemini-3.1-flash-tts-preview",
input: "Hello from Gemini",
voice: "kore",
config: {
google: {
voice_name: "Kore",
},
},
});
# ElevenLabs: pick an exact voice ID
client.generate_speech({
"model": "eleven-labs/eleven-v3",
"input": "Hello from ElevenLabs",
"voice": "rachel",
"config": {"elevenlabs": {"voice_id": "21m00Tcm4TlvDq8ikWAM"}},
})
# Google: pick an exact voice name
client.generate_speech({
"model": "google/gemini-3.1-flash-tts-preview",
"input": "Hello from Gemini",
"voice": "kore",
"config": {"google": {"voice_name": "Kore"}},
})