> ## Documentation Index
> Fetch the complete documentation index at: https://60db.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# WebSocket API

> Real-time Speech-to-Text (STT) and Text-to-Speech (TTS) streaming API

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# WebSocket API

Real-time bidirectional streaming API for Speech-to-Text (STT) and Text-to-Speech (TTS) services.

> **Try it out!** [Test your WebSocket connection](/websocket-test) to verify everything works.

## Overview

The 60db WebSocket API provides real-time streaming capabilities for:

* **Speech-to-Text (STT)**: Convert audio to text with 99+ language support
* **Text-to-Speech (TTS)**: Synthesize natural-sounding speech with multiple voices

## Base URLs

| Environment         | STT URL                    | TTS URL                    |
| ------------------- | -------------------------- | -------------------------- |
| Production          | `ws://api.60db.ai/ws/stt`  | `ws://api.60db.ai/ws/tts`  |
| Production (Secure) | `wss://api.60db.ai/ws/stt` | `wss://api.60db.ai/ws/tts` |

## Authentication

WebSocket connections require authentication using API key via query parameter:

```
ws://api.60db.ai/ws/stt?apiKey=sk_live_your_api_key_here
```

**Getting Your API Key:**

1. Go to [app.60db.ai](https://app.60db.ai)
2. Navigate to **Settings → Developer → API Keys**
3. Click **Create API Key**
4. Copy and store your API key securely

***

## 🎤 STT WebSocket (Speech-to-Text)

**What it does:** You send audio → You get text back

**Use for:** Transcribing speech, voice commands, call center analytics, meeting transcription

### Quick Start

```javascript theme={null}
const WebSocket = require('ws');

const API_KEY = 'sk_live_your_key';
const ws = new WebSocket(`wss://api.60db.ai/ws/stt?apiKey=${API_KEY}`);

ws.onopen = () => console.log('✅ Connected');

ws.onmessage = (data) => {
  const msg = JSON.parse(data);

  if (msg.connection_established) {
    console.log('✅ Authenticated!');

    // Start session
    ws.send(JSON.stringify({
      type: 'start',
      languages: ['en'],
      config: {
        encoding: 'mulaw',
        sample_rate: 8000,
        continuous_mode: true
      }
    }));
  }

  if (msg.type === 'connected') {
    console.log('✅ Ready! Send audio now');

    // Send audio chunks
    const interval = setInterval(() => {
      ws.send(audioBuffer); // Your audio data
    }, 60);

    // Stop after 5 seconds
    setTimeout(() => {
      clearInterval(interval);
      ws.send(JSON.stringify({ type: 'stop' }));
    }, 5000);
  }

  if (msg.type === 'transcription') {
    console.log('📝 Text:', msg.text);
  }

  if (msg.type === 'session_stopped') {
    console.log('✅ Done! Cost:', msg.billing_summary.total_cost);
    ws.close();
  }
};
```

### How STT Works

```
1. Connect → 2. Authenticate → 3. Start session → 4. Send audio → 5. Get text → 6. Stop
```

***

## 🔊 TTS WebSocket (Text-to-Speech)

**What it does:** You send text → You get audio back

**Use for:** Voice assistants, audiobooks, accessibility, chatbots

### Quick Start

```javascript theme={null}
const WebSocket = require('ws');
const fs = require('fs');

const API_KEY = 'sk_live_your_key';
const ws = new WebSocket(`wss://api.60db.ai/ws/tts?apiKey=${API_KEY}`);
const contextId = 'my-session-' + Date.now();
const audioChunks = [];

ws.onopen = () => console.log('✅ Connected');

ws.onmessage = (data) => {
  const msg = JSON.parse(data);

  if (msg.connection_established) {
    console.log('✅ Authenticated!');

    // Create context
    ws.send(JSON.stringify({
      create_context: {
        context_id: contextId,
        voice_id: 'fbb75ed2-975a-40c7-9e06-38e30524a9a1',
        audio_config: {
          audio_encoding: 'LINEAR16',
          sample_rate_hertz: 16000
        }
      }
    }));
  }

  if (msg.context_created) {
    console.log('✅ Context created!');

    // Send text
    ws.send(JSON.stringify({
      send_text: {
        context_id: contextId,
        text: 'Hello, this is a test of the text to speech service.'
      }
    }));

    // Flush
    ws.send(JSON.stringify({
      flush_context: { context_id: contextId }
    }));
  }

  if (msg.audio_chunk) {
    const audioData = Buffer.from(msg.audio_chunk.audioContent, 'base64');
    audioChunks.push(audioData);
    console.log('🔊 Audio chunk received');
  }

  if (msg.flush_completed) {
    console.log('✅ All audio received!');

    // Close context
    ws.send(JSON.stringify({
      close_context: { context_id: contextId }
    }));
  }

  if (msg.context_closed) {
    console.log('✅ Done!');

    // Save audio
    const audio = Buffer.concat(audioChunks);
    fs.writeFileSync('output.pcm', audio);
    console.log('💾 Saved output.pcm');

    ws.close();
  }
};
```

### How TTS Works

```
1. Connect → 2. Authenticate → 3. Create context → 4. Send text → 5. Flush → 6. Get audio → 7. Close
```

***

## 📋 STT vs TTS Comparison

| Feature           | STT (Speech-to-Text)     | TTS (Text-to-Speech)        |
| ----------------- | ------------------------ | --------------------------- |
| **Input**         | Audio (binary)           | Text (string)               |
| **Output**        | Text (string)            | Audio (binary)              |
| **Use Case**      | Transcribe speech        | Generate speech             |
| **Direction**     | Audio → Text             | Text → Audio                |
| **First Message** | `{ type: "start", ... }` | `{ create_context: {...} }` |
| **Session End**   | `{ type: "stop" }`       | `{ close_context: {...} }`  |
| **Pricing**       | \$0.00000833/second      | \$0.00002/character         |

***

## 🧪 Test Your Connection

[Go to WebSocket Test Page](/websocket-test) to test your WebSocket connection in the browser!

***

## 📚 Full Documentation

* 🎤 [STT WebSocket](/websocket-api/stt) - Complete STT guide with all parameters
* 🔊 [TTS WebSocket](/websocket-api/tts) - Complete TTS guide with all parameters
* 🧪 [WebSocket Test](/websocket-test) - Test connections in browser

***

## 💡 Key Concepts

### STT Messages

```javascript theme={null}
// Start session
{ type: "start", languages: ["en"], config: {...} }

// Stop session
{ type: "stop" }
```

### TTS Messages

```javascript theme={null}
// Create context
{ create_context: { context_id, voice_id, audio_config } }

// Send text
{ send_text: { context_id, text: "..." } }

// Get audio
{ flush_context: { context_id } }

// Close session
{ close_context: { context_id } }
```

***

## 📊 Pricing

| Service | Rate                | Minimum |
| ------- | ------------------- | ------- |
| STT     | \$0.00000833/second | \$0.01  |
| TTS     | \$0.00002/character | \$0.01  |

***

## 🆘 Support

* **Email**: [support@60db.ai](mailto:support@60db.ai)
* **Documentation**: [https://docs.60db.ai](https://docs.60db.ai)
* **Status**: [https://status.60db.ai](https://status.60db.ai)
