Soniox

Types

Soniox Client SDK — Types Reference

ApiKeyConfig

type ApiKeyConfig = string | () => Promise<string>;

API key configuration.

  • string - A pre-fetched temporary API key (e.g., injected from SSR)
  • () => Promise<string> - An async function that fetches a fresh temporary key from your backend. Called once per recording session.

Example

// Static key (for demos or SSR-injected keys)
const client = new SonioxClient({ api_key: 'temp:...' });

// Async function (recommended for production)
const client = new SonioxClient({
  api_key: async () => {
    const res = await fetch('/api/get-temporary-key', { method: 'POST' });
    const { api_key } = await res.json();
    return api_key;
  },
});

Note: If you use Node.js, you can use the SonioxNodeClient to fetch a temporary API key via client.auth.createTemporaryKey().


AudioErrorCode

type AudioErrorCode = "permission_denied" | "device_not_found" | "audio_unavailable";

Error codes for audio-related errors


AudioSourceHandlers

type AudioSourceHandlers = {
  onData: (chunk) => void;
  onError: (error) => void;
  onMuted?: () => void;
  onUnmuted?: () => void;
};

Callbacks for receiving audio data and errors from an AudioSource.

Properties

PropertyTypeDescription
onData(chunk) => voidCalled when an audio chunk is available.
onError(error) => voidCalled when a runtime error occurs during audio capture (after start).
onMuted?() => voidCalled when the audio source is muted externally (e.g. OS-level or hardware mute).
onUnmuted?() => voidCalled when the audio source is unmuted after an external mute.

MicrophoneSourceOptions

type MicrophoneSourceOptions = {
  constraints?: MediaTrackConstraints;
  recorderOptions?: MediaRecorderOptions;
  timesliceMs?: number;
};

Options for MicrophoneSource

Properties

PropertyTypeDescription
constraints?MediaTrackConstraintsMediaTrackConstraints for the audio track. Default { echoCancellation: false, noiseSuppression: false, autoGainControl: false, channelCount: 1, sampleRate: 44100 }
recorderOptions?MediaRecorderOptionsMediaRecorder options. See https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/MediaRecorder
timesliceMs?numberTime interval in milliseconds between audio data chunks. Default 60

PermissionResult

type PermissionResult = {
  can_request: boolean;
  status: PermissionStatus;
};

Result of a permission check or request.

Properties

PropertyTypeDescription
can_requestbooleanWhether the user can be prompted again. false means permanently denied (e.g., browser "Block" or iOS settings). Useful for showing "go to settings" instructions.
statusPermissionStatusCurrent permission status.

PermissionStatus

type PermissionStatus = "granted" | "denied" | "prompt" | "unavailable";

Unified permission status across all platforms.


PermissionType

type PermissionType = "microphone";

Permission types supported by the resolver.


RecordOptions

type RecordOptions = SttSessionConfig & {
  buffer_queue_size?: number;
  session_options?: SttSessionOptions;
  signal?: AbortSignal;
  source?: AudioSource;
};

Options for creating a recording

Type Declaration

NameTypeDescription
buffer_queue_size?numberMaximum number of audio chunks to buffer while waiting for key/connection Default 1000
session_options?SttSessionOptionsSDK-level session options (signal, etc.)
signal?AbortSignalAbortSignal for cancellation
source?AudioSourceAudio source to use. Defaults to MicrophoneSource if not provided.

RecordingEvents

type RecordingEvents = {
  connected: () => void;
  endpoint: () => void;
  error: (error) => void;
  finalized: () => void;
  finished: () => void;
  result: (result) => void;
  source_muted: () => void;
  source_unmuted: () => void;
  state_change: (update) => void;
  token: (token) => void;
};

Events emitted by a Recording instance

Properties

PropertyTypeDescription
connected() => voidWebSocket connected and ready.
endpoint() => voidEndpoint detected (speaker finished talking).
error(error) => voidError occurred during recording.
finalized() => voidFinalization complete.
finished() => voidRecording finished (server acknowledged end of stream).
result(result) => voidParsed result received from the server.
source_muted() => voidAudio source was muted externally (e.g. OS-level or hardware mute).
source_unmuted() => voidAudio source was unmuted after an external mute.
state_change(update) => voidRecording state transition.
token(token) => voidIndividual token received.

