Server-side Streaming

Backend pushes an audio stream to LansonAI.

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

Architecture

Your backend                     LansonAI                    Upstream STT
  │                                │                            │
  ├── WS connect (Bearer sk-...) ─→ session.created              │
  │                                │                            │
  ├── read audio file ───────────│                            │
  ├── convert to PCM16LE/16k ─────│                            │
  ├── send binary frames ─────────→ gateway relay ────────────→ VAD + STT
  │←── conversation.item.input_audio_transcription.completed ─│←──────────│
  │                                │                            │
  ├── close ──────────────────────→ meter flush                 │

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", () => {
    // 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) {
        ws.send(JSON.stringify({ type: "input_audio_buffer.flush" }));
        return;
      }
      const frame = audio.slice(offset, offset + FRAME_BYTES);
      ws.send(frame); // binary frame
      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
  • Close explicitly when done to flush meter data
  • 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.