> ## Documentation Index
> Fetch the complete documentation index at: https://docs.praison.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Voice Call API

> Twilio voice integration with OpenAI Realtime API

# Voice Call API

The Voice Call API provides Twilio voice integration with OpenAI's Realtime API for voice-based AI interactions.

## Base URL

```
http://localhost:8090
```

## Endpoints

### Status Page

Check if the server is running.

<ParamField path="GET" type="/status">
  Returns HTML status page
</ParamField>

**Response**

```html theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
<html>
  <head><title>Praison AI Call Server</title></head>
  <body>
    <h1>Praison AI Call Server is running!</h1>
  </body>
</html>
```

***

### Incoming Call Handler

Handle incoming Twilio calls and return TwiML response.

<ParamField path="GET/POST" type="/">
  Twilio webhook for incoming calls
</ParamField>

**Response (TwiML)**

```xml theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say></Say>
  <Pause length="1"/>
  <Connect>
    <Stream url="wss://your-host/media-stream"/>
  </Connect>
</Response>
```

***

### Media Stream WebSocket

WebSocket endpoint for real-time audio streaming between Twilio and OpenAI.

<ParamField path="WebSocket" type="/media-stream">
  WebSocket for audio streaming
</ParamField>

**Connection Flow**

1. Twilio connects to WebSocket
2. Server connects to OpenAI Realtime API
3. Audio streams bidirectionally:
   * Twilio → Server → OpenAI (user speech)
   * OpenAI → Server → Twilio (AI response)

**Twilio Events**

| Event   | Description      |
| ------- | ---------------- |
| `start` | Stream started   |
| `media` | Audio data chunk |
| `stop`  | Stream ended     |

**OpenAI Events**

| Event                               | Description             |
| ----------------------------------- | ----------------------- |
| `session.created`                   | Session initialized     |
| `session.updated`                   | Session config updated  |
| `input_audio_buffer.speech_started` | User started speaking   |
| `input_audio_buffer.speech_stopped` | User stopped speaking   |
| `response.audio.delta`              | AI audio response chunk |
| `response.done`                     | AI response complete    |

## Configuration

### Environment Variables

| Variable           | Required | Default | Description                         |
| ------------------ | -------- | ------- | ----------------------------------- |
| `OPENAI_API_KEY`   | Yes      | -       | OpenAI API key with Realtime access |
| `PORT`             | No       | `8090`  | Server port                         |
| `NGROK_AUTH_TOKEN` | No       | -       | ngrok token for public URL          |
| `PUBLIC`           | No       | `false` | Enable public URL via ngrok         |

### Custom Tools

Create a `tools.py` file in the working directory:

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# tools.py
async def get_weather(location: str) -> dict:
    """Get weather for a location."""
    return {"location": location, "temp": "72°F", "condition": "sunny"}

get_weather.definition = {
    "name": "get_weather",
    "description": "Get current weather for a location",
    "parameters": {
        "type": "object",
        "properties": {
            "location": {"type": "string", "description": "City name"}
        },
        "required": ["location"]
    }
}

tools = [(get_weather.definition, get_weather)]
```

## Usage Example

### Start Server

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# Basic start
python -m praisonai.api.call

# With public URL
python -m praisonai.api.call --public

# Custom port
python -m praisonai.api.call --port 9000
```

### Twilio Configuration

1. Get your public URL (ngrok or deployed)
2. In Twilio Console:
   * Go to Phone Numbers → Your Number
   * Set Voice webhook to: `https://your-url.ngrok.io/`
   * Method: POST

### Python Usage

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonai.api.call import run_server

# Start the server
run_server(port=8090, use_public=True)
```

## Session Configuration

The server sends this session configuration to OpenAI:

```json theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
{
  "type": "session.update",
  "session": {
    "turn_detection": {
      "type": "server_vad",
      "threshold": 0.5,
      "prefix_padding_ms": 300,
      "silence_duration_ms": 200
    },
    "input_audio_format": "g711_ulaw",
    "output_audio_format": "g711_ulaw",
    "voice": "alloy",
    "tools": [],
    "tool_choice": "auto",
    "modalities": ["text", "audio"],
    "temperature": 0.8
  }
}
```

## Related

* [CLI](/docs/cli/cli) - Command-line interface
* [Agent](/docs/sdk/praisonaiagents/agent/agent) - Agent configuration
