Audio
Create a speech
Creates a speech from the supplied request.
요청 본문
The request body.
model
string | enum
필수
One of the available TTS models: tts-1, tts-1-hd, gpt-4o-mini-tts, or gpt-4o-mini-tts-2025-12-15.
enum
input
string
필수
The text to generate audio for. The maximum length is 4096 characters.
instructions
string
Control the voice of your generated audio with additional instructions. Does not work with tts-1 or tts-1-hd.
voice
OpenAI.VoiceIdsShared | enum | object
필수
The voice to use when generating the audio. Supported built-in voices are alloy, ash, ballad, coral, echo, fable, onyx, nova, sage, shimmer, verse, marin, and cedar. You may also provide a custom voice object with an id, for example { "id": "voice_1234" }. Previews of the voices are available in the Text to speech guide. Custom voices must be created from audio samples. Voices created from text prompts are supported only in Live.
enum
enum
response_format
enum
The format to audio in. Supported formats are mp3, opus, aac, flac, wav, and pcm.
stream_format
enum
The format to stream the audio in. Supported formats are sse and audio. sse is not supported for tts-1 or tts-1-hd.
응답
The request has succeeded.
헤더
Transfer-Encoding
string
The TransferEncoding header.
본문
요청
curl https://api.openai.com/v1/audio/speech \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini-tts",
"input": "The quick brown fox jumped over the lazy dog.",
"voice": "alloy"
}' \
--output speech.mp3
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
)
speech = client.audio.speech.create(
input="input",
model="tts-1",
voice="alloy",
)
print(speech)
content = speech.read()
print(content)
import fs from "fs";
import path from "path";
import OpenAI from "openai";
const openai = new OpenAI();
const speechFile = path.resolve("./speech.mp3");
async function main() {
const mp3 = await openai.audio.speech.create({
model: "gpt-4o-mini-tts",
voice: "alloy",
input: "Today is a wonderful day to build something people love!",
});
console.log(speechFile);
const buffer = Buffer.from(await mp3.arrayBuffer());
await fs.promises.writeFile(speechFile, buffer);
}
main();
using System;
using System.IO;
using OpenAI.Audio;
AudioClient client = new(
model: "gpt-4o-mini-tts",
apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY")
);
BinaryData speech = client.GenerateSpeech(
text: "The quick brown fox jumped over the lazy dog.",
voice: GeneratedSpeechVoice.Alloy
);
using FileStream stream = File.OpenWrite("speech.mp3");
speech.ToStream().CopyTo(stream);
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
});
const speech = await client.audio.speech.create({
input: 'input',
model: 'tts-1',
voice: 'alloy',
});
console.log(speech);
const content = await speech.blob();
console.log(content);
package main
import (
"context"
"fmt"
"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
)
func main() {
client := openai.NewClient(
option.WithAPIKey("My API Key"),
)
speech, err := client.Audio.Speech.New(context.TODO(), openai.AudioSpeechNewParams{
Input: "input",
Model: openai.SpeechModelTTS1,
Voice: openai.AudioSpeechNewParamsVoiceUnion{
OfAudioSpeechNewsVoiceString2: openai.String("alloy"),
},
})
if err != nil {
panic(err.Error())
}
fmt.Printf("%+v
", speech)
}
package com.openai.example;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.HttpResponse;
import com.openai.models.audio.speech.SpeechCreateParams;
import com.openai.models.audio.speech.SpeechModel;
public final class Main {
private Main() {}
public static void main(String[] args) {
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
SpeechCreateParams params = SpeechCreateParams.builder()
.input("input")
.model(SpeechModel.TTS_1)
.voice(SpeechCreateParams.Voice.UnionMember1.ALLOY)
.build();
HttpResponse speech = client.audio().speech().create(params);
}
}
require "openai"
openai = OpenAI::Client.new(api_key: "My API Key")
speech = openai.audio.speech.create(input: "input", model: :"tts-1", voice: :alloy)
puts(speech)
요청
curl https://api.openai.com/v1/audio/speech \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini-tts",
"input": "The quick brown fox jumped over the lazy dog.",
"voice": "alloy",
"stream_format": "sse"
}'
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
});
const speech = await client.audio.speech.create({
input: 'input',
model: 'tts-1',
voice: 'alloy',
});
console.log(speech);
const content = await speech.blob();
console.log(content);
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
)
speech = client.audio.speech.create(
input="input",
model="tts-1",
voice="alloy",
)
print(speech)
content = speech.read()
print(content)
package main
import (
"context"
"fmt"
"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
)
func main() {
client := openai.NewClient(
option.WithAPIKey("My API Key"),
)
speech, err := client.Audio.Speech.New(context.TODO(), openai.AudioSpeechNewParams{
Input: "input",
Model: openai.SpeechModelTTS1,
Voice: openai.AudioSpeechNewParamsVoiceUnion{
OfAudioSpeechNewsVoiceString2: openai.String("alloy"),
},
})
if err != nil {
panic(err.Error())
}
fmt.Printf("%+v
", speech)
}
package com.openai.example;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.HttpResponse;
import com.openai.models.audio.speech.SpeechCreateParams;
import com.openai.models.audio.speech.SpeechModel;
public final class Main {
private Main() {}
public static void main(String[] args) {
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
SpeechCreateParams params = SpeechCreateParams.builder()
.input("input")
.model(SpeechModel.TTS_1)
.voice(SpeechCreateParams.Voice.UnionMember1.ALLOY)
.build();
HttpResponse speech = client.audio().speech().create(params);
}
}
require "openai"
openai = OpenAI::Client.new(api_key: "My API Key")
speech = openai.audio.speech.create(input: "input", model: :"tts-1", voice: :alloy)
puts(speech)