Use case

JavaScript & Node.js text-to-speech API

Call POST https://api.sayitapi.com/v1/tts with the fetch built into Node.js 18+, read audio_url from the JSON response, and save the MP3. No npm package or SDK is required.

Quick start with fetch

Export your API key as SAYIT_API_KEY and send it in the SAYIT-API-KEY header. Keys issued by SayIt start with sk_live_. There is no separate sandbox key. The examples are ES modules (.mjs files) so they can use top-level await.

bash
# Node.js 18 or newer has fetch built in; no packages needed
export SAYIT_API_KEY="YOUR_API_KEY"

POST to https://api.sayitapi.com/v1/tts with text, voice_id, and language. The character balance is reduced by text.length.

javascript
// save as tts.mjs and run: node tts.mjs
import { writeFile } from "node:fs/promises"

const res = await fetch("https://api.sayitapi.com/v1/tts", {
  method: "POST",
  headers: {
    "SAYIT-API-KEY": process.env.SAYIT_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    text: "Hello from Node.js.",
    voice_id: "eve",
    language: "en",
    output_format: "mp3",
    speed: 1,
  }),
})
if (!res.ok) throw new Error("TTS failed: " + res.status + " " + (await res.text()))
const data = await res.json()

const audio = await fetch(data.audio_url)
if (!audio.ok) throw new Error("Download failed: " + audio.status)
await writeFile("hello.mp3", Buffer.from(await audio.arrayBuffer()))

console.log(data.characters_used, data.duration_seconds)

A completed response includes id, audio_url, characters_used, duration_seconds, voice_id, and created_at. When text is translated, translated and source_language are added. The signed audio_url expires after 24 hours, so download the MP3 as shown above if you need to keep it.

The body also accepts output_format (mp3 or wav), speed (0.5 to 2.0; the engine applies 0.7 to 1.5 and clamps values outside that range), sample_rate (8000, 16000, 22050, or 44100; default 22050), and speech_tags (boolean, with tags written inline in the text).

Choosing a voice and language

Set voice_id to eve, ara, rex, sal, or leo; the default is eve. See the voices page for samples.

language defaults to auto, which speaks the text as written, and also accepts one of 57 language codes. SayIt translates into 57 languages with native voice support for 20+, and the request body is unchanged. See multilingual apps for more.

javascript
const res = await fetch("https://api.sayitapi.com/v1/tts", {
  method: "POST",
  headers: {
    "SAYIT-API-KEY": process.env.SAYIT_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ text: "Your order has shipped.", voice_id: "rex", language: "es" }),
})
const data = await res.json()
console.log(data.translated, data.source_language)

Keep your API key on the server

Do not put SAYIT-API-KEY in browser JavaScript, where anyone can read it. Call the API from your Node.js backend, a serverless function, or a small proxy route, and return only the audio_url to the browser, which can play it with an <audio> element.

javascript
// Express route: the browser calls /speak, the server holds the key
import express from "express"

const app = express()
app.use(express.json())

app.post("/speak", async (req, res) => {
  const upstream = await fetch("https://api.sayitapi.com/v1/tts", {
    method: "POST",
    headers: {
      "SAYIT-API-KEY": process.env.SAYIT_API_KEY,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ text: String(req.body.text || "").slice(0, 1000), voice_id: "eve" }),
  })
  const data = await upstream.json()
  if (!upstream.ok) return res.status(upstream.status).json(data)
  res.json({ audio_url: data.audio_url })
})

app.listen(3000)

Chunking long text

The documented per-request text limit is 250 characters on demo keys, 1,000 on free keys, and 10,000 on paid keys. GET /v1/account returns the limit as max_text_length, so read it at runtime instead of hardcoding it. This whitespace splitter keeps each request at or below that limit:

javascript
const API = "https://api.sayitapi.com/v1"
const headers = {
  "SAYIT-API-KEY": process.env.SAYIT_API_KEY,
  "Content-Type": "application/json",
}

const account = await (await fetch(API + "/account", { headers })).json()
const limit = account.max_text_length

function splitText(text, limit) {
  const chunks = []
  let chunk = ""
  for (let word of text.split(/\s+/).filter(Boolean)) {
    while (word.length > limit) {
      if (chunk) { chunks.push(chunk); chunk = "" }
      chunks.push(word.slice(0, limit))
      word = word.slice(limit)
    }
    const candidate = (chunk + " " + word).trim()
    if (candidate.length > limit) { chunks.push(chunk); chunk = word }
    else chunk = candidate
  }
  if (chunk) chunks.push(chunk)
  return chunks
}

