TypeScript SDK
TypeScript SDK
Official TypeScript/JavaScript SDK for ProsodyAI
TypeScript SDK
The official @prosody/sdk package provides a type-safe client for the ProsodyAI API.
Installation
npm install @prosody/sdkpnpm add @prosody/sdkyarn add @prosody/sdkbun add @prosody/sdkBasic Usage
import { Prosody } from '@prosody/sdk';
// Initialize the client
const client = new Prosody({
apiKey: process.env.PROSODY_API_KEY,
});
// Analyze audio
const result = await client.analyze({
audio: audioBuffer,
vertical: 'contact_center',
});
console.log(result.emotion); // "satisfied"
console.log(result.valence); // 0.7
console.log(result.arousal); // 0.4Features
Single Utterance Analysis
Analyze a single audio segment and get emotion predictions:
const result = await client.analyze({
audio: audioBuffer, // Buffer, Blob, or base64 string
vertical: 'healthcare', // Target vertical
features: ['emotion', 'prosody', 'vad'],
});Real-Time Streaming
Process audio in real-time with WebSocket streaming:
const stream = client.stream({
vertical: 'sales',
sessionId: 'call-uuid',
onEmotion: (data) => console.log(data),
onPrediction: (data) => console.log(data),
});
stream.send(audioChunk);
await stream.close();Conversation Tracking
Track multi-turn conversations with forward prediction:
const session = client.createSession({
vertical: 'contact_center',
sessionId: 'call-12345',
});
// Add utterances as they occur
await session.addUtterance({ audio: chunk1, speaker: 'customer' });
await session.addUtterance({ audio: chunk2, speaker: 'agent' });
// Get conversation-level predictions
const predictions = session.getPredictions();
console.log(predictions.escalationRisk); // 0.23
console.log(predictions.predictedCsat); // 4.1
console.log(predictions.recommendedTone); // "empathetic"Webhooks
Configure webhooks for async processing:
await client.webhooks.create({
url: 'https://your-app.com/webhook',
events: ['analysis.complete', 'session.end'],
secret: 'your-webhook-secret',
});Configuration Options
const client = new Prosody({
// Required
apiKey: 'psk_...',
// Optional
baseUrl: 'https://api.prosody.ai', // Custom API endpoint
timeout: 30000, // Request timeout (ms)
retries: 3, // Retry failed requests
vertical: 'contact_center', // Default vertical
// Streaming options
streaming: {
reconnect: true, // Auto-reconnect on disconnect
bufferSize: 4096, // Audio buffer size
},
});Error Handling
import { Prosody, ProsodyError, RateLimitError } from '@prosody/sdk';
try {
const result = await client.analyze({ audio });
} catch (error) {
if (error instanceof RateLimitError) {
console.log(`Rate limited. Retry after: ${error.retryAfter}s`);
} else if (error instanceof ProsodyError) {
console.log(`API error: ${error.message} (${error.code})`);
}
}TypeScript Support
The SDK is written in TypeScript and provides full type definitions:
import type {
AnalyzeRequest,
AnalyzeResponse,
Emotion,
Vertical,
VADScore,
ConversationPrediction,
} from '@prosody/sdk';See the Types Reference for complete type definitions.