Skip to main content

Python Integration

Connect to Perf Voice Agents from Python for server-side applications, IVR systems, telephony integrations, or testing.

Prerequisites

  • websocket-client — WebSocket client library
  • PyAudio — Cross-platform audio I/O (requires PortAudio system library)

Installing PortAudio

PyAudio requires the PortAudio system library:

Quick Start

How It Works

  1. Connect — Opens a WebSocket to wss://api.withperf.pro/v1/voice/conversation with your API key and agent ID
  2. Wait for init — The conversation_initiation_metadata message signals the pipeline is ready
  3. Stream audio — A background thread reads microphone PCM16 chunks, base64-encodes them, and sends via WebSocket
  4. Play responses — Agent audio arrives as base64 PCM16 and is written directly to the speaker stream
  5. Keepalive — Responds to ping messages with pong to keep the connection alive

Audio Format

Sending Pre-Recorded Audio

To send a WAV file instead of live microphone input:

Saving Transcripts

Connection Test

Quick test without audio — verify authentication and agent connectivity:

Error Handling