# Twilio Integration

> Configure Twilio for voice communication in Menace Voice

## What you need

- A [Twilio account](https://www.twilio.com/try-twilio). Trial accounts can
  test with restrictions; upgrade before production use.
- One Twilio number with **Voice** capability.
- The **Account SID** and **Auth Token** for the Twilio project that owns the
  number.

## Estimated price

Twilio lists US local numbers from **$1.15 per month**, plus inbound/outbound
usage. Country, number type, capabilities, and regulatory requirements change
the price. Confirm the monthly fee in Twilio Console before buying and check
the [current Twilio Voice pricing](https://www.twilio.com/en-us/voice/pricing/us).

## Set up Twilio step by step

### 1. Buy a voice-capable number

1. Sign in to [Twilio Console](https://console.twilio.com/).
2. Open **Products & Services → Numbers & Senders → Phone Numbers**.
3. Click **Set up a new phone number**. In the legacy Console this is
   **Develop → Phone Numbers → Manage → Buy a number**.
4. Choose the country and select **Voice** under capabilities.
5. Review the number's monthly fee and any address or identity requirements.
6. Click **Buy** and complete any required compliance registration.

Twilio documents this flow in [Phone Numbers](https://www.twilio.com/docs/numbers-and-senders/phone-number-senders)
and [How to buy a number](https://help.twilio.com/articles/223135247-How-to-Search-for%20-and-Buy-a-Twilio-Phone-Number-from-Console).

### 2. Copy the credentials Menace Voice requests

1. Return to the Console home page and find **Account Info**.
2. Copy the **Account SID**. It starts with `AC`.
3. Reveal and copy the **Auth Token**.

<Warning>
  Paste the Auth Token only into the **Auth Token** field. A Twilio API Key SID
  is a different credential and is not accepted by this integration.
</Warning>

### 3. Connect Twilio in Menace Voice

1. Open **Phone numbers** and click **Add phone number**.
2. Choose **Twilio**, then click **Continue**.
3. Enter a recognizable connection name, such as `Main Twilio account`.
4. Paste the **Account SID** and **Auth Token**, then click **Continue**.
5. Enter the purchased number with its country code, for example
   `+1 415 555 1234`.
6. Choose the agent that should answer incoming calls, or choose
   **Outgoing calls only**.
7. Finish setup.

### 4. Test the number

Call the Twilio number from another phone. Confirm that the selected agent
answers and that the call appears in **Agent Runs**. If Menace Voice reports a
provider-sync warning, follow the inbound checks below.

## Inbound Calling Setup

Inbound routing is driven by the phone number itself — there is a **single webhook URL** for the entire org, and Menace Voice resolves which agent to run from the called number's assigned inbound workflow. **When you save an inbound workflow on a phone number, Menace Voice automatically pushes the webhook URL to that number's `VoiceUrl` in your Twilio account** (provided the credentials are correct and the number belongs to that account).

### Step 1: Assign an Inbound Workflow to the Phone Number

1. Go to **/telephony-configurations** and open your Twilio configuration
2. In the **Phone numbers** section, edit the number that should receive inbound calls
3. Set its **Inbound workflow** to the agent that should answer
4. Save

### Step 2: Verify the Webhook URL in Twilio Console

1. Go to **Phone Numbers** → **Manage** → **Active Numbers** in Twilio Console
2. Click the phone number you assigned an inbound workflow to in Step 1
3. In the **Voice Configuration** section, confirm:
   - **Webhook** is set to: `https://voice.menaceui.com/api/v1/telephony/inbound/run`
   - **HTTP Method** is `POST`

   <Note>
     Menace Voice pushed this URL automatically when you saved the inbound workflow
     in Step 1. If the field is empty, shows a different URL, or Menace Voice
     surfaced a sync warning, the auto-push failed — most often because the
     credentials are incorrect or the number isn't owned by this Twilio
     account. Paste the URL into the field yourself, set the method to
     `POST`, and click **Save Configuration**. On self-hosted Menace Voice,
     replace `voice.menaceui.com` with your backend domain.
   </Note>

   <img
     className="block dark:hidden"
     src="/images/twilio-inbound-config.png"
     alt="Twilio phone number voice configuration showing webhook URL setup for inbound calls"
   />
   <img
     className="hidden dark:block"
     src="/images/twilio-inbound-config.png"
     alt="Twilio phone number voice configuration showing webhook URL setup for inbound calls"
   />

### Step 3: Verify Setup

- Ensure your Menace Voice instance is publicly accessible
- Verify any firewalls allow Twilio's IP ranges

### Test Inbound Calling

1. Call your configured Twilio phone number from another phone
2. Verify your Menace Voice agent answers and responds
3. Check call logs in both Menace Voice dashboard and Twilio Console

## Troubleshooting

<AccordionGroup>
  <Accordion title="Invalid phone number error">
    Ensure phone numbers include country code in E.164 format: `+1234567890`
  </Accordion>

  <Accordion title="Authentication failed">
    - Verify Account SID and Auth Token are correct - Check for extra spaces in
    credentials - Ensure credentials haven't been regenerated in Twilio Console
  </Accordion>

  <Accordion title="Webhook signature validation failing">
    - Confirm your Auth Token matches exactly - Verify the webhook URL matches
    what Twilio sends - Check if you're behind a proxy that modifies requests
  </Accordion>

  <Accordion title="No audio on calls">
    - Verify WebSocket connection is established - Check firewall rules for
    WebSocket traffic - Ensure audio pipeline is configured correctly
  </Accordion>

  <Accordion title="Inbound calls go to voicemail">
    - Verify the Twilio number's webhook is set to
    `https://voice.menaceui.com/api/v1/telephony/inbound/run` - Ensure the webhook
    URL is publicly accessible from the internet - Confirm the called number
    exists in your Menace Voice telephony configuration and has an **Inbound
    workflow** assigned - Verify Menace Voice instance is running and responding
  </Accordion>

  <Accordion title="Voice agent doesn't respond to inbound calls">
    - Confirm the phone number has an **Inbound workflow** assigned in
    /telephony-configurations - Check webhook signature validation is working
    (Auth Token in Menace Voice matches Twilio Console) - Verify WebSocket connection
    establishes successfully - Review call logs for error messages
  </Accordion>
</AccordionGroup>

## Best Practices

- Test your configuration with a single call before running campaigns
- Monitor Twilio Console for usage and billing
