Server-side Streaming

PreviousNext

Stream server-side audio into Lanson Live API.

Connect your backend to Lanson Live API to stream audio from server-side sources (files, pipelines, telephony).

Architecture

Server-side Streaming Architecture

Authentication

Server-side connections use the API key directly:

const ws = new WebSocket(
  "wss://audio.lansonai.com/v1/audio/transcriptions/stream",
  { headers: { Authorization: "Bearer sk-..." } }
);

No session token needed — the key never leaves your server.

Streaming a file

import { readFileSync } from "fs";
import WebSocket from "ws";

const ws = new WebSocket(
  "wss://audio.lansonai.com/v1/audio/transcriptions/stream",
  { headers: { Authorization: "Bearer sk-..." } }
);

ws.on("open", () => {
  // Recommended: set the audio language before sending audio
  ws.send(JSON.stringify({ type: "session.update", language: "en" }));

  // Read pre-converted PCM16LE 16kHz mono audio
  const audio = readFileSync("audio.pcm");
  const FRAME_BYTES = 3200; // 100ms at 16kHz 16-bit mono
  let offset = 0;

  const sendFrame = () => {
    if (offset >= audio.length) {
      // Continue capture through natural pauses so VAD can finish the last segment.
      // Manual flush is not implemented on the public endpoint.
      return;
    }
    const frame = audio.subarray(offset, offset + FRAME_BYTES);
    // Send base64 JSON frames (binary frames are not transcribed today)
    ws.send(JSON.stringify({
      type: "input_audio_buffer.append",
      audio: frame.toString("base64"),
    }));
    offset += FRAME_BYTES;
    setTimeout(sendFrame, 100); // simulate real-time
  };
  sendFrame();
});

ws.on("message", (data) => {
  const event = JSON.parse(data.toString());
  if (event.type === "conversation.item.input_audio_transcription.completed") {
    console.log(`[${event.utterance_index}] ${event.text}`);
  }
});

Converting audio server-side

ffmpeg -i input.mp3 -ar 16000 -ac 1 -c:a pcm_s16le output.pcm

Connection management

  • Keep the connection alive by sending frames at a steady rate
  • If audio source pauses, send silent frames to avoid idle timeout
  • Wait for expected completed events before closing; close finalizes usage accounting, not transcript delivery
  • For long-running streams, monitor session duration limits

Token management

Server-side connections do not need session tokens. Use Authorization: Bearer sk-... in the WebSocket upgrade headers.