Text-to-Speech
TTS POST Endpoint
Generate text-to-speech audio from a complete text block with HTTP POST.
POST
/
post
/
speech
/
tts
cURL
curl -L -X POST https://api.gradium.ai/api/post/speech/tts \
-H "x-api-key: your_api_key" \
-H "Content-Type: application/json" \
-d '{"text": "Hello, world!", "voice_id": "YTpq7expH9539ERJ", "output_format": "wav", "only_audio": true}' \
> output.wavimport requests
resp = requests.post(
"https://api.gradium.ai/api/post/speech/tts",
json={
"text": "Hello, world!",
"voice_id": "YTpq7expH9539ERJ",
"output_format": "wav",
"only_audio": True,
},
headers={"x-api-key": "your_api_key"},
)
resp.raise_for_status()
with open("output.wav", "wb") as f:
f.write(resp.content)const options = {
method: 'POST',
headers: {'x-api-key': '<x-api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({text: '<string>', voice_id: '<string>', only_audio: true})
};
fetch('https://api.gradium.ai/api/post/speech/tts', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.gradium.ai/api/post/speech/tts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'text' => '<string>',
'voice_id' => '<string>',
'only_audio' => true
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-api-key: <x-api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.gradium.ai/api/post/speech/tts"
payload := strings.NewReader("{\n \"text\": \"<string>\",\n \"voice_id\": \"<string>\",\n \"only_audio\": true\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-api-key", "<x-api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.gradium.ai/api/post/speech/tts")
.header("x-api-key", "<x-api-key>")
.header("Content-Type", "application/json")
.body("{\n \"text\": \"<string>\",\n \"voice_id\": \"<string>\",\n \"only_audio\": true\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.gradium.ai/api/post/speech/tts")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<x-api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"text\": \"<string>\",\n \"voice_id\": \"<string>\",\n \"only_audio\": true\n}"
response = http.request(request)
puts response.read_bodyHeaders
Your Gradium API key
Body
application/json
The text to convert to speech
Voice ID from the library or custom voice ID
Audio output format
Available options:
wav, pcm, opus, ulaw_8000, mulaw_8000, alaw_8000, pcm_8000, pcm_16000, pcm_22050, pcm_24000, pcm_44100, pcm_48000 When true, returns raw audio bytes instead of JSON
Response
Audio data returned successfully
Previous
STT WebSocket StreamStream audio to Gradium speech-to-text over WebSocket for real-time transcription.
Next
⌘I
cURL
curl -L -X POST https://api.gradium.ai/api/post/speech/tts \
-H "x-api-key: your_api_key" \
-H "Content-Type: application/json" \
-d '{"text": "Hello, world!", "voice_id": "YTpq7expH9539ERJ", "output_format": "wav", "only_audio": true}' \
> output.wavimport requests
resp = requests.post(
"https://api.gradium.ai/api/post/speech/tts",
json={
"text": "Hello, world!",
"voice_id": "YTpq7expH9539ERJ",
"output_format": "wav",
"only_audio": True,
},
headers={"x-api-key": "your_api_key"},
)
resp.raise_for_status()
with open("output.wav", "wb") as f:
f.write(resp.content)const options = {
method: 'POST',
headers: {'x-api-key': '<x-api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({text: '<string>', voice_id: '<string>', only_audio: true})
};
fetch('https://api.gradium.ai/api/post/speech/tts', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.gradium.ai/api/post/speech/tts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'text' => '<string>',
'voice_id' => '<string>',
'only_audio' => true
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-api-key: <x-api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.gradium.ai/api/post/speech/tts"
payload := strings.NewReader("{\n \"text\": \"<string>\",\n \"voice_id\": \"<string>\",\n \"only_audio\": true\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-api-key", "<x-api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.gradium.ai/api/post/speech/tts")
.header("x-api-key", "<x-api-key>")
.header("Content-Type", "application/json")
.body("{\n \"text\": \"<string>\",\n \"voice_id\": \"<string>\",\n \"only_audio\": true\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.gradium.ai/api/post/speech/tts")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<x-api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"text\": \"<string>\",\n \"voice_id\": \"<string>\",\n \"only_audio\": true\n}"
response = http.request(request)
puts response.read_body