Get started with the Text-to-Speech API
On this page
Updated November 3, 2026, based on the xAI Text to Speech and Pricing pages. Sources: Text to Speech · Pricing.
Text in, audio out
The Grok Text-to-Speech API does one thing and does it well: it turns text into high-quality audio. Where the Voice Agent manages a dialogue, with turn-taking and interruptions, TTS works strictly one way: you send text and get sound back. That simplicity makes it the tool of choice whenever nobody talks back to the machine: narrated articles, phone greetings, voice-overs for e-learning modules, audio notifications.
HTTP or WebSocket: two ways to call the service
HTTP mode is the most direct. You send a POST request with your text, and the response contains the complete audio file, in MP3 by default:
curl -X POST "https://api.x.ai/v1/tts" \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-o output.mp3 \
-d '{
"text": "Hello and welcome to Corsen Academy.",
"voice_id": "ara",
"language": "en"
}'
Note the -o output.mp3: the response body is raw audio, not JSON wrapping a link. This mode is a perfect fit for background generation, such as a nightly batch that produces audio versions of your articles, since nobody is waiting in front of a screen.
WebSocket mode meets a different need: hearing the sound before the whole text has been synthesized. You open a connection and pass the parameters in the URL:
wss://api.x.ai/v1/tts?language=en&voice=ara&codec=mp3
You then send the text in pieces with text.delta, signal the end with text.done, and the audio comes back as base64 chunks on audio.delta. Streaming lifts the length limit of HTTP mode and, above all, lets you synthesize as you go, which is essential when the text itself is still being generated by a model.
HTTP request parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
text | string | Yes | The text to convert, up to 60,000 characters |
voice_id | string | No | The voice to use (default: eve) |
language | string | Yes | BCP-47 code (en, fr, de, …) or auto |
output_format | object | No | Codec, sample rate, and bit rate |
speed | number | No | Playback speed, from 0.7 to 1.5 (default: 1.0) |
Two points deserve attention. language is required, which often surprises people: the service doesn’t guess the language of your text unless you explicitly ask it to with the value auto. And voice_id uses exactly the same voices as the Voice Agent (28 built-in voices as of November 3, 2026, including eve, ara, rex, sal, and leo, plus your custom voices), and it ignores case. The same voice persona can therefore power both your conversational agent and your recorded content, which is worth taking advantage of for brand consistency.
Choose the output format
The output_format field controls the codec and quality:
{
"text": "Text to convert.",
"voice_id": "rex",
"language": "en",
"output_format": {
"codec": "wav",
"sample_rate": 44100
}
}
Pick the codec based on what happens to the file next. mp3 suits general use: well compressed and playable everywhere, it’s the sensible default for a website or a podcast. wav is lossless, so heavier, but it’s what you need when an editor is going to rework the track. pcm delivers raw audio, for processing pipelines that have no use for a container. Finally, mulaw and alaw are the G.711 telephony codecs, for telecom systems that require them.
Sample rates range from 8000 to 48000 Hz, with a default of 24000 Hz, which covers the vast majority of uses. For MP3, the bit rate goes from 32000 to 192000 bps, with a default of 128000. Going up to 192000 for spoken voice adds next to nothing to the listening experience and makes your files a third larger: save that headroom for content that mixes music and voice.
Query the voice catalog
The API exposes two read-only endpoints that complement generation. GET /v1/tts/voices returns all available voices with their metadata, and GET /v1/tts/voices/{voice_id} returns the same fields (voice_id, name, language) for a single voice; the tone and use cases of each voice are described on the xAI Text to Speech page. Instead of hard-coding voice names in your interface, populate your voice picker from these endpoints, and your app will stay correct the day the catalog changes.
Key takeaways
- TTS converts text to audio over HTTP (simple) or WebSocket (streaming)
- It costs $15 per million characters, with a maximum of 60,000 characters per HTTP request
- 28 built-in voices, five codecs, and six sample rates give you plenty of flexibility
- WebSocket mode has no limit on total text length (each delta is capped at 60,000 characters)
- A BCP-47 code is required to specify the language (or
autofor automatic detection)