Async Streaming Text to Speech - Uplift AI API Docs
Streaming async TTS
Python
import requests
import json
# Step 1: Initiate streaming async TTS
url = "https://api.upliftai.org/v1/synthesis/text-to-speech/stream-async"
payload = json.dumps({
"voiceId": "v_meklc281",
"text": "سلام، یہ پاکستان کی تاریخ کے بارے میں ہے۔",
"outputFormat": "MP3_22050_128"
})
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
}
response = requests.post(url, headers=headers, data=payload)
result = response.json()
# Step 2: Stream audio with ~300ms first chunk
media_id = result['mediaId']
token = result['token']
audio_url = f"https://api.upliftai.org/v1/synthesis/stream-audio/{media_id}?token={token}"
# This URL supports chunked streaming
# First chunk arrives in ~300ms
JavaScript
// Initiate streaming TTS
async function streamAudio(text) {
const response = await fetch('https://api.upliftai.org/v1/synthesis/text-to-speech/stream-async', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
voiceId: "v_meklc281",
text: text,
outputFormat: "MP3_22050_128"
})
});
const { mediaId, token } = await response.json();
const streamUrl = `https://api.upliftai.org/v1/synthesis/stream-audio/${mediaId}?token=${token}`;
// Create audio element that starts streaming
const audio = new Audio(streamUrl);
audio.play(); // Starts playing as chunks arrive
return audio;
}
cURL
curl --request POST \
--url https://api.upliftai.org/v1/synthesis/text-to-speech/stream-async \
--header 'Authorization: <api-key>' \
--header 'Content-Type: application/json' \
--data '
{
"text": "سلام، آپ اِس وقت اوریٹر کی آواز سن رہے ہیں۔",
"voiceId": "v_meklc281",
"phraseReplacementConfigId": "<string>"
}
'
PHP
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.upliftai.org/v1/synthesis/text-to-speech/stream-async",
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' => 'سلام، آپ اِس وقت اوریٹر کی آواز سن رہے ہیں۔',
'voiceId' => 'v_meklc281',
'phraseReplacementConfigId' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Authorization: <api-key>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
Go
package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.upliftai.org/v1/synthesis/text-to-speech/stream-async"
payload := strings.NewReader("{\n \"text\": \"سلام، آپ اِس وقت اوریٹر کی آواز سن رہے ہیں۔\",\n \"voiceId\": \"v_meklc281\",\n \"phraseReplacementConfigId\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "<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))
}
Java
HttpResponse<String> response = Unirest.post("https://api.upliftai.org/v1/synthesis/text-to-speech/stream-async")
.header("Authorization", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"text\": \"سلام، آپ اِس وقت اوریٹر کی آواز سن رہے ہیں۔\",\n \"voiceId\": \"v_meklc281\",\n \"phraseReplacementConfigId\": \"<string>\"\n}")
.asString();
Ruby
require 'uri'
require 'net/http'
url = URI("https://api.upliftai.org/v1/synthesis/text-to-speech/stream-async")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"text\": \"سلام، آپ اِس وقت اوریٹر کی آواز سن رہے ہیں۔\",\n \"voiceId\": \"v_meklc281\",\n \"phraseReplacementConfigId\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body
Responses
200
{
"mediaId": "media_abc123xyz",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
400
{
"message": "Invalid request parameters"
}
429
{
"message": "Rate limit exceeded, please try again later"
}
Authorizations
- Authorization: string, required, API key with format "Bearer sk_api_..."
Body
- text: string, required, The text to synthesize, Maximum string length:
2500.
- outputFormat: enum, required, Format of the audio output. Available options:
MP3_22050_32, MP3_22050_64, MP3_22050_128, etc.
- voiceId: string, required, Identifier for the voice to use. Example:
"v_meklc281"
- phraseReplacementConfigId: string, Optional ID of a phrase replacement configuration to apply.