> ## 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 Test

> Test 60db WebSocket connections in your browser

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

# WebSocket Test

Test your WebSocket connections directly in the browser to verify everything is working correctly.

## STT Connection Test

Test your Speech-to-Text WebSocket connection.

<div style={{padding: '20px', border: '1px solid #e0e0e0', borderRadius: '8px', marginBottom: '20px'}}>
  <div style={{marginBottom: '15px'}}>
    <label style={{display: 'block', marginBottom: '5px', fontWeight: 'bold'}}>API Key:</label>

    <input type="text" id="stt-api-key" placeholder="Enter your API key (sk_live_...)" style={{width: '100%', padding: '8px', border: '1px solid #ccc', borderRadius: '4px'}} />
  </div>

  <button
    onClick={() => {
  const apiKey = document.getElementById('stt-api-key').value;
  if (!apiKey) {
    alert('Please enter your API key');
    return;
  }
  const ws = new WebSocket(`wss://api.60db.ai/ws/stt?apiKey=${apiKey}`);
  const output = document.getElementById('stt-output');

  output.innerHTML = '<div style="color: blue;">Connecting...</div>';

  ws.onopen = () => {
    output.innerHTML += '<div style="color: green;">✓ Connected to STT WebSocket!</div>';

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

  ws.onmessage = (event) => {
    const msg = JSON.parse(event.data);
    const msgType = msg.type || Object.keys(msg)[0];
    output.innerHTML += `<div style="color: #333;">← ${msgType}: ${JSON.stringify(msg).substring(0, 100)}...</div>`;

    if (msg.type === 'connected') {
      setTimeout(() => {
        ws.send(JSON.stringify({ type: 'stop' }));
      }, 1000);
    }

    if (msg.type === 'session_stopped') {
      output.innerHTML += '<div style="color: green; font-weight: bold;">✓ Test completed successfully!</div>';
      ws.close();
    }
  };

  ws.onerror = (error) => {
    output.innerHTML += '<div style="color: red;">✗ Connection error</div>';
  };

  ws.onclose = () => {
    output.innerHTML += '<div style="color: #666;">Connection closed</div>';
  };
}}
    style={{padding: '10px 20px', background: '#4A3228', color: 'white', border: 'none', borderRadius: '4px', cursor: 'pointer'}}
  >
    Test STT Connection
  </button>

  <div id="stt-output" style={{marginTop: '15px', padding: '10px', background: '#f5f5f5', borderRadius: '4px', minHeight: '100px', fontFamily: 'monospace', fontSize: '12px'}}>
    Output will appear here...
  </div>
</div>

## TTS Connection Test

Test your Text-to-Speech WebSocket connection.

<div style={{padding: '20px', border: '1px solid #e0e0e0', borderRadius: '8px', marginBottom: '20px'}}>
  <div style={{marginBottom: '15px'}}>
    <label style={{display: 'block', marginBottom: '5px', fontWeight: 'bold'}}>API Key:</label>

    <input type="text" id="tts-api-key" placeholder="Enter your API key (sk_live_...)" style={{width: '100%', padding: '8px', border: '1px solid #ccc', borderRadius: '4px'}} />
  </div>

  <button
    onClick={() => {
  const apiKey = document.getElementById('tts-api-key').value;
  if (!apiKey) {
    alert('Please enter your API key');
    return;
  }
  const ws = new WebSocket(`wss://api.60db.ai/ws/tts?apiKey=${apiKey}`);
  const contextId = 'test-' + Date.now();
  const output = document.getElementById('tts-output');

  output.innerHTML = '<div style="color: blue;">Connecting...</div>';

  ws.onopen = () => {
    output.innerHTML += '<div style="color: green;">✓ Connected to TTS WebSocket!</div>';

    // 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
        }
      }
    }));
  };

  ws.onmessage = (event) => {
    const msg = JSON.parse(event.data);
    const msgType = Object.keys(msg)[0];
    output.innerHTML += `<div style="color: #333;">← ${msgType}: ${JSON.stringify(msg).substring(0, 100)}...</div>`;

    if (msg.context_created) {
      // Send text
      ws.send(JSON.stringify({
        send_text: {
          context_id: contextId,
          text: 'Hello, this is a test.'
        }
      }));

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

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

    if (msg.context_closed) {
      output.innerHTML += '<div style="color: green; font-weight: bold;">✓ Test completed successfully!</div>';
      ws.close();
    }
  };

  ws.onerror = (error) => {
    output.innerHTML += '<div style="color: red;">✗ Connection error</div>';
  };

  ws.onclose = () => {
    output.innerHTML += '<div style="color: #666;">Connection closed</div>';
  };
}}
    style={{padding: '10px 20px', background: '#4A3228', color: 'white', border: 'none', borderRadius: '4px', cursor: 'pointer'}}
  >
    Test TTS Connection
  </button>

  <div id="tts-output" style={{marginTop: '15px', padding: '10px', background: '#f5f5f5', borderRadius: '4px', minHeight: '100px', fontFamily: 'monospace', fontSize: '12px'}}>
    Output will appear here...
  </div>
