Neuphonic Agents and Twilio Integration Guide
Integrate conversational AI assistants with Twilio to create seamless voice experiences.
This demo illustrates how to integrate Neuphonic’s agent features with Twilio to create an interactive and intelligent phone voice agent capable of handling both inbound and outbound phone calls.
Prerequisites
Before getting started, ensure you have:
- A Neuphonic account and API Key
- A Twilio account with a Twilio phone number
- Python 3.9+
- A free ngrok account with the ngrok CLI installed and authenticated
Implementation
In this section, you will learn how to set up a server that manages both incoming and outgoing calls. The server will interact with Twilio to handle incoming audio and forward responses back to the user’s phone using Twilio’s services.
Looking for a complete example? Check out this demo on GitHub which you can run instantly.
Initialize the Project
Create a folder to hold the python project.
Create a Virtual Environment
Create a virtual environment using your desired environment manager.
Install Dependencies
Next, install the necessary dependencies.
Configure Your Environment
Create and configure your .env
file with the following secrets
NEUPHONIC_API_KEY
: Found on the Neuphonic portalTWILIO_AUTH_TOKEN
andTWILIO_ACCOUNT_SID
: Available in your Twilio portalFROM_NUMBER
: Your Twilio-purchased phone numberTO_NUMBER
: Your personal phone number for receiving test calls
Leave SERVER_BASE_URL
blank for now - we’ll fill this in later.
Create the Project Files
Create an app.py
and make_outbound_call.py
file with the following code
Your project should now look like this
Start ngrok
Start ngrok using the following command.
Copy the displayed URL (excluding the https://
prefix) and add it to your .env
file as SERVER_BASE_URL
. It will look something like 123d-45-678-912-3.ngrok-free.app
.
Note that this is an ephemeral address, every time you restart the ngrok service using the above command, you will be given a new URL.
Start the Server
Start the FastAPI server using the following command:
Your server is now running and ready to accept incoming calls and make outgoing calls.
Initiate an Outbound Call
Initiate a call from your Twilio number FROM_NUMBER
to your personal number TO_NUMBER
using
Enjoy chatting away!
Looking for a complete example? Check out this demo on GitHub which you can run instantly.
Initialize the Project
Create a folder to hold the python project.
Create a Virtual Environment
Create a virtual environment using your desired environment manager.
Install Dependencies
Next, install the necessary dependencies.
Configure Your Environment
Create and configure your .env
file with the following secrets
NEUPHONIC_API_KEY
: Found on the Neuphonic portalTWILIO_AUTH_TOKEN
andTWILIO_ACCOUNT_SID
: Available in your Twilio portalFROM_NUMBER
: Your Twilio-purchased phone numberTO_NUMBER
: Your personal phone number for receiving test calls
Leave SERVER_BASE_URL
blank for now - we’ll fill this in later.
Create the Project Files
Create an app.py
and make_outbound_call.py
file with the following code
Your project should now look like this
Start ngrok
Start ngrok using the following command.
Copy the displayed URL (excluding the https://
prefix) and add it to your .env
file as SERVER_BASE_URL
. It will look something like 123d-45-678-912-3.ngrok-free.app
.
Note that this is an ephemeral address, every time you restart the ngrok service using the above command, you will be given a new URL.
Start the Server
Start the FastAPI server using the following command:
Your server is now running and ready to accept incoming calls and make outgoing calls.
Initiate an Outbound Call
Initiate a call from your Twilio number FROM_NUMBER
to your personal number TO_NUMBER
using
Enjoy chatting away!
The JavaScript guide is coming soon! Contact us at support@neuphonic.com if you need help in the meantime.
Twilio Setup for Inbound Calls
This section explains how to set up the server you made in the above section to handle incoming calls to your Twilio phone number.
Get Your ngrok URL
Get the ngrok URL you created in the last section when you set up your server.
Configure Twilio
- Go to the Twilio Console
- Navgiate to
Phone Number
->Manage
->Active Number
and select your phone number. - Under
Voice Configuration
, set the URL in theA call comes in
section to your ngrok url,https://123d-45-678-912-3.ngrok-free.app/twilio/inbound_call
. - Set the HTTP method to POST.
Call Your Twilio Number
Go ahead and call your Twilio number. Your phone call will be connected through to the server and you can chat away!
Next Steps
This demo provides a basic introduction to integrating Neuphonic Agents with Twilio. To explore more advanced features and capabilities check out the Twilio Voice Documentation and Neuphonic Agents Documentation.