Developers
Get started
Developer quickstart
Learn the basics and make your first request with the Ultrasafe AI API. Generate text, run chat completions, and explore all available models.
Replace
YOUR_API_KEY with your actual API key to make requests.1// Buffer to hold incoming audio chunks before the upstream session is ready
2const audioBuffer: Buffer[] = [];
3let sessionReady = false;
4
5// Open a streaming ASR session with the UltraSafe API
6const session = usf.audio.stream.connect({
7 model: 'usf-mini-asr',
8 sampleRate: 16000,
9 audioFormat: 'pcm_s16le',
10 enableVad: false,
11 partialResults: true,
12 interimMinDurationMs: 500,
13 fullContextRetranscription: true,
14});
15
16// Once the upstream session is open, flush any buffered audio
17session.onopen = () => {
18 sessionReady = true;
19 for (const chunk of audioBuffer) session.sendAudio(chunk);
20 audioBuffer.length = 0;
21};
22
23// Forward transcript events to the client WebSocket
24session.ontranscript = (event) => {
25 if (socket.readyState === 1) socket.send(JSON.stringify(event));
26};
27
28// Propagate upstream session close to the client
29session.onclose = (code, reason) => {
30 if (socket.readyState === 1) socket.close(code, reason);
31};
32
33// On upstream error, notify the client and close
34session.onerror = (err) => {
35 if (socket.readyState === 1) {
36 socket.send(JSON.stringify({ error: err.message }));
37 socket.close();
38 }
39};
40
41// Relay incoming audio from the client to the upstream ASR session
42socket.on('message', (data: Buffer) => {
43 if (sessionReady) {
44 session.sendAudio(data);
45 } else {
46 audioBuffer.push(data);
47 }
48});
49
50// When the client disconnects, close the upstream ASR session
51socket.on('close', () => {
52 if (session.readyState === 1) session.close();
53});