Skip to main content

Overview

The WebSocket endpoint provides real-time text-to-speech conversion with streaming audio output. This is ideal for applications requiring low-latency audio generation (e.g. interactive assistants). For one-shot or chunked HTTP streaming, see TTS REST or TTS SSE.

Endpoint

wss://api.vachana.ai/api/v1/tts

Authentication

All WebSocket connections require the following headers:
HeaderRequiredDescriptionExample
Content-TypeYesMust be application/jsonapplication/json
X-API-Key-IDYesYour API key for authentication<your-api-key-id>

Request Format

Send a JSON message with the following structure:
{
  "text": "Websocket Endpoint production test..",
  "voice": "Sia",
  "language": "IND-IN",
  "model": "vachana-voice-v1",
  "audio_config": {
    "sample_rate": 44100,
    "num_channels": 1,
    "sample_width": 2,
    "encoding": "linear_pcm",
    "container": "wav"
  }
}
num_channels
integer
required
Number of audio channels (e.g., 1 for mono, 2 for stereo)
sample_width
integer
required
Sample width in bytes (e.g., 2 for 16-bit audio)
encoding
string
required
Audio encoding format (e.g., linear_pcm)
container
string
required
Audio container format (e.g., wav)

Response

The server streams audio data in real-time as binary chunks. Each chunk contains PCM audio data according to the specified audio_config.

Example Usage

const ws = new WebSocket("wss://api.vachana.ai/api/v1/tts", {
  headers: {
    "Content-Type": "application/json",
    "X-API-Key-ID": "<your-api-key>",
  },
});

ws.on("open", () => {
  const request = {
    text: "Websocket Endpoint production test..",
    voice: "Sia",
    language: "IND-IN",
    model: "vachana-voice-v1",
    audio_config: {
      sample_rate: 44100,
      num_channels: 1,
      sample_width: 2,
      encoding: "linear_pcm",
      container: "wav",
    },
  };

  ws.send(JSON.stringify(request));
});

ws.on("message", (data) => {
  // Handle audio chunks
  console.log("Received audio chunk:", data);
});

ws.on("error", (error) => {
  console.error("WebSocket error:", error);
});

ws.on("close", () => {
  console.log("WebSocket connection closed");
});