"stream": true to receive audio in chunks (Transfer-Encoding: chunked)
as soon as the first bytes are ready. This minimizes time-to-first-byte and is
ideal for long texts or live playback.
curl -N -X POST https://api.trysasha.ru/v1/speech \
-H "Authorization: Bearer $SASHA_API_KEY" \
-H "Content-Type: application/json" \
-d '{"text":"Длинный текст для потоковой озвучки...","stream":true}' \
--output stream.mp3
Node.js
import { writeFile } from "node:fs/promises";
const res = await fetch("https://api.trysasha.ru/v1/speech", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.SASHA_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ text: "Привет из стрима!", stream: true }),
});
if (!res.ok) throw new Error(`Error ${res.status}: ${await res.text()}`);
const chunks = [];
for await (const chunk of res.body) {
// Process each chunk here (play / forward). We just collect them.
chunks.push(chunk);
}
await writeFile("stream.mp3", Buffer.concat(chunks));
Python
import os, requests
with requests.post(
"https://api.trysasha.ru/v1/speech",
headers={"Authorization": f"Bearer {os.environ['SASHA_API_KEY']}"},
json={"text": "Привет из стрима!", "stream": True},
stream=True,
) as res:
res.raise_for_status()
with open("stream.mp3", "wb") as f:
for chunk in res.iter_content(chunk_size=4096):
f.write(chunk)
On error, a streaming response may arrive with no audio. Always check the HTTP
status /
res.ok before saving the body to a file.