Skip to Content
Vatis TechAPI docs
Real-time transcription

Real-time transcription

Stream audio over a WebSocket while it is recorded, and get the text back as it is spoken.

WS/v1/speech-to-text/realtime

One WebSocket connection is one session. Audio goes in binary frames; everything else is JSON text messages.

The API keeps no transcript of a live session: keep the finals you receive. Its segments can differ from those of a batch transcript of the same audio, which is made from the whole recording at once.

A session, from start to end

These steps are the Quickstart's program, piece by piece: a 16 kHz 16-bit mono WAV file sent as if someone were speaking, with speakers on. The whole program is in The whole program.

Open the WebSocket

The address is wss://api.eu.vatis.tech/v1/speech-to-text/realtime, and only model_id is required. Add the session's options to params. Open the connection with the header Authorization: Bearer and your key; a web page sends a token instead (Live text in a web page).

const url = "wss://api.eu.vatis.tech/v1/speech-to-text/realtime";
const headers = { Authorization: `Bearer ${process.env.VATIS_KEY}` };

// Request parameters
const params = { model_id: "vatis_ro_v9_realtime", diarize: "true" };

const ws = new WebSocket(`${url}?${new URLSearchParams(params)}`, { headers });

Expected result: the connection opens. JavaScript needs Node.js 22 or later; Go needs Go 1.23 or later and go get github.com/coder/websocket; Python needs pip install --upgrade websockets; Java needs Java 11 or later and C# .NET 8 or later, with nothing to install.

Wait for session.started

The first message says the session is ready. Start sending audio when it comes.

ws.onmessage = (event) => { // every message, until the server closes the connection
  const message = JSON.parse(event.data);
  if (message.type === "session.started") { // the session is ready: send the audio
    sendAudio();

Expected result: session.started, with every option as the API applied it in config (trimmed here). A refused session gets one error message instead, then the server closes: Errors in a session.

{"type": "session.started", "session_id": "sess_8d41c2a7e09b4f6a9c03d5e1b2a7f480", "model_id": "vatis_ro_v9_realtime",
 "config": {"encoding": "pcm_s16le", "sample_rate": 16000, "commit": "vad", "diarize": true, ...}}

Send audio

Send the audio in binary frames at the pace of real time: 100 ms every 100 ms, which is 3,200 bytes at 16 kHz. The code ends by sending end, the last step.

async function sendAudio() {
  for (let start = 0; start < audio.length; start += chunkSize) {
    ws.send(audio.subarray(start, start + chunkSize)); // bytes go as a binary frame
    await sleep(100); // at the pace of real time
  }
  ws.send(JSON.stringify({ type: "end" })); // no more audio
}

Expected result: no answer to each frame. The text comes as messages, in the next step. The rules for frames: Sending audio.

Read partial and final text

A partial is the segment so far, and the next one replaces it. A final is a finished segment, and it never changes.

} else if (message.type === "partial") { // the words so far, replaced by the next one
  console.log(`... ${message.text}`);
} else if (message.type === "final") { // a finished segment, it never changes
  console.log(`${message.speaker}: ${message.text}`);
} else if (message.type === "session.ended") { // after the last final
  console.log(`done: ${message.duration_s} s of audio`);
} else if (message.type === "error") {
  console.log(`error ${message.code}: ${message.message}`);
}

Expected result: messages like these. Times are seconds from the start of the session's audio. More on both: Partial and final text.

{"type": "partial", "segment_id": 1, "text": "bună ziua", "start": 0.12, "end": 1.28, "speaker": "speaker_0", "words": [...]}
{"type": "partial", "segment_id": 1, "text": "bună ziua luați loc", "start": 0.12, "end": 1.92, "speaker": "speaker_0", "words": [...]}
{"type": "final", "segment_id": 1, "text": "Bună ziua, luați loc.", "start": 0.12, "end": 1.7, "speaker": "speaker_0"}

End the session

When there is no more audio, send {"type": "end"}, as the last line of the audio step does, and keep reading until the server closes.

Expected result: the last final messages, then session.ended, then the server closes with code 1000.

{"type": "final", "segment_id": 2, "text": "Am venit pe 12 martie.", "start": 3.6, "end": 5.2, "speaker": "speaker_1"}
{"type": "session.ended", "session_id": "sess_8d41c2a7e09b4f6a9c03d5e1b2a7f480", "duration_s": 5.6}
(close 1000)

The whole program

The steps above, as one program: the one the console's Real-time page shows in its Code view. It sends a WAV file at the pace of real time and prints the messages. The file must be 16 kHz, 16-bit, mono: the conversion command is in the program.

// Needs Node.js 22 or newer, no packages.
// Set your API key, then run: export VATIS_KEY=your_api_key && node live.mjs
import { readFileSync } from "node:fs";
import { setTimeout as sleep } from "node:timers/promises";

const url = "wss://api.eu.vatis.tech/v1/speech-to-text/realtime";
const headers = { Authorization: `Bearer ${process.env.VATIS_KEY}` };

// Request parameters
const params = {
  model_id: "vatis_ro_v9_realtime",
  diarize: "true",
};

// The audio: a 16 kHz 16-bit mono WAV. To convert a file:
// ffmpeg -i your-audio.mp3 -ac 1 -ar 16000 -c:a pcm_s16le speech.wav
const wav = readFileSync("speech.wav");
const audio = wav.subarray(wav.indexOf("data") + 8);
const chunkSize = 3200; // 100 ms of audio

// Open the session
const ws = new WebSocket(`${url}?${new URLSearchParams(params)}`, { headers });

// Send the audio at real-time pace
async function sendAudio() {
  for (let start = 0; start < audio.length; start += chunkSize) {
    ws.send(audio.subarray(start, start + chunkSize));
    await sleep(100);
  }
  ws.send(JSON.stringify({ type: "end" }));
  console.log("finished sending");
}

// Print the messages as they come
ws.onmessage = (event) => {
  const message = JSON.parse(event.data);
  if (message.type === "session.started") {
    console.log("connected");
    sendAudio();
  } else if (message.type === "partial") {
    console.log(`... ${message.text}`);
  } else if (message.type === "final") {
    console.log(`${message.speaker}: ${message.text}`);
  } else if (message.type === "session.ended") {
    console.log(`done: ${message.duration_s} s of audio`);
  } else if (message.type === "error") {
    console.log(`error ${message.code}: ${message.message}`);
  }
};

// A failed connection is an error; when the server closes, the program ends
ws.onerror = () => {
  console.error("connection failed");
  process.exit(1);
};
ws.onclose = (event) => {
  if (event.code !== 1000) { // 1000 is the normal end
    console.error(`connection failed: close code ${event.code}`);
    process.exit(1);
  }
  process.exit();
};

In this guide

Next steps

On this page