JavaScript SDK
The PerfVoice SDK is a single JavaScript file that handles the entire voice agent integration — WebSocket connection, microphone capture, audio encoding, playback, interruptions, and keepalive. No dependencies, no build step.Installation
Add the SDK via a script tag:PerfVoice class globally (or as a UMD/CommonJS module).
Quick Start
Constructor
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
apiKey | string | Yes | — | Your project API key (pk_live_...) |
agentId | string | Yes | — | Voice agent ID from the dashboard |
wsUrl | string | No | wss://api.withperf.pro/v1/voice/conversation | WebSocket endpoint URL |
Methods
voice.start()
Start a voice conversation. Requests microphone permission, opens a WebSocket connection, and begins streaming audio.
Promise<void> that resolves when the connection is established and the agent is ready. Rejects if microphone access is denied or the WebSocket connection fails.
Note: Browsers require a user gesture (click/tap) before allowing microphone access. Always call start() from a button click handler.
voice.stop()
End the conversation immediately. Stops all audio playback, releases the microphone, and closes the WebSocket.
Events
Subscribe to events withvoice.on(event, callback) and unsubscribe with voice.off(event, callback).
transcript
Fired when a transcript is available for either the agent or the user.
| Argument | Type | Description |
|---|---|---|
role | 'agent' | 'user' | Who said it |
text | string | The transcript text |
connected
Fired when the voice session is established and the agent is ready.
| Argument | Type | Description |
|---|---|---|
conversationId | string | Unique session identifier |
disconnected
Fired when the WebSocket connection closes (either by calling stop() or due to a server-side close).
| Argument | Type | Description |
|---|---|---|
code | number | WebSocket close code |
reason | string | Close reason (may be empty) |
status
Fired whenever the connection status changes.
| Argument | Type | Description |
|---|---|---|
status | 'disconnected' | 'connecting' | 'connected' | Current state |
error
Fired when an error occurs (microphone denied, WebSocket failure, etc.).
| Argument | Type | Description |
|---|---|---|
err | Error | The error object |
interruption
Fired when the user interrupts the agent (starts speaking while the agent is talking). The SDK automatically stops agent audio playback.
message
Fired for any unhandled message type from the server. Useful for debugging.
Properties
| Property | Type | Description |
|---|---|---|
voice.status | string | Current status: 'disconnected', 'connecting', or 'connected' |
voice.conversationId | string | null | Current session ID (null when disconnected) |
Full Example
A complete example with status indicator, transcript display, and error handling:React Integration
Error Handling
| Error | Cause | Resolution |
|---|---|---|
NotAllowedError | Microphone permission denied | Prompt user to allow microphone in browser settings |
WebSocket connection failed | Network issue or invalid credentials | Check API key and network connectivity |
Already connecting / Already connected | start() called while already active | Check voice.status before calling start() |
Browser Support
The SDK requires:navigator.mediaDevices.getUserMedia(microphone access)AudioContext(audio playback)WebSocket(real-time communication)
Related
- Voice Agents Overview — Architecture and features
- WebSocket Protocol — Raw WebSocket integration
- Python Integration — Server-side Python integration