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

# Twilio

> Connect your Twilio phone numbers to Rapida AI for inbound and outbound voice AI conversations.

Twilio is a cloud communications platform that provides programmable voice, messaging, and video services. Rapida integrates with Twilio to enable AI-powered phone conversations — both inbound (customers call your Twilio number) and outbound (your assistant calls customers).

> [!NOTE]
>
> Twilio uses **WebSocket media streams** for real-time audio. When a call connects, Twilio opens a WebSocket to Rapida carrying μ-law encoded audio at 8kHz. Rapida handles all codec conversion and resampling transparently.

## How It Works

```mermaid theme={null}
sequenceDiagram
    participant Caller
    participant Twilio as Twilio Cloud
    participant API as Rapida Webhook API
    participant WS as Rapida WebSocket
    participant AI as AI Pipeline (STT → LLM → TTS)

    Caller->>Twilio: Dials your Twilio number
    Twilio->>API: POST /v1/talk/twilio/call/{assistantId}
    API->>API: Create conversation + save call context
    API-->>Twilio: TwiML (Connect → Stream)

    rect rgb(230, 255, 230)
    Note over Twilio,AI: Media Streaming
    Twilio->>WS: WebSocket connect (μ-law audio)
    WS->>AI: Audio → STT → LLM → TTS
    AI->>WS: TTS audio
    WS->>Twilio: μ-law audio
    Twilio->>Caller: AI speaks
    end

    Caller->>Twilio: Hangup
    Twilio->>API: Status callback
```

***

## Prerequisites

> [card] Twilio Account
>
> An active Twilio account with at least one phone number

> [card] Rapida Account
>
> An active Rapida account with a configured voice assistant

