Prompt
How do I make my website read text aloud using an API?
Latest observation
Oct 11, 2026
OpenAI APIWeb search: off
To make a website read text aloud using an API, you typically use a Text-to-Speech (TTS) service. The basic flow is:
- User enters text
- Your frontend sends the text to your backend or directly to a TTS API
- The API returns an audio file/stream
- You play that audio in the browser
Common ways to do it
Option 1: Use a browser Web Speech API
If you just want simple speech in the browser and don’t need an external API:
<button onclick="speakText()">Speak</button>
<script>
function speakText() {
const text = "Hello, welcome to my website!";
const utterance = new SpeechSynthesisUtterance(text);
speechSynthesis.speak(utterance);
}
</script>
Pros: No server needed, easy
Cons: Voice quality varies by browser/device, limited control
Option 2: Use a third-party TTS API
Popular options:
- Google Cloud Text-to-Speech
- Amazon Polly
- Azure Speech
- ElevenLabs
- OpenAI TTS (if available in your stack)
Typical pattern:
Frontend
Send text to your backend:
async function speakText(text) {
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 audio = new Audio(audioUrl);
audio.play();
}
Backend
Your server calls the TTS provider and returns audio:
app.post('/api/tts', async (req, res) => {
const { text } = req.body;
// Call your TTS provider here
const audioBuffer = await generateSpeechAudio(text);
res.set({
'Content-Type': 'audio/mpeg'
});
res.send(audioBuffer);
});
Important notes
- Do not put API keys in frontend code if using a paid TTS service.
- Use a backend endpoint to protect your secret key.
- You may want to:
- validate input length
- rate limit requests
- cache repeated audio
- stream audio for faster playback
Simplest production approach
If you want the easiest real-world setup:
- Frontend sends text to your backend
- Backend calls TTS API
- Backend returns MP3/WAV
- Browser plays returned audio
If you want, I can show you:
- a complete example using JavaScript + Node.js, or
- an example using a specific provider like Google, Azure, AWS, or ElevenLabs.