SDK Reference
This reference guide provides detailed information about all classes, methods, interfaces, and options available in the Klarisent STT SDK.
KlarisentSTT Class
The main class for interacting with the Klarisent Speech-to-Text service.
Constructor
constructor({
api_key,
connectionId,
debug = false,
pauseDuration = 0.6,
}: IKlarisentSTTConstructor)
Parameters
api_key
string
Required. Your Klarisent API key
connectionId
string
Optional. Custom identifier for the connection. If not provided, a UUID will be generated
debug
boolean
Optional. Enable debug logging. Default: false
pauseDuration
number
Optional. Pause duration for Voice Activity Detection in seconds. Minimum: 0.5. Default: 0.6
Methods
establishConnection
Establishes a WebSocket connection to the Klarisent server.
establishConnection({ replaceableObjects }: { replaceableObjects: [] }): Promise<{ status: number, message: string }>
Parameters:
replaceableObjects
: An array of objects (reserved for future use)
Returns:
Promise resolving to a connection status object
Example:
await stt.establishConnection({ replaceableObjects: [] });
sendBase64Audio
Sends base64-encoded audio for transcription through the REST API.
sendBase64Audio(payload: ISendBase64AudioPayload): Promise<ISendAudioStreamResponse>
Parameters:
payload
: Object containing audio data and configuration options
Returns:
Promise resolving to a transcription response object
Example:
const result = await stt.sendBase64Audio({
audio: base64String,
fileName: 'recording.flac',
language: 'en',
trigger: [TRIGGER.QuestionDetection]
});
sendAudioStream
Streams audio for real-time transcription through WebSockets.
sendAudioStream(audioStream: any): void
Parameters:
audioStream
: A readable stream of audio data
Returns:
void
Example:
stt.sendAudioStream(microphoneStream);
stop
Terminates the connection.
stop(): void
Returns:
void
Example:
stt.stop();
onTranscription
Registers a callback function for transcription events.
onTranscription(callback: (data: ITranscriptionCallBackPayload) => void): void
Parameters:
callback
: Function to be called when transcription is received
Example:
stt.onTranscription((data) => {
console.log('Transcription:', data.transcript);
});
onQuestion
Registers a callback function for question detection events.
onQuestion(callback: (data: IQuestionCallBackPayload) => void): void
Parameters:
callback
: Function to be called when questions are detected
Example:
stt.onQuestion((data) => {
console.log('Questions:', data.questions);
});
onError
Registers a callback function for error events.
onError(callback: (error: any) => void): void
Parameters:
callback
: Function to be called when an error occurs
Example:
stt.onError((error) => {
console.error('Error:', error);
});
onStreamEnd
Registers a callback function for stream end events.
onStreamEnd(callback: () => void): void
Parameters:
callback
: Function to be called when the audio stream ends
Example:
stt.onStreamEnd(() => {
console.log('Stream ended');
});
setDebugMode
Enables or disables debug logging.
setDebugMode(debug: boolean): void
Parameters:
debug
: Boolean indicating whether to enable debug logging
Example:
stt.setDebugMode(true);
Properties
isConnected
Indicates if the WebSocket connection is currently established.
isConnected: boolean
Example:
console.log('Connection status:', stt.isConnected);
Interfaces
IKlarisentSTTConstructor
Parameters for initializing the KlarisentSTT class.
interface IKlarisentSTTConstructor {
api_key: string;
connectionId?: string;
debug?: boolean;
pauseDuration?: number;
}
ISendBase64AudioPayload
Parameters for the sendBase64Audio method.
interface ISendBase64AudioPayload {
audio: string;
fileName?: string;
language: "en" | "hi";
trigger?: TRIGGER[];
}
ITranscriptionCallBackPayload
Data received in transcription events.
interface ITranscriptionCallBackPayload {
transcript: string;
questions: string[];
}
IQuestionCallBackPayload
Data received in question detection events.
interface IQuestionCallBackPayload {
transcript: string;
questions: string;
}
ISendAudioStreamResponse
Response from the sendBase64Audio method.
interface ISendAudioStreamResponse {
status: number;
message: string;
data: ISendAudioStreamResponseData;
}
ISendAudioStreamResponseData
Data included in the API response.
interface ISendAudioStreamResponseData {
audioSizeKB: number;
duration: number;
transcription: string;
transcriptionTime: number;
trigger?: {
[TRIGGER.QuestionDetection]?: string[];
[TRIGGER.SentimentAnalysis]?: {
sentiment: string;
text: string;
};
[TRIGGER.SentimentAnalysisAdvanced]?: {
sentiment: string;
text: string;
};
};
}
Enums
TRIGGER
Enumeration of available analysis triggers.
enum TRIGGER {
QuestionDetection = "6746edc31c1965a1ee9bc2d4",
SentimentAnalysis = "674882924ed3a11958beaab7",
SentimentAnalysisAdvanced = "67605b1a4fcedccfa3e2d494"
}
Constants
BASE_URL
The base URL for API requests.
const BASE_URL = "https://api.klarisent.com";
Error Codes
200
Success
400
Bad Request - Invalid parameters
401
Unauthorized - Invalid API key
403
Forbidden - Account restrictions
413
Payload Too Large - Audio file too big
429
Too Many Requests - Rate limit exceeded
500
Internal Server Error
Best Practices
Audio Formats
For optimal recognition:
Sample Rate: 16000 Hz
Channels: 1 (mono)
Format: FLAC or WAV
Bit Depth: 16-bit
Connection Management
Always check
isConnected
before sending audio streamsHandle reconnection logic for
Last updated