You will need your **Twilio Account SID** and **Auth Token**, which you can find on the [Twilio Console dashboard](https://console.twilio.com/).

***

## Step 1: Set Up Provider Credentials

First, store your Twilio API credentials in Rapida so the platform can authenticate with Twilio for outbound calls and webhook validation.

**1. Navigate to External Integrations**

Go to **Integration > Tools** in the Rapida dashboard. You will see a grid of available external integrations.

![External Integrations Page](https://mintcdn.com/rapidaai/iFtJHOhZE0HHFT_Q/images/integration/external/external.png?fit=max\&auto=format\&n=iFtJHOhZE0HHFT_Q\&q=85\&s=99328dbb5a850dcb54adad31362a0aef)

**2. Select Twilio**

Find the **Twilio** card and click **"Setup Credential"**.

**3. Enter Your Twilio Credentials**

![Twilio Credentials](https://mintcdn.com/rapidaai/WawFKwvQr8qexZDO/images/integration/external/twilio_credentials.png?fit=max\&auto=format\&n=WawFKwvQr8qexZDO\&q=85\&s=7704f1c5968a9fa7ff5452184be10996)

A modal will appear. Fill in the following fields:

| Field | Description | Where to Find |
| - | - | - |
| **Key Name** | A friendly name for this credential (e.g., "Production Twilio") | Your choice |
| **Account Token** | Your Twilio Auth Token | [Twilio Console](https://console.twilio.com/) → Auth Token |
| **Account SID** | Your Twilio Account SID | [Twilio Console](https://console.twilio.com/) → Account SID |

Click **"Configure"** to save.

**4. Verify Connection**

After saving, the Twilio card should display **"Connected"**. Click on it to see credential details, last updated time, and management options.

***

## Step 2: Configure Phone Deployment

With credentials saved, configure your assistant's phone deployment to use Twilio.

**1. Open Your Assistant**

Navigate to **Assistants** and select the assistant you want to deploy via phone.

**2. Go to Phone Deployment**

Click **"Deploy"** → **"Phone"** to open the phone deployment configuration page.

**3. Select Twilio as Telephony Provider**

In the **Telephony** section:

1. Select **Twilio** from the telephony provider dropdown
2. Choose the Twilio credential you created in Step 1 from the **Credential** dropdown
3. Enter your **Phone** number — this is the Twilio phone number that will receive inbound calls and serve as the caller ID for outbound calls (e.g., `+15551234567`)

**4. Configure Experience Settings**

Set up the conversation experience:

| Setting | Description | Default |
| - | - | - |
| **Greeting** | The first message the AI speaks when answering | *(optional)* |
| **Error Message** | Message spoken when an error occurs | *(optional)* |
| **Idle Timeout** | Seconds before prompting an idle caller | `30` |
| **Idle Message** | Message spoken when caller is idle | `"Are you there?"` |
| **Idle Timeout Retries** | How many times to retry before ending call | `2` |
| **Max Call Duration** | Maximum call length in seconds | `300` |

**5. Configure Audio Providers**

Select your **Speech-to-Text** (STT) and **Text-to-Speech** (TTS) providers. These determine how audio is transcribed and synthesized during the call.

> [!NOTE]
>
> For Twilio calls, audio arrives in **μ-law 8kHz** format. Rapida automatically resamples to 16kHz for the STT provider and converts TTS output back to μ-law for Twilio.

**6. Save Deployment**

Click **"Deploy"** to save. Your assistant is now ready to handle phone calls via Twilio.

***

## Step 3: Configure Your Twilio Phone Number

Point your Twilio phone number's webhook to Rapida so incoming calls are routed to your AI assistant.

**1. Open Twilio Console**

Go to [Twilio Console → Phone Numbers → Active Numbers](https://console.twilio.com/us1/develop/phone-numbers/manage/incoming) and select your phone number.

**2. Set Voice Webhook**

Under **Voice Configuration**, set:

| Field | Value |
| - | - |
| **A call comes in** | Webhook |
| **URL** | `https://websocket-01.in.rapida.ai/v1/talk/twilio/call/{your-assistant-id}?x-api-key={your-api-key}` |
| **HTTP Method** | `POST` |

Replace `{your-assistant-id}` with your Rapida assistant ID.

**3. Set Status Callback (Optional)**

To receive call lifecycle events (ringing, answered, completed):

| Field | Value |
| - | - |
| **Status Callback URL** | `https://websocket-01.in.rapida.ai/v1/talk/twilio/ctx/{contextId}/event?x-api-key={your-api-key}` |
| **Status Callback Events** | `initiated`, `ringing`, `answered`, `completed` |

> [!NOTE]
>
> The status callback URL uses a `contextId` returned during the call setup. Twilio sends events to this URL automatically when configured in TwiML.

**4. Save**

Click **Save** in the Twilio console. Your phone number is now connected to Rapida.

***

## Making Outbound Calls

Once your phone deployment is configured, you can initiate outbound calls using the Rapida API or SDKs.

> [tab] Python
>
> ```python theme={null}
> from rapida import Rapida
>
> client = Rapida(api_key="rpd-xxx-your-key")
>
> call = client.calls.create(
>     assistant_id=123456789,
>     to_number="+15551234567",
>     from_number="+15559876543",  # optional, uses deployment default
>     metadata={"campaign": "follow-up"},
> )
>
> print(f"Call queued: conversation_id={call.conversation.id}")
> ```

> [tab] Node.js
>
> ```typescript theme={null}
> import { Rapida } from 'rapida';
>
> const client = new Rapida({ apiKey: 'rpd-xxx-your-key' });
>
> const call = await client.calls.create({
>   assistantId: 123456789,
>   toNumber: '+15551234567',
>   fromNumber: '+15559876543',
>   metadata: { campaign: 'follow-up' },
> });
>
> console.log(`Call queued: ${call.conversation.id}`);
> ```

> [tab] cURL
>
> ```bash theme={null}
> curl -X POST https://api.rapida.ai/v1/talk/call \
>   -H "Authorization: Bearer rpd-xxx-your-key" \
>   -H "Content-Type: application/json" \
>   -d '{
>     "assistant": { "assistant_id": 123456789 },
>     "to_number": "+15551234567",
>     "from_number": "+15559876543",
>     "metadata": { "campaign": "follow-up" }
>   }'
> ```

***

## Features

| Feature | Description |
| - | - |
| **Inbound Calls** | Customers call your Twilio number and speak with your AI assistant |
| **Outbound Calls** | Initiate calls programmatically via SDK or API |
| **Real-time Streaming** | Bidirectional audio via Twilio Media Streams (WebSocket) |
| **Call Recording** | Automatic conversation capture for review and compliance |
| **Status Callbacks** | Receive call lifecycle events (ringing, answered, completed) |
| **WhatsApp** | Send and receive WhatsApp messages via Twilio |
| **Global Numbers** | Use Twilio phone numbers from 100+ countries |

***

## Troubleshooting

> [accordion] Calls go to voicemail instead of Rapida
>
> * Verify the webhook URL in your Twilio phone number configuration
> * Ensure the URL uses `https://` and the correct assistant ID
> * Check that your Rapida API key is valid

> [accordion] One-way audio or no audio
>
> * Verify your Twilio credential (Account SID + Auth Token) is correct in Rapida
> * Check that your STT and TTS providers are configured in the phone deployment
> * Ensure the assistant has an active version with a valid LLM provider

> [accordion] Outbound calls fail
>
> * Verify the Twilio credential is connected in **Integration > Tools**
> * Ensure the `from_number` is a valid Twilio phone number in your account
> * Check that the phone deployment is configured and saved

> [accordion] Status callbacks not received
>
> * Ensure the status callback URL is configured in Twilio console
> * Verify the URL format: `https://websocket-01.in.rapida.ai/v1/talk/twilio/ctx/{contextId}/event?x-api-key={your-api-key}`
> * Check Twilio's [Debugger](https://console.twilio.com/us1/monitor/logs/debugger) for webhook errors

***

## Related Resources

> [card] [Create an Assistant](https://doc.rapida.ai/assistants/create-assistant.md)
>
> Build your voice AI assistant

> [card] [Phone Deployment](https://doc.rapida.ai/voice-deployment-options/phone.md)
>
> Overview of phone deployment options

> [card] [Outbound Call API](https://doc.rapida.ai/api-reference/call/create-call.md)
>
> API reference for creating calls

> [card] [Conversation Logs](https://doc.rapida.ai/activity/conversation-logs.md)
>
> View call history and transcripts

For more information on Twilio's platform, visit the [Twilio Developer Documentation](https://www.twilio.com/docs).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.