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);
// STT tags this frame with `type` and keeps the fields top-level
// (TTS nests them under `connection_established` — the shapes differ).
if (msg.type === 'connection_established') {
console.log('✅ Authenticated!');
// Start session
ws.send(JSON.stringify({
type: 'start',
languages: ['en'],
config: {
encoding: 'mulaw',
sample_rate: 8000,
continuous_mode: true
}
}));
}
// Wait for `session_started`, not `connected` — `connected` fires before
// the session exists and arrives twice (proxy frame, then upstream frame).
if (msg.type === 'session_started') {
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();
}
};