</div>

## Manual Testing with Browser Console

You can also test WebSockets directly in your browser's developer console:

### STT Test

```javascript theme={null}
// Open browser console (F12) and paste this code:
const ws = new WebSocket('wss://api.60db.ai/ws/stt?apiKey=sk_live_your_key');

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

ws.onmessage = (event) => {
  const msg = JSON.parse(event.data);
  console.log('←', msg.type || Object.keys(msg)[0], msg);
};

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

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

### TTS Test

```javascript theme={null}
// Open browser console (F12) and paste this code:
const ws = new WebSocket('wss://api.60db.ai/ws/tts?apiKey=sk_live_your_key');
const ctx = 'test-' + Date.now();

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

ws.onmessage = (event) => {
  const msg = JSON.parse(event.data);
  console.log('←', Object.keys(msg)[0], msg);

  if (msg.connection_established) {
    ws.send(JSON.stringify({
      create_context: {
        context_id: ctx,
        voice_id: 'fbb75ed2-975a-40c7-9e06-38e30524a9a1',
        audio_config: { audio_encoding: 'LINEAR16', sample_rate_hertz: 16000 }
      }
    }));
  }

  if (msg.context_created) {
    ws.send(JSON.stringify({ send_text: { context_id: ctx, text: 'Hello!' } }));
    ws.send(JSON.stringify({ flush_context: { context_id: ctx } }));
  }

  if (msg.context_closed) console.log('✓ Test completed!');
};
```

## Using wscat (Command Line)

Install wscat:

```bash theme={null}
npm install -g wscat
```

### Test STT

```bash theme={null}
wscat -c "wss://api.60db.ai/ws/stt?apiKey=sk_live_your_key"
# Then send: {"type":"start","languages":["en"],"config":{"encoding":"mulaw","sample_rate":8000}}
# Then send: {"type":"stop"}
```

### Test TTS

```bash theme={null}
wscat -c "wss://api.60db.ai/ws/tts?apiKey=sk_live_your_key"
# Then send: {"create_context":{"context_id":"test","voice_id":"fbb75ed2-975a-40c7-9e06-38e30524a9a1","audio_config":{"audio_encoding":"LINEAR16","sample_rate_hertz":16000}}}
# Then send: {"send_text":{"context_id":"test","text":"Hello"}}
# Then send: {"flush_context":{"context_id":"test"}}
```

## Connection Status Meanings

| Status             | Meaning                                       |
| ------------------ | --------------------------------------------- |
| ✓ Connected        | WebSocket connection established successfully |
| ✓ Authenticated    | API key validated successfully                |
| ✓ Session started  | STT/TTS session ready                         |
| ✓ Test completed   | All tests passed successfully                 |
| ✗ Connection error | Failed to connect (check API key or network)  |

## Troubleshooting

### Connection fails

* **Check API key**: Make sure it starts with `sk_live_`
* **Check network**: Try from a different browser or network
* **Check URL**: Use `wss://` for secure, `ws://` for non-secure

### Authentication fails

* **Verify API key**: Get a fresh key from [app.60db.ai](https://app.60db.ai)
* **Check credits**: Ensure you have sufficient credits

### Session not starting

* **Check logs**: Look at the console output for error messages
* **Verify parameters**: Ensure all required fields are included

## Related

* [STT WebSocket](/websocket-api/stt) - Complete STT documentation
* [TTS WebSocket](/websocket-api/tts) - Complete TTS documentation
