Prompt
How do I generate speech from text in a backend job?
Latest observation
Oct 11, 2026
OpenAI APIWeb search: off
To generate speech from text in a backend job, you typically use a text-to-speech (TTS) service or library inside your worker process, then save the resulting audio file and return/store its URL or path.
Common approach
-
Queue a background job
- Your app stores the text and metadata in a DB.
- A job queue/worker picks it up asynchronously.
-
Call a TTS engine
- Cloud APIs: Google Cloud TTS, Amazon Polly, Azure TTS, OpenAI TTS, etc.
- Local/open-source: e.g. Coqui TTS, Piper, Festival, eSpeak.
-
Write the audio to storage
- Save as
.mp3,.wav, etc. - Upload to S3/GCS/local disk/object storage.
- Save as
-
Mark the job complete
- Save audio URL/path and status in your DB.
- Notify frontend if needed.
Example flow
Pseudocode
Job starts
-> load text from DB
-> sanitize/validate text
-> call TTS API
-> receive audio bytes
-> save audio file
-> update job record with file URL
-> done
Example in Node.js using a cloud TTS API
import fs from "fs";
import path from "path";
async function generateSpeech(text, outputPath) {
const response = await fetch("https://api.example-tts.com/speak", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.TTS_API_KEY}`
},
body: JSON.stringify({
text,
voice: "alloy",
format: "mp3"
})
});
if (!response.ok) {
throw new Error(`TTS failed: ${response.statusText}`);
}
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
fs.writeFileSync(outputPath, buffer);
return outputPath;
}
Example in Python using a TTS SDK
import requests
def generate_speech(text, output_file):
r = requests.post(
"https://api.example-tts.com/speak",
headers={"Authorization": f"Bearer {API_KEY}"},
json={"text": text, "voice": "alloy", "format": "mp3"},
timeout=60,
)
r.raise_for_status()
with open(output_file, "wb") as f:
f.write(r.content)
return output_file
Backend job considerations
- Async processing: TTS can be slow, so don’t do it in the request thread.
- Chunk long text: Some APIs have text-length limits.
- Retries: Network/API failures happen.
- Rate limiting: Respect provider limits.
- Caching: If the same text is requested often, reuse existing audio.
- Security: Validate text; avoid exposing API keys.
- Audio format: MP3 is common for web delivery, WAV for higher quality/editing.
If you want local generation
You can run a model on your server and avoid external APIs, but it requires:
- More CPU/GPU resources
- Model setup/maintenance
- Higher operational complexity
If you want, I can show you a complete backend worker example in Node.js, Python, Go, or Rails using a specific TTS provider.