Skip to navigation

REST API

View as Markdown

POST /text-to-speech converts text into speech and returns the complete audio, base64-encoded inside a JSON response. Use it for text that is known up front and when you need the whole file before you play or store it. To start playback before synthesis finishes, use the HTTP Streaming API or the WebSocket API. See Which API to use to compare all three.

EndpointPOST https://api.sarvam.ai/text-to-speech
Auth headerapi-subscription-key: <your key>
Max text2,500 characters per request
Default outputWAV, 22,050 Hz
ResponseJSON with request_id and audios (base64-encoded audio)

Models

ModelVoicesDefault speaker
bulbul:v4-flash200+ persona voicesshubh_enhi_ads
bulbul:v3 (API default when model is omitted)37 speakersshubh

Both models share the same request format, limits and parameters. A speaker must belong to the model you request. See Voices for the full list and Bulbul for model details.

Quickstart

Set your API key as the SARVAM_API_KEY environment variable, then run one of these. Each saves the speech as a WAV file.

import os
from sarvamai import SarvamAI
from sarvamai.play import save
client = SarvamAI(api_subscription_key=os.environ["SARVAM_API_KEY"])
response = client.text_to_speech.convert(
text="नमस्ते! Sarvam AI में आपका स्वागत है।",
language_code="hi-IN",
model="bulbul:v4-flash",
speaker="simran_en_customer",
)
save(response, "output.wav")

Always pass speaker explicitly. The Python SDK’s default speaker is not valid for current models, and the API default depends on model.

Request parameters

ParameterTypeRequiredDefaultDescription
textstringYesnoneText to convert. 1 to 2,500 characters. Code-mixed text is supported.
language_codestringYesnoneBCP-47 code: hi-IN, bn-IN, ta-IN, te-IN, gu-IN, kn-IN, ml-IN, mr-IN, pa-IN, od-IN, en-IN
modelstringNobulbul:v3bulbul:v4-flash or bulbul:v3
speakerstringNoDepends on modelVoice to use. Must be valid for the chosen model.
pacenumberNo1.0Speech speed, 0.5–2.0
pitchnumberNo0.0Pitch shift, -0.5–0.5
loudnessnumberNo1.0Volume multiplier, 0.1–2.5
temperaturenumberNo0.6Expressiveness, 0.01–1.0
speech_sample_rateintegerNo22050Output sample rate in Hz: 8000, 16000, 22050, 24000, 32000, 44100, 48000
output_audio_codecstringNowavwav, mp3, aac, flac, linear16, mulaw, alaw, opus
enable_preprocessingbooleanNofalseNormalize English words and numbers before synthesis
dict_idstringNononeID of a pronunciation dictionary (format p_xxxxxxxx)

opus fails with a 400 at the default 22,050 Hz. Set speech_sample_rate to 8000, 16000, 24000 or 48000 when you use it. Unknown request fields are ignored without an error.

For how pace, pitch and loudness sound in practice, see the pace, pitch and loudness guides.

Response

{
"request_id": "20241115_12345678-1234-5678-1234-567812345678",
"audios": ["UklGRiQAAABXQVZFZm10IBAAAAABAAEAQB8AAAB9AAACABAAZGF0YQAAAAA..."]
}
FieldTypeDescription
request_idstringUnique ID for the request. Include it when you contact support.
audiosarray of stringsBase64-encoded audio in the codec you requested. Decode audios[0] to get the file.

The audio is base64 text, not binary. Decode it before you save or play it, and use a file extension that matches output_audio_codec (for example .mp3 for mp3).

import base64
with open("output.wav", "wb") as f:
f.write(base64.b64decode(response.audios[0]))

In a browser, build a playable URL from the base64 string:

const audio = new Audio("data:audio/wav;base64," + response.audios[0]);
audio.play();

Customize the output

This example speaks Tamil text slightly faster, as 24 kHz MP3.

import base64
import os
from sarvamai import SarvamAI
client = SarvamAI(api_subscription_key=os.environ["SARVAM_API_KEY"])
response = client.text_to_speech.convert(
text="வணக்கம்! சர்வம் ஏஐக்கு வரவேற்கிறோம்.",
language_code="ta-IN",
model="bulbul:v4-flash",
speaker="simran_en_customer",
pace=1.1,
speech_sample_rate=24000,
output_audio_codec="mp3",
)
with open("output.mp3", "wb") as f:
f.write(base64.b64decode(response.audios[0]))

Errors

Errors return a JSON error object with message, code and request_id. The full code table, retry guidance and SDK exception reference are on Errors & Troubleshooting.

StatusCauseFix
400text is longer than 2,500 charactersSplit the text and send several requests
400speaker does not belong to modelThe error message lists the valid speakers for that model
400Invalid language_codeThe error message lists the valid codes
400Unsupported speech_sample_rate, or opus at 22,050 HzUse a rate from the parameter table
429Rate limit exceededWait and retry with backoff. See Credits & Rate Limits.
import os
from sarvamai import SarvamAI
from sarvamai.core.api_error import ApiError
client = SarvamAI(api_subscription_key=os.environ["SARVAM_API_KEY"])
try:
response = client.text_to_speech.convert(
text="Welcome to Sarvam AI!",
language_code="en-IN",
model="bulbul:v4-flash",
speaker="simran_en_customer",
)
except ApiError as e:
print(f"Error {e.status_code}: {e.body}")

See the API Reference for the full endpoint spec.

Need help? Contact us on Discord.