---
title: "How to Connect Your Twilio Agent to External APIs"
description: "This tutorial shows how to connect a Twilio voice agent to an external REST API so it can retrieve live information during a conversation. It uses C# .NET 9, Twilio Conversation Relay, WebSockets, OpenAI tool calling, and the authentication-free Cat Facts API as a demonstration. An ASP.NET Core application exposes a /voice webhook, connects calls to a /ws endpoint, maintains conversation messages, streams model responses, and executes get_cat_fact by requesting and parsing JSON from the Cat Facts service. The guide also covers environment variables, NuGet dependencies, ngrok tunneling, Twilio number configuration, and troubleshooting for webhook URLs and API keys. After setup, callers can ask for a cat fact and receive one from the external service, illustrating how function tools can give voice agents real-time data and perform API-backed work."
---

# How to Connect Your Twilio Agent to External APIs

[Twilio](https://yomu.fyi/company/twilio) · Amanda Lange · Sep 15, 2026

**Type:** Tutorial

## Summary

This tutorial shows how to connect a Twilio voice agent to an external REST API so it can retrieve live information during a conversation. It uses C# .NET 9, Twilio Conversation Relay, WebSockets, OpenAI tool calling, and the authentication-free Cat Facts API as a demonstration. An ASP.NET Core application exposes a /voice webhook, connects calls to a /ws endpoint, maintains conversation messages, streams model responses, and executes get\_cat\_fact by requesting and parsing JSON from the Cat Facts service. The guide also covers environment variables, NuGet dependencies, ngrok tunneling, Twilio number configuration, and troubleshooting for webhook URLs and API keys. After setup, callers can ask for a cat fact and receive one from the external service, illustrating how function tools can give voice agents real-time data and perform API-backed work.

## Context

The tutorial is aimed at .NET developers and requires a Twilio account with a voice-capable phone number, the .NET 9 SDK or later, an OpenAI API key, ngrok, and an IDE or text editor.

## Approach / What changed

The implementation uses an ASP.NET Core application with Twilio Conversation Relay over a WebSocket. It handles setup, voice prompts, interruptions, and errors; sends conversation history to OpenAI; accumulates tool-call arguments; and runs get\_cat\_fact to fetch and parse data from the Cat Facts API before returning the result through the voice conversation.

## Takeaways

- Twilio Conversation Relay connects the voice-capable Twilio number to an ASP.NET Core WebSocket endpoint, while TwiML supplies the initial greeting.
- The get\_cat\_fact tool calls the Cat Facts API, parses its JSON response, and returns either the fact, a fallback message, or an error when retrieval fails.
- ngrok exposes the local webhook to Twilio; the configured URL must match the running tunnel and end with /voice, using POST.

## FAQ

### How does the voice agent retrieve information from an external API?

OpenAI tool calling selects the get\_cat\_fact function during the conversation. The function requests data from the Cat Facts API, parses the JSON response, and sends the result back through the voice agent.

### What authentication does the demonstration API require?

The Cat Facts API used in the tutorial requires no additional authentication. If another external API needs credentials, the tutorial says those keys can be stored in the environment configuration and must be authenticated according to that API's rules.

### Why is ngrok needed when testing the application locally?

ngrok exposes the locally running webhook to the internet so Twilio can reach it. The Twilio phone-number configuration must use the current ngrok URL with /voice appended.

**Tags:** [.NET](https://yomu.fyi/topic/dotnet), [OpenAI API](https://yomu.fyi/topic/openai-api), [REST APIs](https://yomu.fyi/topic/rest-api), [WebSockets](https://yomu.fyi/topic/websockets)

- Source: [Twilio](https://www.twilio.com/en-us/blog/developers/tutorials/product/how-to-connect-twilio-agent-to-external-apis)
- Source URL: https://www.twilio.com/en-us/blog/developers/tutorials/product/how-to-connect-twilio-agent-to-external-apis
- Ingested by Yomu: 2026-09-17T00:01:22.912Z

[Read original post](https://www.twilio.com/en-us/blog/developers/tutorials/product/how-to-connect-twilio-agent-to-external-apis)
