HIPAA-eligible DIY Twilio alternative
For more context, see the blog release post: https://vectorly.app/blog/open-telephony-stack
This repository contains the complete infrastructure for building a HIPAA-eligible phone system using Asterisk and AWS Chime SDK SIP Media Application.
This is a production-ready alternative to Twilio that gives you full control over your telephony infrastructure while maintaining HIPAA eligibility. Includes a sample voice agent server for integrating with AI voice services (e.g., OpenAI Realtime API).
Why this matters
Twilio has limitations for healthcare applications:
- Limited control over media routing
- Complex HIPAA BAA requirements
- $2,000/mo charge for HIPAA-compliant services
- Media Streams only available on select plans
This solution provides:
- HIPAA eligible solution - You control all infrastructure
- Complete control - Own your PBX, customize everything
- Production-ready - Battle-tested in real healthcare applications
- Scalable - Handles concurrent calls with ease
What this is
A complete and secure telephony system built to handle both inbound and outbound calls:
- Receives calls via AWS Chime Voice Connector (you get a real phone number)
- Terminates SIP/TLS on Asterisk running in Docker
- Bridges the audio via RTP to a WebSocket connection
- Streams base64 μ-law audio to your AI voice server
- Twilio-like API (the WebSocket interface is modeled after Twilio's Media Streams API)
You bring your own AI. This just handles the phone infrastructure.
Who this is for
Use case examples:
- Building voice AI in healthcare and need HIPAA compliance without Twilio's BAA costs
- Customizing call handling in ways Twilio doesn't allow
- Wanting full control over your telephony stack
- Learning how telephony infrastructure works and building a VoIP stack from scratch
Consider alternatives if:
- You just need basic voice for a side project (Twilio is easier)
- You don't want to manage infrastructure
- You don't have any special compliance needs
Infrastructure requires time and maintenance. That's the trade-off.
Architecture
The system routes calls from AWS Chime Voice Connector through an Asterisk PBX server, which bridges RTP audio streams to a FastAPI shim server. The shim server converts the audio to WebSocket events compatible with Twilio's Media Streams API, allowing seamless integration with your voice AI services.

Port reference
| Service |
Port |
Protocol |
Description |
| Asterisk SIP |
5061 |
TCP/TLS |
SIP signaling with AWS Chime |
| Asterisk ARI |
8088 |
HTTP |
Asterisk REST Interface (localhost only) |
| Shim server |
8080 |
HTTP |
FastAPI server, health endpoints |
| RTP media |
10000-10299 |
UDP |
Audio streams to/from Asterisk |
Call flow
Here's what happens when someone calls your number:
- Caller dials your AWS Chime phone number
- Chime sends SIP INVITE to your Asterisk server (TLS:5061)
- Asterisk matches the call in
extensions.conf
Answer()
Stasis(voice-agent)
- ARI sends
StasisStart event to shim server via WebSocket
- Shim server:
- Opens WebSocket to your voice server
- Creates ARI mixing bridge
- Adds PSTN channel to bridge
- Allocates UDP port for RTP (10000-10299); each live call gets its own port
- Creates
ExternalMedia channel pointing to that port
- Adds
ExternalMedia channel to bridge
- Audio flows: PSTN ↔ Bridge ↔
ExternalMedia ↔ Shim (RTP) ↔ Voice Server (WSS)
- Caller hangs up (or AI ends call via a tool call)
- ARI sends
ChannelHangupRequest / ChannelDestroyed
- Shim cleans up: closes WebSocket, deletes bridge, releases port
Environment and dependencies with uv
This project uses uv to manage Python environments and pinned dependencies.
Setup
# install uv
pip install uv
uv --version
# create a venv in .venv
uv venv --prompt open-telephony-stack
# activate env
source .venv/bin/activate # or use `uv run` without activating
# install exactly what lockfile says (if uv.lock exists)
uv sync
# dev install (editable mode, so `import _` from src/ works live)
uv pip install -e .
Files
pyproject.toml: Project metadata + declared dependencies.
uv.lock: Fully pinned versions for reproducible installs (generated by uv lock). Do not manually edit.
src/: Source package (open-telephony-stack).
Common commands
# run shim server in env
uv run uvicorn servers.asterisk_shim_server:app --reload
Updating dependencies
Add new library items to dependencies in pyproject.toml, and then:
# 1. edit pyproject.toml
uv lock
# 2. regenerate + sync
uv sync
Or, if you just want uv.lock to use the latest version of dependencies (including submodules):
uv lock --upgrade --no-cache
Docker builds also install from pyproject.toml for consistency.
Architecture overview
Signal flow
- Inbound Call: PSTN → AWS Chime SIP → Asterisk (via SIP/TLS)
- ARI Events: Asterisk notifies Shim Server via ARI WebSocket
- Bridge Setup: Shim creates mixing bridge, ExternalMedia channel
- RTP Streaming: Asterisk ↔ Shim Server (μ-law RTP on UDP)
- WebSocket Bridge: Shim ↔ Voice Server (WSS to internal ALB)
- AI Processing: Voice Server ↔ AI Voice Agent Server
Key components
The core telephony engine running in Docker:
- SIP/TLS termination with AWS Chime SDK
- Dialplan routing for inbound/outbound calls
- ARI (Asterisk REST Interface) for programmatic control
- ExternalMedia channels for RTP bridging
- Auto-renewing Let's Encrypt TLS certificates
Config Files:
pjsip.conf - SIP trunk configuration for AWS Chime
extensions.conf - Call routing dialplan
ari.conf - REST API credentials
http.conf - HTTP server for ARI
modules.conf - Loaded Asterisk modules
FastAPI application that bridges Asterisk to your voice AI service:
- ARI Supervisor - Manages WebSocket connection to Asterisk
- CallSession - Per-call state machine handling RTP ↔ WSS
- RTP Pacer - Maintains perfect 20ms cadence for audio
- Health endpoints - Monitor active calls and system status
The heart of the real-time audio processing:
- RTP socket management - Allocates ports from configurable range
- ExternalMedia lifecycle - Creates/destroys ARI channels
- Bidirectional audio - Concurrent RTP → WSS and WSS → RTP loops
- Buffer management - Queues audio for smooth 20ms frame delivery
- Mark/ACK handling - Supports interruption via Twilio-compatible events
Manages all active calls:
- WebSocket lifecycle - Auto-reconnect on disconnection
- StasisStart events - Spawns CallSession for new calls
- Cleanup orchestration - Graceful shutdown on hangup
- Health monitoring - Tracks active sessions
An example FastAPI application that bridges the shim server to OpenAI Realtime API:
- WebSocket endpoint - Receives Twilio-compatible media streams from shim server
- OpenAI Realtime API integration - Connects to OpenAI for voice-to-voice AI conversations
- Call state management - Tracks call metadata, transcripts, and session state
- Tool call handling - Supports agent tools (e.g.,
end_call) for call control
- Interruption handling - Detects caller speech and truncates assistant audio
- Transcript logging - Captures conversation transcripts for both user and assistant
Quick start
Prerequisites
- AWS Account with Chime SDK SIP Media Application
- EC2 Instance (t3.medium or larger, Amazon Linux 2023)
- Elastic IP assigned to EC2
- Domain name pointing to the Elastic IP
- AI Voice Agent Server (external service that handles voice AI processing)
- Docker and Docker Compose installed
1. Set up AWS Chime SIP trunk
- Create a SIP Media Application in AWS Chime console
- Provision a phone number and associate it with the SIP Media Application
- Configure outbound calling hostnames (your Asterisk server domain)
- Note your Chime outbound hostname (e.g.,
+1XXXXXXXXXX.voiceconnector.chime.aws)
2. Configure DNS
Before setting up TLS certificates, you need to configure DNS so that AWS Chime can resolve your Asterisk server's hostname. Create an A record pointing your SIP subdomain to your EC2 instance's Elastic IP:
| Record Type |
Name |
Value |
TTL |
| A |
sip.yourdomain.com |
Your Elastic IP (e.g., 54.123.45.67) |
300 (or default) |
This DNS record must be in place before:
- Requesting Let's Encrypt certificates (Certbot validates domain ownership)
- Configuring AWS Chime Voice Connector termination (Chime needs to resolve the hostname)
- Setting
external_signaling_address in pjsip.conf (must match the DNS name)
After creating the record, wait for DNS propagation (usually a few minutes, but can take up to 48 hours depending on TTL). You can verify with:
dig sip.yourdomain.com
# or
nslookup sip.yourdomain.com
3. Install Asterisk server
cd deployment/asterisk-server
# Install Docker & Docker Compose (if needed)
sudo yum -y install docker
sudo systemctl start docker && sudo systemctl enable docker
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# Start Asterisk
docker-compose up -d
# View logs
docker logs -f asterisk-server
# Access Asterisk CLI
docker exec -it asterisk-server asterisk -rvvvvv
4. Configure TLS certificates
AWS Chime requires TLS for SIP. Use Let's Encrypt:
# Install certbot
sudo yum install -y certbot
# Get certificate (ensure port 80 is open)
sudo certbot certonly --standalone \
--preferred-challenges http \
-d sip.yourdomain.com \
--agree-tos -m your@email.com
# Enable auto-renewal
sudo systemctl enable --now certbot-renew.timer
# Set up deploy hook to reload Asterisk
sudo mkdir -p /etc/letsencrypt/renewal-hooks/deploy
sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-asterisk.sh > /dev/null <<'EOF'
#!/bin/sh
docker exec asterisk-server asterisk -rx "core reload"
EOF
sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-asterisk.sh
5. Configure Asterisk
Edit deployment/asterisk-server/asterisk-config/pjsip.conf:
[chime-out]
type=endpoint
context=from-pstn
disallow=all
allow=ulaw
aors=chime-out
outbound_auth=chime-out
media_encryption=sdes
; ... (see full config in file)
[chime-in]
type=endpoint
context=from-pstn
disallow=all
allow=ulaw
; ... (see full config in file)
Edit deployment/asterisk-server/asterisk-config/extensions.conf:
[from-pstn]
exten => s,1,Answer()
same => n,Stasis(voice-agent)
same => n,Hangup()
6. Deploy shim server
# Set environment variables
cp .env.example .env
# Edit .env with your values:
# - ARI_BASE=http://127.0.0.1:8088/ari
# - ARI_USER=ariuser
# - ARI_PASS=your_password
# - ECS_MEDIA_WSS_URL=wss://your-voice-server.com/voice/voice
# - EXTERNAL_MEDIA_HOST=127.0.0.1
# - RTP_PORT_START=10000
# - RTP_PORT_END=10299
# Build Docker image
docker build -t asterisk-shim -f deployment/shim-server/Dockerfile .
# Run shim server (host networking for RTP)
docker run -d --env-file .env --network host --name asterisk-shim asterisk-shim
# View logs
docker logs -f asterisk-shim
7. Test the system
Call your AWS Chime phone number. You should see:
- Asterisk logs: Inbound SIP INVITE