Skip to main content

POST /api/speech

Generate high-quality audio from text using OpenAI’s text-to-speech models. This endpoint returns audio as a base64-encoded data URL ready for playback.

Request Body

string
required
The text to convert to speech. Maximum length varies by model but typically supports several paragraphs.
string
default:"alloy"
The voice to use for speech generation. OpenAI provides six natural-sounding voices:
  • alloy (default) - Neutral and balanced
  • echo - Clear and expressive
  • fable - Warm and engaging
  • onyx - Deep and authoritative
  • nova - Energetic and bright
  • shimmer - Soft and calm

Response

string
Base64-encoded audio data URL in the format data:audio/[format];base64,[data]. Ready to use with HTML5 <audio> elements or Web Audio API.

Example Request

Example Response

Voice Characteristics

Choose the voice that best fits your use case:

Audio Format

The API returns audio in MP3 format by default, encoded as a base64 data URL. This format:
  • Works directly in browser <audio> elements
  • Compatible with Web Audio API
  • Small file size for quick transmission
  • High quality at 24kHz sample rate

Use Cases

Voice Responses

Generate voice responses from AI in the Voice Agent

Notifications

Speak important notifications or alerts

Accessibility

Read text content aloud for visually impaired users

Multilingual Support

Generate speech in multiple languages with natural pronunciation

Error Handling

400 Bad Request

500 Server Error

Performance Considerations

  • Average response time: 1-3 seconds depending on text length
  • Text is processed in chunks for longer inputs
  • Audio is streamed and encoded efficiently
  • Use shorter text segments for faster response times

Integration Example

Complete example with error handling and playback controls:
Source: nextjs-backend/src/app/api/speech/route.ts
  • Transcribe - Convert speech to text (STT)
  • Voice Agent - Real-time voice conversations
  • Chat - Generate text responses that can be spoken