JavaScript Text to Speech
Add text to speech to a JavaScript project by calling the Narakeet REST API. The example on this page uses the built-in Node.js fetch — no third-party HTTP library required. It works in Node 18+ and any runtime that supports the Fetch API (Deno, Bun, Cloudflare Workers).
See the Text to Speech API reference for authentication, polling, subtitle conversion, and other features common to all languages.
- Text to Speech in JavaScript
- JS Text to Speech
- Browser Requests and CORS
- JavaScript Text Reader
- TTS JavaScript
- JavaScript Text to Speech API Options
Text to Speech in JavaScript
The snippet below posts text to the API and pipes the response into a file. Because Node.js fetch is built in since version 18, this runs without installing any packages.
import { writeFile } from 'node:fs/promises';
const apiKey = process.env.NARAKEET_API_KEY;
const voice = 'mickey';
const text = 'Hi there from the API';
const response = await fetch(
`https://api.narakeet.com/text-to-speech/m4a?voice=${voice}`,
{
method: 'POST',
headers: {
'Accept': 'application/octet-stream',
'Content-Type': 'text/plain',
'x-api-key': apiKey,
},
body: text,
},
);
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
const buffer = Buffer.from(await response.arrayBuffer());
await writeFile('result.m4a', buffer);
Set NARAKEET_API_KEY in your environment before running. Create a key from the API Keys page. The full project with additional examples is at text-to-speech-api-nodejs-example on GitHub.
JS Text to Speech
Because JS text to speech through the API is just an HTTP POST, it fits into any JavaScript environment — server-side Node scripts, edge functions, or CI/CD automation. No native add-on or compiled dependency to worry about.
The streaming approach is helpful when generating large files. Instead of buffering the whole response, you can pipe it using node:stream for lower memory usage. For most short-content requests, the fetch example above is simpler and sufficient.
Scenarios where JS text to speech works well:
- A Next.js or Express route that returns audio for an accessibility widget
- A GitHub Action step that narrates release notes into an MP3
- An AWS Lambda triggered by S3 uploads that reads documents aloud
- A Slack bot that converts messages to audio clips on demand
When your input exceeds 1 KB or you need WAV files, use the Long Content (Polling) API. A Node.js polling example is at https://github.com/narakeet/text-to-speech-polling-api-nodejs-example.
Browser Requests and CORS
The Narakeet API does not set CORS headers, so fetch calls from browser-side JavaScript will fail. This is intentional. Your API key is a secret tied to your account balance — embedding it in front-end code would expose it to anyone who opens the browser developer tools.
Instead, keep the API call on the server and expose a thin endpoint to your front-end. For example, with Express:
import express from 'express';
const app = express();
app.use(express.text());
app.post('/api/tts', async (req, res) => {
const response = await fetch(
'https://api.narakeet.com/text-to-speech/mp3?voice=mickey',
{
method: 'POST',
headers: {
'Accept': 'application/octet-stream',
'Content-Type': 'text/plain',
'x-api-key': process.env.NARAKEET_API_KEY,
},
body: req.body,
},
);
if (!response.ok) {
return res.status(response.status).send('TTS error');
}
res.set('Content-Type', 'audio/mpeg');
const buffer = Buffer.from(await response.arrayBuffer());
res.send(buffer);
});
Your front-end then calls /api/tts on your own domain — no API key leaves the server. The same pattern works with Next.js API routes, Cloudflare Workers, AWS Lambda behind API Gateway, or any other server-side setup. Keep the key in an environment variable and let the server proxy the request.
JavaScript Text Reader
A JavaScript text reader built on the Narakeet API produces identical audio regardless of the user’s browser or OS. Browser-based alternatives like the Web Speech API depend on whatever voices the operating system provides, so the same page sounds different on Chrome, Safari, and Firefox. A server-side JavaScript text reader eliminates that inconsistency, and gives you access to 900 AI voices across 100 languages.
TTS JavaScript
Integrating TTS into JavaScript projects requires nothing beyond the ability to make HTTPS requests. No dedicated TTS JavaScript package, no WebAssembly binary, no model files. Your TTS JavaScript code sends plain text and receives audio bytes — Node.js, Deno, Bun, or a Cloudflare Worker all handle this identically.
JavaScript Text to Speech API Options
Adjust the output by modifying the request URL:
| Parameter | Values | What it does |
|---|---|---|
voice |
Any of 900 names | Selects the speaker (browse voices) |
voice-speed |
0.5 – 2.0 |
Reading pace multiplier (1.0 is normal) |
voice-volume |
soft / medium / loud |
Output loudness |
| endpoint path | /m4a, /mp3, /wav |
Audio format (WAV needs the polling API) |
For pitch adjustments, SSML-like pauses, and multi-voice scripts, embed a script header in the request body. See Configuring Audio Tasks for the full reference.