Real-time API Examples
Complete code examples for real-time transcription.
Browser Live Captions
<!DOCTYPE html>
<html>
<head>
<title>Live Captions</title>
</head>
<body>
<h1>Live Captions</h1>
<button id="start">Start</button>
<button id="stop">Stop</button>
<div id="transcript"></div>
<script>
const API_KEY = 'YOUR_API_KEY';
let ws;
let audioContext;
let processor;
document.getElementById('start').onclick = async () => {
// 1. Initialize session
const response = await fetch(
'https://api.scriptix.io/api/v4/realtime/session',
{
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
language: 'en',
sample_rate: 16000
})
}
);
const session = await response.json();
// 2. Connect WebSocket
ws = new WebSocket(session.websocket_url);
ws.onmessage = (event) => {
const message = JSON.parse(event.data);
if (message.type === 'final') {
document.getElementById('transcript').innerHTML +=
``;
}
};
// 3. Capture microphone
const stream = await navigator.mediaDevices.getUserMedia({
audio: true
});
audioContext = new AudioContext({ sampleRate: 16000 });
const source = audioContext.createMediaStreamSource(stream);
processor = audioContext.createScriptProcessor(1024, 1, 1);
source.connect(processor);
processor.connect(audioContext.destination);
processor.onaudioprocess = (e) => {
const float32 = e.inputBuffer.getChannelData(0);
const pcm16 = convertToPCM16(float32);
ws.send(pcm16);
};
};
document.getElementById('stop').onclick = () => {
if (processor) processor.disconnect();
if (audioContext) audioContext.close();
if (ws) {
ws.send(JSON.stringify({ type: 'end_of_session' }));
ws.close();
}
};
function convertToPCM16(float32Array) {
const pcm16 = new Int16Array(float32Array.length);
for (let i = 0; i < float32Array.length; i++) {
const s = Math.max(-1, Math.min(1, float32Array[i]));
pcm16[i] = s < 0 ? s * 0x8000 : s * 0x7FFF;
}
return pcm16.buffer;
}
</script>
</body>
</html>
Python Live Transcription
import requests
import websocket
import json
import pyaudio
import threading
API_KEY = 'YOUR_API_KEY'
def transcribe_microphone():
# 1. Initialize session
response = requests.post(
'https://api.scriptix.io/api/v4/realtime/session',
headers={
'Authorization': f'Bearer {API_KEY}',
'Content-Type': 'application/json'
},
json={
'language': 'en',
'sample_rate': 16000,
'interim_results': True
}
)
session = response.json()
ws_url = session['websocket_url']
# 2. WebSocket handlers
def on_message(ws, message):
data = json.loads(message)
if data['type'] == 'final':
print(f"\nFinal: {data['text']}")
elif data['type'] == 'partial':
print(f"\rPartial: {data['text']}", end='', flush=True)
def on_error(ws, error):
print(f"\nError: {error}")
def on_close(ws):
print("\nDisconnected")
# 3. Connect WebSocket
ws = websocket.WebSocketApp(
ws_url,
on_message=on_message,
on_error=on_error,
on_close=on_close
)
# 4. Stream microphone in separate thread
def stream_audio():
p = pyaudio.PyAudio()
stream = p.open(
format=pyaudio.paInt16,
channels=1,
rate=16000,
input=True,
frames_per_buffer=640 # 20ms chunks
)
print("Listening... (Ctrl+C to stop)")
try:
while True:
data = stream.read(640)
ws.send(data, opcode=websocket.ABNF.OPCODE_BINARY)
except KeyboardInterrupt:
ws.send(json.dumps({'type': 'end_of_session'}))
ws.close()
stream.stop_stream()
stream.close()
p.terminate()
# Run both threads
threading.Thread(target=ws.run_forever).start()
threading.Thread(target=stream_audio).start()
if __name__ == '__main__':
transcribe_microphone()
Node.js Call Center Example
const axios = require('axios');
const WebSocket = require('ws');
const mic = require('mic');
const API_KEY = 'YOUR_API_KEY';
async function transcribeCall() {
// 1. Initialize session
const response = await axios.post(
'https://api.scriptix.io/api/v4/realtime/session',
{
language: 'en',
sample_rate: 16000,
interim_results: true
},
{
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
}
}
);
const { websocket_url } = response.data;
// 2. Connect WebSocket
const ws = new WebSocket(websocket_url);
ws.on('open', () => {
console.log('Connected to real-time API');
// 3. Start microphone
const micInstance = mic({
rate: '16000',
channels: '1',
encoding: 'signed-integer',
bitwidth: '16'
});
const micInputStream = micInstance.getAudioStream();
micInputStream.on('data', (data) => {
ws.send(data);
});
micInstance.start();
// Stop after 60 seconds
setTimeout(() => {
micInstance.stop();
ws.send(JSON.stringify({ type: 'end_of_session' }));
ws.close();
}, 60000);
});
// 4. Process exports
const transcripts = [];
ws.on('message', (data) => {
const message = JSON.parse(data);
if (message.type === 'final') {
console.log(`[${new Date().toISOString()}] ${message.text}`);
transcripts.push(message.text);
} else if (message.type === 'partial') {
process.stdout.write(`\r${message.text.padEnd(80)}`);
}
});
ws.on('close', () => {
console.log('\nCall transcription complete');
console.log('\nFull transcript:');
console.log(transcripts.join(' '));
});
}
transcribeCall();