OpenAI Realtime SIP Call Handler
This service connects incoming SIP calls (via Vobiz telephony) directly to OpenAI's Realtime API, enabling AI-powered voice conversations. When someone calls your Vobiz number, the AI answers and has a real-time conversation with the caller natively via SIP.
Architecture Flow
Flow Overview
- Caller dials your Vobiz number (e.g. +918046733659)
- Vobiz routes the call to OpenAI's SIP endpoint
- OpenAI sends a webhook notification to your service
- Your service accepts the call via the OpenAI API
- WebSocket connection established for audio streaming
- The AI converses with the caller in real-time
Prerequisites
Before starting, ensure you have the following credentials and environments setup:
- ✅OpenAI Account: You will need your API Key, Project ID, and Webhook Secret from the OpenAI Platform.
- ✅Vobiz Account: Ensure you have a SIP Trunk configured and a Phone number assigned.
- ✅Development Environment: Python 3.8+ and `ngrok` installed to handle webhooks locally.
Step 1: Vobiz Configuration
Before receiving calls, you'll need to create an Origination URI and point your Vobiz SIP Trunk directly to the OpenAI SIP Endpoint.
- Create the Origination URI: Navigate to Origination URIs (https://console.vobiz.ai/app/sip/in/uri).
Add a new URI setting the destination to:
proj_xyz123@sip.api.openai.com:5061. Note: It is critical to explicitly add port5061. - Create the Inbound Trunk: Navigate to Inbound Trunks (https://console.vobiz.ai/app/sip/in/trunks).
Create a new trunk and attach the Origination URI you just created. Ensure the trunk is associated with your active Vobiz phone number.
Step 2: Service Installation
Install Python Dependencies
# Create virtual environment
python -m venv venv
# Activate virtual environment
venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtConfigure Environment Variables
Copy .env.example to .env and update it with your actual credentials:
# OpenAI Configuration
OPENAI_API_KEY=sk-your-api-key-here
OPENAI_PROJECT_ID=proj_r9BlSVZT8fTiZqmL5BwIN4z8
OPENAI_WEBHOOK_SECRET=whsec-your-webhook-secret-here
OPENAI_PROMPT_ID=pmpt_69b443cf6f088190a704075d0ce92ae30555b4ac58111073
# Debug (set to false in production)
SKIP_SIGNATURE_VALIDATION=false
# Vobiz Configuration
VOBIZ_PHONE_NUMBER=+918046733659
# Service Configuration
SERVICE_PORT=8000
# AI Configuration
AI_INSTRUCTIONS=You are a helpful customer support agent
AI_VOICE=alloyReplace placeholder values with your actual credentials from the OpenAI platform.
Step 3: OpenAI Platform Configuration
- API Key: Create or copy it from OpenAI API Keys.
- Project ID: Copy your Project ID (
proj_xxxxx) from Organization Settings. - Webhook Secret: You'll generate this next.
Configure Webhook (CRITICAL)
OpenAI needs to know where to send incoming call HTTP notifications.
- Start your Python service to generate the ngrok url (see Step 4).
- Copy the ngrok URL displayed (e.g.,
https://abc123.ngrok.io). - Go to Webhooks Settings.
- Configure the Endpoint URL to
https://abc123.ngrok.io/webhook/incoming. - Set Event Type to
realtime.call.incoming. - Copy the webhook secret, update your
.envfile, and restart your python service.
Step 4: Running the Service
Local Testing (with ngrok)
Run the service using your Python environment:
python app.pyThe service will automatically start the ngrok tunnel, display the webhook endpoint for OpenAI configuration, and launch the server.
Production Deployment
For production, deploy to a server with a static public URL like Render, Railway, AWS EC2, or Google Cloud Run.
- HTTPS is enabled (required by OpenAI).
- Use a production WSGI server (e.g.,
gunicorn -w 4 -b 0.0.0.0:8000 app:app). - Ensure
SKIP_SIGNATURE_VALIDATION=false.
Step 5: Testing
Test Webhook Endpoint
- In the OpenAI Webhooks page, click "Send test event".
- Select event type:
realtime.call.incomingand click "Send". - Expected Result: Service logs should show "Received webhook", and OpenAI should show "Received HTTP 200".
Test Real Call
- Call your assigned Vobiz number.
- The AI should answer within 2-3 seconds and open a conversational loop.
Troubleshooting & FAQ
Webhook Returns 401 (Invalid Signature)
Cause: Webhook secret mismatch.
Verify OPENAI_WEBHOOK_SECRET in .env matches the OpenAI platform. Make sure there are no extra spaces or quotes. Finally, restart the service. Use SKIP_SIGNATURE_VALIDATION=true for debugging only.
Call Gets 500 Error from OpenAI
Cause: Webhook not configured or unreachable via the Ngrok Tunnel.
Verify the webhook is configured in OpenAI, ensure the ngrok URL is online and reachable, and test the webhook with the "Send test event" button to check the logs.
"No session found for call_id"
This is expected behavior for "Test Events", since OpenAI uses fake call IDs for testing. Perform a real phone call to test actual streaming functionality.