const longText = "Paste a long article here."
for (const piece of splitText(longText, limit)) {
  const res = await fetch(API + "/tts", {
    method: "POST",
    headers,
    body: JSON.stringify({ text: piece, voice_id: "eve", language: "en" }),
  })
  if (!res.ok) throw new Error("TTS failed: " + res.status)
  console.log((await res.json()).audio_url)
}

Each piece is a separate synthesis request and is billed by its own text.length. Text longer than the plan allows is rejected with text_too_long.

Batch requests

POST /v1/tts/batch is for paid keys only. The batch size is 50, and the body is { "requests": [ { "text": "..." } ] }. The balance is reserved up front and failed items are refunded. The response is { "results": [...], "total": n }. Free and demo keys receive plan_required.

javascript
const res = await fetch("https://api.sayitapi.com/v1/tts/batch", {
  method: "POST",
  headers: {
    "SAYIT-API-KEY": process.env.SAYIT_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ requests: [{ text: "First clip." }, { text: "Second clip." }] }),
})
const result = await res.json()
console.log(result.total, result.results)

Errors, quota, and retries

Errors use { "error": { "code", "message" } }. Retry only 429 and 5xx responses. A 429 includes a Retry-After header. Do not retry 400, 401, 402, or 403; fix the request or key instead.

quota_exceeded (402) means the character balance is too low for the request, and the balance does not reset monthly. Read chars_balance and chars_used from GET /v1/account before large jobs. See pricing for character packs.

javascript
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))

async function postTts(payload, attempts = 5) {
  for (let attempt = 0; attempt < attempts; attempt++) {
    const res = await fetch("https://api.sayitapi.com/v1/tts", {
      method: "POST",
      headers: {
        "SAYIT-API-KEY": process.env.SAYIT_API_KEY,
        "Content-Type": "application/json",
      },
      body: JSON.stringify(payload),
    })

    // Retry only 429 and 5xx responses.
    if ((res.status === 429 || res.status >= 500) && attempt < attempts - 1) {
      const retryAfter = Number(res.headers.get("Retry-After"))
      await sleep((retryAfter > 0 ? retryAfter : 2 ** attempt) * 1000)
      continue
    }

    const data = await res.json()
    if (!res.ok) throw new Error(data.error?.code + ": " + data.error?.message)
    return data
  }
}

const data = await postTts({ text: "Hello from Node.js.", voice_id: "eve", language: "en" })
console.log(data.audio_url)

FAQ

Do I need an npm package or SDK?

No. There is no official npm package; the examples use the fetch built into Node.js 18+ against the REST endpoint. The same code works in Deno, Bun, and serverless functions that support fetch.

Can I call the API directly from the browser?

Do not ship your API key to the browser. Call SayIt from your server and pass only the audio_url to the page.

What is the maximum text per request?

Demo keys allow 250 characters, free keys 1,000, and paid keys 10,000. GET /v1/account returns the value as max_text_length; split longer text into chunks or use the batch endpoint.

How long is the audio URL available?

The signed audio_url expires after 24 hours, and generated audio is deleted after 24 hours, so download anything you want to keep.

Prefer Python? See the Python text-to-speech guide, or read the full API docs.

Node.js quick start

const res = await fetch("https://api.sayitapi.com/v1/tts", {
  method: "POST",
  headers: {
    "SAYIT-API-KEY": process.env.SAYIT_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ text: "Hello from Node.js.", voice_id: "eve", language: "en" }),
})
console.log((await res.json()).audio_url)

Powered by Grok TTS. Generated audio is deleted after 24 hours.

Live playground105 / 250
1.00×

API accepts 0.5–2.0. The speech engine applies 0.7–1.5.

curl -X POST https://api.sayitapi.com/v1/tts \
  -H "SAYIT-API-KEY: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"text":"Hello! This is SayIt. <emphasis>Important words</emphasis> stand out, and a pause [pause] sounds natural.","voice_id":"eve","language":"en","output_format":"mp3","speech_tags":true,"speed":1}'

Live demo uses a server-side key, limited to 250 characters. Audio expires after 24 hours.