Twilio outbound calls

Build an outbound calling AI agent with Twilio and ElevenLabs.

In this guide you will learn how to build an integration with Twilio to initialise outbound calls to your prospects and customers.

What You’ll Need

Agent Configuration

Before integrating with Twilio, you’ll need to configure your agent to use the correct audio format supported by Twilio.

1

Configure TTS Output

  1. Navigate to your agent settings.
  2. Go to the Voice section.
  3. Select “μ-law 8000 Hz” from the dropdown.
2

Set Input Format

  1. Navigate to your agent settings. 2. Go to the Advanced section. 3. Select “μ-law 8000 Hz” for the input format.
3

Enable auth and overrides

  1. Navigate to your agent settings.
  2. Go to the security section.
  3. Toggle on “Enable authentication”.
  4. In “Enable overrides” toggle on “First message” and “System prompt” as you will be dynamically injecting these values when initiating the call.

Implementation

Looking for a complete example? Check out this Javascript implementation on GitHub.

1

Initialize the Project

First, set up a new Node.js project:

$mkdir conversational-ai-twilio
>cd conversational-ai-twilio
>npm init -y; npm pkg set type="module";
2

Install dependencies

Next, install the required dependencies for the project.

$npm install @fastify/formbody @fastify/websocket dotenv fastify ws
3

Create the project files

Create a .env and outbound.js file with the following code:

ELEVENLABS_AGENT_ID=<your-agent-id>
ELEVENLABS_API_KEY=<your-api-key>
# Twilio
TWILIO_ACCOUNT_SID=<your-account-sid>
TWILIO_AUTH_TOKEN=<your-auth-token>
TWILIO_PHONE_NUMBER=<your-twilio-phone-number>
4

Run the server

You can now run the server with the following command:

$node outbound.js

If the server starts successfully, you should see the message [Server] Listening on port 8000 (or the port you specified) in your terminal.

Testing

  1. In another terminal, run ngrok http --url=<your-url-here> 8000.
  2. Make a request to the /outbound-call endpoint with the customer’s phone number, the first message you want to use and the custom prompt:
$curl -X POST https://<your-ngrok-url>/outbound-call \
>-H "Content-Type: application/json" \
>-d '{
> "prompt": "You are Eric, an outbound car sales agent. You are calling to sell a new car to the customer. Be friendly and professional and answer all questions.",
> "first_message": "Hello Thor, my name is Eric, I heard you were looking for a new car! What model and color are you looking for?",
> "number": "number-to-call"
> }'
  1. You will see the call get initiated in your server terminal window and your phone will ring, starting the conversation once you answer.

Troubleshooting

If the WebSocket connection fails:

  • Verify your ngrok URL is correct in Twilio settings
  • Check that your server is running and accessible
  • Ensure your firewall isn’t blocking WebSocket connections

If there’s no audio output:

  • Confirm your ElevenLabs API key is valid
  • Verify the AGENT_ID is correct
  • Check audio format settings match Twilio’s requirements (μ-law 8kHz)

Security Best Practices

Follow these security guidelines for production deployments:

  • Use environment variables for sensitive information - Implement proper authentication for your endpoints - Use HTTPS for all communications - Regularly rotate API keys - Monitor usage to prevent abuse
Built with