RecordingState

type RecordingState = 
  | "idle"
  | "starting"
  | "connecting"
  | "recording"
  | "paused"
  | "stopping"
  | "stopped"
  | "error"
  | "canceled";

Unified recording lifecycle states.


SonioxClientOptions

type SonioxClientOptions = {
  api_key: ApiKeyConfig;
  buffer_queue_size?: number;
  default_session_options?: SttSessionOptions;
  permissions?: PermissionResolver;
  ws_base_url?: string;
};

Options for creating a SonioxClient instance.

Properties

PropertyTypeDescription
api_keyApiKeyConfigAPI key configuration. - string - A pre-fetched temporary API key (e.g., injected from SSR) - () => Promise<string> - Async function that fetches a fresh key from your backend
buffer_queue_size?numberDefault maximum number of audio chunks to buffer while waiting for key/connection. Can be overridden per-recording. Default 1000
default_session_options?SttSessionOptionsDefault session options applied to all sessions. Can be overridden per-recording.
permissions?PermissionResolverOptional permission resolver for pre-flight microphone permission checks. Not set by default (SSR-safe, RN-safe). Example import { BrowserPermissionResolver } from '@soniox/client'; const client = new SonioxClient({ api_key: fetchKey, permissions: new BrowserPermissionResolver(), });
ws_base_url?stringWebSocket URL for real-time connections. Default 'wss://stt-rt.soniox.com/transcribe-websocket'

SttOptions

type SttOptions = {
  api_key: string;
  session_options?: SttSessionOptions;
};

Options for creating a low-level STT session.

Properties

PropertyTypeDescription
api_keystringResolved API key string (temporary key).
session_options?SttSessionOptionsSession options (signal, etc.).

AudioSource

Platform-agnostic audio source interface.

Implementations must:

  • Begin capturing audio in start() and deliver chunks via handlers.onData
  • Stop all capture and release resources in stop()
  • Throw typed errors from start() if capture cannot begin (e.g., permission denied)

Example

// Built-in browser source
const source = new MicrophoneSource();

// Custom source (e.g., React Native)
class MyAudioSource implements AudioSource {
  async start(handlers: AudioSourceHandlers) { ... }
  stop() { ... }
}

Methods

pause()?

optional pause(): void;

Pause audio capture (optional). When paused, no data should be delivered via onData.

Returns

void


resume()?

optional resume(): void;

Resume audio capture after pause (optional).

Returns

void


start()

start(handlers): Promise<void>;

Start capturing audio.

Parameters

ParameterTypeDescription
handlersAudioSourceHandlersCallbacks for audio data and errors

Returns

Promise<void>

Throws

AudioPermissionError if microphone access is denied

Throws

AudioDeviceError if no audio device is found

Throws

AudioUnavailableError if audio capture is not supported


stop()

stop(): void;

Stop capturing audio and release all resources. Safe to call multiple times.

Returns

void


PermissionResolver

Platform-agnostic permission resolver.

Implementations handle platform-specific permission APIs:

  • Browser: navigator.permissions.query + getUserMedia
  • React Native: expo-av or react-native-permissions

Example

// Check before recording
const mic = await resolver.check('microphone');
if (mic.status === 'denied' && !mic.can_request) {
  showGoToSettingsMessage();
}

Methods

check()

check(permission): Promise<PermissionResult>;

Check current permission status WITHOUT prompting the user.

Parameters

ParameterType
permission"microphone"

Returns

Promise<PermissionResult>


request()

request(permission): Promise<PermissionResult>;

Request permission from the user (may show a system prompt). On platforms where status is already 'granted', this is a no-op.

Parameters

ParameterType
permission"microphone"

Returns

Promise<PermissionResult>


resolveApiKey()

function resolveApiKey(config): Promise<string>;

Resolves an ApiKeyConfig to a plain API key string.

Parameters

ParameterTypeDescription
configApiKeyConfigThe API key configuration

Returns

Promise<string>

The resolved API key string

Throws

If the function rejects or returns a non-string value