Build and Host a Text-to-Speech SaaS with Node.js and Bluehost
Introduction If you’ve ever wanted to turn a piece of text into a natural‑sounding voice without managing a massive infrastructure, you’re in the right place. In this tutorial we’ll build a tiny Text‑to‑Speech (TTS) Sa
Introduction
If you’ve ever wanted to turn a piece of text into a natural‑sounding voice without managing a massive infrastructure, you’re in the right place. In this tutorial we’ll build a tiny Text‑to‑Speech (TTS) SaaS using Node.js and the powerful voice‑cloning engine from ElevenLabs. Once the API is working locally, we’ll ship it to a live environment with Bluehost, an easy‑to‑use and affordable hosting provider that supports Node.js apps out of the box.
By the end of this article you’ll have:
- A REST endpoint (
/speak) that accepts plain text and returns an audio file. - A simple front‑end page that lets you type a sentence and hear it back instantly.
- A production‑ready deployment on Bluehost so anyone can start using your voice AI service.
Let’s dive in!
Prerequisites
| What you need | Why it matters |
|---|---|
| Node.js (v18+) | Modern JavaScript runtime for our server. |
| npm or yarn | Package manager to install dependencies. |
| ElevenLabs API key | Provides access to the high‑quality TTS model. Get yours here: https://try.elevenlabs.io/kr07zfuqn1bp |
| Bluehost account | Host the app on a reliable platform with one‑click Node.js support. Sign up at https://bluehost.sjv.io/5k0d52. |
| cURL or Postman | Test the endpoint locally before deploying. |
If you already have a Bluehost account, you can skip the sign‑up step and head straight to the “Deploy” section later.
Setting up the ElevenLabs API
ElevenLabs offers a REST API that accepts raw text and returns an MP3 stream. The API is straightforward:
POST https://api.elevenlabs.io/v1/text-to-speech/{voice_id}
Headers:
xi-api-key: YOUR_API_KEY
Content-Type: application/json
Body:
{
"text": "Your text here",
"model_id": "eleven_monolingual_v1"
}
Tip: The free tier gives you a decent number of characters per month, which is perfect for a prototype. For production workloads, consider upgrading to a paid plan.
Create a file called .env in your project root and store the secret safely:
ELEVENLABS_API_KEY=your_elevenlabs_api_key_here
ELEVENLABS_VOICE_ID=EXAVITQu4vr4xn6j3iV2 # default "Rachel" voice, change if you like
PORT=3000
Building the Node.js TTS Service
- Initialize the project
mkdir tts-saas && cd tts-saas
npm init -y
npm install express dotenv node-fetch
-
Create
server.js
// server.js
require('dotenv').config();
const express = require('express');
const fetch = require('node-fetch');
const path = require('path');
const app = express();
app.use(express.json());
app.use(express.static('public'));
const ELEVEN_API = 'https://api.elevenlabs.io/v1/text-to-speech';
const { ELEVENLABS_API_KEY, ELEVENLABS_VOICE_ID, PORT = 3000 } = process.env;
// Simple health check
app.get('/health', (req, res) => res.send('OK'));
// Main TTS endpoint
app.post('/speak', async (req, res) => {
const { text } = req.body;
if (!text) return res.status(400).json({ error: 'Text is required' });
try {
const response = await fetch(`${ELEVEN_API}/${ELEVENLABS_VOICE_ID}`, {
method: 'POST',
headers: {
'xi-api-key': ELEVENLABS_API_KEY,
'Content-Type': 'application/json',
Accept: 'audio/mpeg',
},
body: JSON.stringify({
text,
model_id: 'eleven_monolingual_v1',
}),
});
if (!response.ok) {
const err = await response.text();
throw new Error(`ElevenLabs error: ${err}`);
}
// Pipe the MP3 straight back to the client
res.set('Content-Type', 'audio/mpeg');
response.body.pipe(res);
} catch (err) {
console.error(err);
res.status(500).json({ error: err.message });
}
});
// Serve a tiny UI
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(PORT, () => console.log(`🚀 Server listening on http://localhost:${PORT}`));
-
Add a minimal front‑end (
public/index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Text‑to‑Speech SaaS</title>
<style>
body { font-family: Arial, sans-serif; margin: 2rem; }
textarea { width: 100%; height: 100px; }
button { padding: .5rem 1rem; margin-top: .5rem; }
audio { margin-top: 1rem; display: block; }
</style>
</head>
<body>
<h1>🔊 Your Voice AI Playground</h1>
<textarea id="txt" placeholder="Enter text here…"></textarea><br/>
<button id="speakBtn">Speak</button>
<audio id="player" controls></audio>
<script>
const btn = document.getElementById('speakBtn');
const txt = document.getElementById('txt');
const player = document.getElementById('player');
btn.onclick = async () => {
const response = await fetch('/speak', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: txt.value })
});
if (!response.ok) {
alert('Error: ' + (await response.json()).error);
return;
}
const blob = await response.blob();
player.src = URL.createObjectURL(blob);
};
</script>
</body>
</html>
- Run locally
node server.js
Open http://localhost:3000 in your browser, type a sentence, and hit Speak. You should hear a crystal‑clear voice generated by ElevenLabs.
Testing the API with cURL
If you prefer a quick terminal test, use the following command (replace YOUR_TEXT with something fun):
curl -X POST http://localhost:3000/speak \
-H "Content-Type: application/json" \
-d '{"text":"YOUR_TEXT"}' \
--output speech.mp3
Open speech.mp3 with any media player to verify the output.
Deploying to Bluehost
1. Create a Node.js app in cPanel
- Log in to your Bluehost dashboard (
https://bluehost.sjv.io/5k0d52). - Navigate to Advanced → cPanel and look for Node.js App under the Software section.
- Click Create Application. Choose:
| Setting | Value |
|---|---|
| Application Root |
tts-saas (or any folder you like) |
| Application URL |
yourdomain.com or a sub‑domain |
| Application Mode | Production |
| Node.js Version |
18.x (or latest) |
Bluehost will spin up a lightweight container and give you a SSH command to push code.
2. Push the code
You can either git clone a repository or use the File Manager to upload a zipped copy. For simplicity, let’s use the command line:
# SSH into your Bluehost account (instructions in the cPanel UI)
ssh [email protected]
# Move to the app directory
cd ~/apps/tts-saas
# Initialize a git repo (if you haven’t already)
git init
git remote add origin https://github.com/yourname/tts-saas.git
git pull origin main
If you’re uploading manually, just copy the server.js, package.json, public/ folder, and .env (make sure to never commit the .env file to a public repo).
3. Install dependencies
npm install
4. Set environment variables
In the Node.js App interface, click Environment Variables and add the three keys from your local .env file (ELEVENLABS_API_KEY, ELEVENLABS_VOICE_ID, PORT). The UI will automatically inject them into the runtime.
5. Start / Restart the app
Back in the Node.js App page, hit Restart Application. Bluehost will rebuild the container and expose your endpoint at the URL you configured.
6. Verify the deployment
Open https://yourdomain.com (or the sub‑domain you chose). The same UI you tested locally should appear, now powered by a live server. Try a few sentences—if you hear the voice, you’re all set!
Monitoring & Scaling
- Logs: Bluehost’s Node.js interface provides a real‑time log tail. Use it to spot errors like missing API keys.
- Rate limits: ElevenLabs imposes per‑minute limits on the free tier. If you expect higher traffic, upgrade the ElevenLabs plan and consider adding a simple queue (e.g., BullMQ) to smooth bursts.
-
HTTPS: Bluehost automatically provisions a free SSL certificate via Let’s Encrypt, so all traffic to your
/speakendpoint is encrypted out of the box.
Wrap‑up
You now have a functional Text‑to‑Speech SaaS built with Node.js, powered by the high‑quality voice engine from ElevenLabs, and hosted on Bluehost—the affordable, one‑click solution for Node projects. The stack is deliberately lightweight so you can iterate quickly, add features like user authentication or usage tracking, and even experiment with voice cloning (ElevenLabs supports custom voice uploads for a fee).
Next steps you might explore
- User accounts with JWT authentication.
- Database (MySQL on Bluehost) to store request logs and billing.
- Webhooks from ElevenLabs for real‑time progress updates on long‑form audio.
Call to Action
Ready to give your app a human voice? Grab an API key from ElevenLabs here: https://try.elevenlabs.io/kr07zfuqn1bp and spin up your first production‑grade instance on Bluehost with a single click: https://bluehost.sjv.io/5k0d52.
Happy coding, and may your SaaS speak as clearly as you think!
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.