DEV Community

Domonique Luchin
Domonique Luchin

Posted on

How VAPI connects to Asterisk PJSIP for AI voice calls

I run six AI-powered businesses from a single Vultr VPS. Each business needs phone capabilities. Instead of paying $50+ per month for hosted voice solutions, I built my own system using Asterisk and VAPI.

Here's exactly how I connected VAPI to Asterisk PJSIP for AI voice calls.

The Problem with SaaS Voice Solutions

Most AI voice platforms want you to use their phone numbers and infrastructure. You pay per minute, per call, per feature. For one business, maybe that works. For six businesses making hundreds of calls monthly, those costs add up fast.

I needed control. I needed my own phone system that could handle AI agents without monthly subscriptions bleeding my profit margins.

My Setup Overview

  • Server: Vultr VPS (4GB RAM, 2 vCPU)
  • PBX: Asterisk 18 with PJSIP
  • AI Voice: VAPI agents
  • SIP Provider: Flowroute (you can use any)
  • Monthly Cost: $47 total (server + DID numbers + minutes)

This setup handles inbound and outbound AI calls for all six businesses.

Asterisk PJSIP Configuration

First, configure your PJSIP endpoints in /etc/asterisk/pjsip.conf:

[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0:5060

[vapi-endpoint]
type=endpoint
context=vapi-context
disallow=all
allow=ulaw
allow=alaw
auth=vapi-auth
aors=vapi-aor
direct_media=no
ice_support=yes
media_encryption=sdes

[vapi-auth]
type=auth
auth_type=userpass
username=vapi-user
password=your-secure-password

[vapi-aor]
type=aor
max_contacts=5
remove_existing=yes
Enter fullscreen mode Exit fullscreen mode

Dialplan for AI Call Routing

Your dialplan (/etc/asterisk/extensions.conf) routes calls to VAPI:

[vapi-context]
exten => _X.,1,NoOp(Incoming call for AI: ${CALLERID(num)})
same => n,Set(CHANNEL(hangup_handler_wipe)=vapi-cleanup,s,1)
same => n,Dial(PJSIP/vapi-endpoint,30)
same => n,Hangup()

[vapi-cleanup]
exten => s,1,NoOp(Call ended, cleanup complete)
Enter fullscreen mode Exit fullscreen mode

For outbound calls through your SIP provider:

[outbound-context]
exten => _1NXXNXXXXXX,1,NoOp(Outbound call to: ${EXTEN})
same => n,Dial(PJSIP/${EXTEN}@flowroute-trunk,60)
same => n,Hangup()
Enter fullscreen mode Exit fullscreen mode

VAPI Integration Code

VAPI needs to register with your Asterisk server as a SIP client. Here's the Python code I use to manage the connection:

import requests
import json

def create_vapi_phone_number():
    url = "https://api.vapi.ai/phone-number"

    payload = {
        "provider": "byom",
        "byomPhoneNumber": {
            "server": "your-asterisk-server.com",
            "username": "vapi-user",
            "password": "your-secure-password"
        },
        "assistantId": "your-assistant-id"
    }

    headers = {
        "Authorization": f"Bearer {VAPI_API_KEY}",
        "Content-Type": "application/json"
    }

    response = requests.post(url, json=payload, headers=headers)
    return response.json()

# Configure your AI assistant
def setup_vapi_assistant():
    url = "https://api.vapi.ai/assistant"

    payload = {
        "model": {
            "provider": "openai",
            "model": "gpt-4",
            "temperature": 0.1
        },
        "voice": {
            "provider": "eleven-labs",
            "voiceId": "your-voice-id"
        },
        "firstMessage": "Hello, how can I help you today?"
    }

    headers = {
        "Authorization": f"Bearer {VAPI_API_KEY}",
        "Content-Type": "application/json"
    }

    response = requests.post(url, json=payload, headers=headers)
    return response.json()
Enter fullscreen mode Exit fullscreen mode

Connection Flow

Here's how the pieces connect:

  1. Inbound Call: Customer dials your number → SIP provider → Asterisk → VAPI agent
  2. Outbound Call: VAPI agent → Asterisk → SIP provider → Customer
  3. Audio Processing: Real-time audio flows between caller and AI through PJSIP channels

Configuration Gotchas

Audio Codec Issues: VAPI works best with ulaw and alaw. Don't enable high-definition codecs unless you want choppy AI responses.

NAT Problems: If your Asterisk server is behind NAT, add these lines to pjsip.conf:

[global]
external_media_address=your-public-ip
external_signaling_address=your-public-ip
Enter fullscreen mode Exit fullscreen mode

Firewall Rules: Open UDP port 5060 for SIP signaling and UDP ports 10000-20000 for RTP audio.

Real Performance Numbers

My setup handles:

  • 12 concurrent AI calls without issues
  • Average call setup time: 2.3 seconds
  • Audio latency: 150-200ms (includes AI processing)
  • Monthly costs: $47 for unlimited calls within my limits

Compare that to hosted solutions charging $0.10+ per minute for AI calls.

Why This Matters

You control your phone infrastructure. No vendor lock-in. No surprise billing. No rate limits beyond your hardware capacity.

When one of my businesses needs a new phone number or different call routing, I make the change in my dialplan. Takes 5 minutes. No support tickets or billing adjustments.

Next Steps

Start with a basic Asterisk installation on a VPS. Get PJSIP working with a simple SIP provider first. Then add VAPI integration once your PBX handles regular calls properly.

You'll spend a weekend learning Asterisk configuration. But you'll save hundreds monthly and own your communication stack completely.

Want the complete configuration files? I've documented my entire setup at [your documentation link]. Stop paying monthly fees for infrastructure you can own.

Top comments (0)