How to Connect Your Twilio Agent to External APIs in Python
Time to read:
The world is starting to increasingly rely on voice-enabled AI agents to get work done. But an agent can only do so much. Voice AI agents by themselves are able to hold conversations, but what happens if the agent needs to do something like retrieve customer data, look at an inventory, or book an appointment for a user?
In order to make your AI agent truly helpful, you need for that AI agent to have access to real time information. External APIs can provide that information. When your agent works together with an API, your agent is empowered to get information your users really need, and take actions on the user’s behalf like viewing inventory, calendars, menus, and more.
In this tutorial, you will use Python and FastAPI to build a voice agent using Twilio Conversation Relay. Your agent will use tool calling from an LLM-driven conversation to dynamically fetch live data from an external REST API. This tutorial uses a simple API with no additional authentication requirements to showcase the potential of the AI tool. When you have completed the tutorial, you should understand the pipeline to interact with an external API, and how you could employ this functionality in your own builds.
Prerequisites
To complete this tutorial you will need the following:
- A free Twilio account with a voice-capable phone number
- Python 3.11 or later
- An OpenAI API key
- ngrok to expose local webhooks to Twilio
- An IDE or text editor such as Visual Studio Code
Building the application
Step 1 - Set up the FastAPI project
Your first step is creating a new folder and a Python virtual environment. Go into your terminal and type the following:
If you are on Windows, activate your virtual environment with .venv\Scripts\activate instead. A virtual environment keeps the packages you install for this project separate from the rest of your system, which helps avoid version conflicts with other Python projects.
Step 2 - Install dependencies
Install the packages you will need for your project by typing the following into your terminal:
These packages are necessary for your project setup: FastAPI and uvicorn will run your web server and handle the websocket connection. The openai package will be used to connect your solution to OpenAI. The twilio package will allow your application to generate TwiML. The python-dotenv package allows you to import your environment variables into your solution using a .env file, and httpx will let your application make asynchronous requests to the external API.
Step 3 - Configure environment variables
This tutorial is simple enough not to require much information from your Twilio account. But you will need somewhere to safely store your OpenAI API key. Create a file called .env in your project folder. Add to that file the following text:
Your OpenAI API Key is generated from OpenAI’s dashboard. You shouldn’t need any other keys in this file. However, if you decide later to call an API that has additional authentication, that key can be stored here as well.
Step 4 - Build the base application
You will use the very simple API, Cat Facts, in this demo. Our application is going to make a simple API call to request a “Cat Fact” from our agent. This API requires no additional authentication and has simple output, which makes it very useful for a demonstration.
Create a new file in your project folder called main.py. Open this file in your IDE of choice, and adjust it to have the following code:
This code is making a connection to a websocket to enable your agent. You are using Conversation Relay to build the connection between OpenAI and your voice-capable Twilio number, creating a voice agent that can hold a natural sounding conversation. The host is read straight off the incoming request’s Host header, so the websocket URL always matches whatever ngrok hostname is currently forwarding to your server, no manual configuration needed. Notice that you have also added a simple greeting for your agent using the welcome_greeting parameter, which is generated into TwiML by the Twilio helper library. This greeting line can be adjusted as needed to give the user an initial prompt for interaction.
Step 5 - Handle Conversation Relay
You will need some additional code to connect your websocket to Conversation Relay. Add the following to the bottom of your main.py file:
This code is communicating with your websocket, breaking your voice inquiries down into conversation messages to be processed by the AI. Changing your voice responses to text, it then streams that text in real time to the AI in order to get fast responses.
This is one important component, but you still need to make the connection to OpenAI. You will do that in the next step.
Step 6 - Connect to OpenAI
In this step, you will configure a tool function schema using the OpenAI Python SDK, and write the function that streams responses back to the caller.
Write the system prompt instructing the agent when to execute external API calls based on user voice prompts. You’ll see the prompt inside the SYSTEM_PROMPT constant in the code below. You can adjust this to your needs. In this prompt, you make sure that the AI realizes it’s being used for voice interaction, by reminding it not to use any bullet points or emojis when it communicates.
Add the following to the top of your main.py file, just below your other imports:
The get_cat_fact function is what actually calls our external API. It reaches out to the API located at https://catfact.ninja and parses the json response from the API. If it can’t find a fact, say, if the connection to the API is interrupted, it returns an error.
Now add the streaming function that ties the system prompt, the tool, and the OpenAI SDK together. Add this to the bottom of your main.py file:
This function streams tokens from OpenAI back to your websocket as they are generated, which keeps the perceived latency low for the caller. If the model decides it needs a cat fact, it responds with a tool call instead of text. Your code then executes that tool, sends the result back to OpenAI, and streams the model’s follow-up response, the one that actually answers the caller, back over the websocket.
Testing your application
Now it is time to test your application and chat with your AI.
First, run your application using this command in the terminal:
Once your webhook is running, you will need to expose it to the internet by using ngrok or another tunneling service.
Replace 8000 with whatever port your application is running on if you have a different port.
Now ngrok will provide you with a url for utilizing in your Twilio console. Go into your Twilio console and find the Twilio phone number that you prepared. Under the option A Call Comes In, choose Webhook, and fill in your ngrok URL followed by /voice, as shown in the graphic below:
Be sure also that your HTTP block is set to POST.
Now save this configuration, and call your Twilio Phone Number.
You should hear a message with the AI greeting that you provided in main.py.
Try asking your AI about a cat fact and you will get a cat fact from the cat fact API!
Troubleshooting
If you are having some difficulty with your call, there are some common problems you might want to check. First of all, make sure your ngrok URL is correct in the console and matches the one that’s in your terminal, with /voice appended to the end.
If you are still having issues, check your environment variables. You will need to make sure your API keys are correct for any key that you happen to be using, including your key for OpenAI. The sample API requires no additional keys, but if you decide to expand the application, you will also need to authenticate any external APIs that you call. Check the rules for your individual APIs.
Conclusion
Connecting LLM function tools to external HTTP endpoints empowers Twilio voice agents with real-time data. With the use of external APIs, you can create an agent that doesn’t just respond to questions, but truly does the work your customers need.
Are you looking for some further project ideas or further reading? We also have a series on getting started creating your AI Phone Agent with Conversation Relay.
We can’t wait to see what we can help you build!
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.