Prompt
How do I make my website read text aloud with a TTS API?
Latest observation
Oct 11, 2026
OpenAI APIWeb search: off
To make a website read text aloud with a TTS (text-to-speech) API, the basic flow is:
- User enters or selects text
- Your frontend sends that text to your backend
- Your backend calls the TTS API
- The API returns audio
- You play the audio in the browser
Simple architecture
- Frontend: gets text and sends it to your server
- Backend: stores your API key securely and requests speech from the TTS provider
- Browser: receives the audio file/stream and plays it
Why use a backend?
Don’t put TTS API keys directly in the browser if the API key is secret. Anyone can inspect frontend code and steal it. Use a backend as a proxy.
Example with a generic API
1) Frontend: collect text and request audio
<textarea id="text">Hello, world!</textarea>
<button onclick="speakText()">Speak</button>
<audio id="player" controls></audio>
<script>
async function speakText() {
const text = document.getElementById('text').value;
const res = await fetch('/api/tts', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text })
});
const audioBlob = await res.blob();
const audioUrl = URL.createObjectURL(audioBlob);
const player = document.getElementById('player');
player.src = audioUrl;
player.play();
}
</script>
2) Backend: call the TTS API and return audio
Example in Node.js/Express:
import express from 'express';
import fetch from 'node-fetch';
const app = express();
app.use(express.json());
app.post('/api/tts', async (req, res) => {
const { text } = req.body;
const apiResponse = await fetch('https://api.example.com/tts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.TTS_API_KEY}`
},
body: JSON.stringify({
text,
voice: 'en-US'
})
});
if (!apiResponse.ok) {
return res.status(500).send('TTS failed');
}
res.setHeader('Content-Type', 'audio/mpeg');
apiResponse.body.pipe(res);
});
app.listen(3000);
If you want a quicker option: use the browser’s built-in speech API
For simple use cases, you may not need a TTS service at all. Browsers support the Web Speech API:
<textarea id="text">Hello!</textarea>
<button onclick="speak()">Speak</button>
<script>
function speak() {
const text = document.getElementById('text').value;
const utterance = new SpeechSynthesisUtterance(text);
speechSynthesis.speak(utterance);
}
</script>
Pros
- No backend needed
- Fast and free
- Easy to implement
Cons
- Voice quality varies by device/browser
- Less control than a real TTS API
Things to consider when choosing a TTS API
- Voice quality
- Languages/accents supported
- Streaming vs file-based output
- Latency
- Pricing
- Audio format like MP3 or WAV
- SSML support for pauses, emphasis, pronunciation
Recommended implementation tips
- Validate text length before sending to the API
- Cache generated audio if the same text is requested often
- Use streaming if the API supports it for faster playback
- Add rate limiting to avoid abuse
- Handle errors gracefully
- Sanitize user input if you allow HTML or markup
If you want, I can show you:
- a full working example in React, Next.js, or plain HTML/JS
- how to do it with a specific provider like OpenAI, Google Cloud TTS, AWS Polly, ElevenLabs, or Azure