Stream Text to Speech - Uplift AI API Docs
client.tts.createStream(request)
Returns a Readable stream. The first chunk arrives quickly — use this for real-time playback where you want to start playing audio before the full response is ready.
import UpliftAI from '@upliftai/sdk-js';
import { createWriteStream } from 'fs';
const client = new UpliftAI({
apiKey: 'your-api-key',
});
const { stream, metadata } = await client.tts.createStream({
text: 'اردو میں ایک لمبا جملہ، جو سٹریمنگ کی جانچ کے لیے ہے',
voiceId: 'v_meklc281',
outputFormat: 'MP3_22050_64',
});
console.log(`Request ID: ${metadata.requestId}`);
console.log(`Content-Type: ${metadata.contentType}`);
const fileStream = createWriteStream('output.mp3');
let totalBytes = 0;
for await (const chunk of stream) {
totalBytes += chunk.length;
fileStream.write(chunk);
process.stdout.write(`\rReceived ${totalBytes} bytes`);
}
fileStream.end();
console.log('\nDone!');
Request Parameters
| Parameter |
Type |
Required |
Description |
text |
string |
Yes |
Text to synthesize |
voiceId |
string |
Yes |
Voice profile ID |
outputFormat |
string |
No |
Audio format — defaults to WAV_22050_32 |
phraseReplacementConfigId |
string |
No |
ID from phrase replacements |
Response
| Field |
Type |
Description |
stream |
Readable |
Node.js readable stream of audio chunks |
metadata.requestId |
string |
Request ID for debugging |
metadata.contentType |
string |
MIME type of the audio |
metadata.sampleRate |
number |
Audio sample rate |
When to Use Streaming vs Sync
| Method |
Best For |
tts.create() |
Batch processing, saving files, when you need the full buffer |
tts.createStream() |
Real-time playback, large texts, when first-byte latency matters |