With just a few lines of code, your .NET application can make and receive phone calls with Twilio Programmable Voice.
In this quickstart, you will learn how to:
- Sign up for Twilio and get your first voice-enabled Twilio phone number
- Set up your development environment to make and receive phone calls
- Make an outbound phone call which plays an MP3
- Receive and respond to an inbound phone call which reads a message to the caller using Text to Speech
Prefer to get started by watching a video? Check out our video on how to place and receive phone calls with C# on Youtube.
If you already have a Twilio account and a voice-enabled Twilio phone number you’re all set here! Log in then feel free to jump to the next step.
The next thing you'll need is a voice-capable Twilio phone number. If you don't currently own a Twilio phone number with voice call functionality, you'll need to purchase one. After navigating to the Buy a Number page, check the "Voice" box and click "Search."
You’ll then see a list of available phone numbers and their capabilities. Find a number that suits your fancy and click "Buy" to add it to your account.
Now that you have a Twilio account and a programmable phone number, you have the basic tools you need to make a phone call.
You could use Twilio's HTTP API to make your phone calls, but we'll make things even simpler by using Twilio's official C# / .NET helper library. Let's install that next.
If you’ve gone through one of our other C# / .NET Quickstarts already and have .NET and the Twilio .NET helper library installed, you can skip this step and get straight to making your first phone call.
To make your first phone call, you’ll need to have .NET and the Twilio .NET package installed.
If you have Visual Studio installed, you are ready to get going. If you plan to use .NET Core with an editor other than Visual Studio, you can check if you already have .NET Core installed on your machine by opening up a terminal and running the following command:
You should see something like:
$ dotnet --version 2.1.4
If you don't have .NET Core already installed and will not be using Visual Studio, you can download .NET Core from Microsoft.
The Twilio .NET Package supports the .NET Framework 3.5 and above as well as any framework that supports .NET Standard 1.4.
The easiest way to install the library is using NuGet, the .NET package manager that lets you install the libraries you need.
If you are using Visual Studio, select the "File" menu and choose "New" then "Project..." and select "Console App" (either .NET Core or .NET Framework will work).
Then, select "Tools," "NuGet Package Manager," and "Package Manager Console" from the main menu in Visual Studio and type this command:
If you are using the
dotnet command line tool, run these commands to create a new .NET project:
mkdir twilio-test cd twilio-test dotnet new console dotnet add package Twilio
Now that we have .NET and the Twilio .NET library installed, we can make an outgoing phone call from the Twilio phone number we just purchased with a single API request. Open the file in your new project called Program.cs and type or paste in this code sample, replacing the template code that's already there.
This code starts a phone call between the two phone numbers that we pass as arguments. The 'from' number is our Twilio number, and the 'to' number is who we want to call.
The ‘url’ argument points to some TwiML (Twilio Markup Language), which tells Twilio what to do next when our recipient answers their phone. This TwiML tells Twilio to read a message using text to speech and then play an MP3.
Before this code will work, though, we need to edit it a little to work with your Twilio account.
Swap the placeholder values for
authToken with your personal Twilio credentials. Go to https://www.twilio.com/console and log in. On this page, you’ll find your unique Account SID and Auth Token, which you’ll need any time you send messages through the Twilio Client like this. You can reveal your auth token by clicking on the eyeball icon:
Program.cs and replace the values for
authToken with your unique values.
Please note: it's okay to hardcode your credentials when getting started, but you should use configuration to keep them secret before deploying to production. ASP.NET applications should use the built-in configuration system for ASP.NET on the .NET Framework or ASP.NET Core. Other types of .NET applications could use environment variables.
Remember that voice-enabled phone number you bought just a few minutes ago? Go ahead and replace the existing
from number with that one, making sure to use E.164 formatting:
[+][country code][phone number including area code]
Next, replace the to phone number with your mobile phone number. This can be any phone number that can receive calls, but it’s a good idea to test with your phone so that you can see the magic happen! As above, you should use E.164 formatting for this value.
Save your changes and run this code either in Visual Studio or from your terminal:
That’s it! Your phone should ring with a call from your Twilio number, and you'll hear our short message for you. 😉
If you're using a Twilio trial account, outgoing phone calls are limited to phone numbers you have verified with Twilio. Phone numbers can be verified via your Twilio Console's Verified Caller IDs. For other trial account restrictions and limitations, check out our guide on how to work with your free Twilio trial account.
When your Twilio number receives an incoming call, Twilio will send an HTTP request to a server you control. This callback mechanism is known as a webhook. When Twilio sends your application a request, it expects a response in the TwiML XML format telling it how to respond to the message. Let's see how we would build this in C# using ASP.NET MVC for .NET Framework 4.6.1. If you prefer using ASP.NET Core, check out this article. If you need to use ASP.NET Web API, we have an article for that, as well.
In Visual Studio, select the "File" menu and choose "New" then "Project..." and select "ASP.NET Web Application (.NET Framework)."
Next, choose the "MVC" template.
Select "Tools," "NuGet Package Manager," and "Package Manager Console" from the main menu in Visual Studio and type the following command:
Install-Package Twilio.AspNet.Mvc -DependencyVersion HighestMinor
In the directory named
Controllers, create a new Controller called
VoiceController.cs and use the following code to create a server that can handle incoming calls.
Run the project from Visual Studio. You should see the ASP.NET MVC home page running on
localhost and a random port number.
You'll need to make your application accessible over the internet. We'll show you how to set that up next so your app can receive calls.
We’ve just built a small ASP.NET MVC application to accept incoming phone calls. Before it will work, we need to make sure that Twilio can reach our application.
Most Twilio services use webhooks to communicate with your application. When Twilio receives a phone call, for example, it reaches out to a URL in your application for instructions on how to handle the message.
When you’re working on your ASP.NET application in your development environment, your app is only reachable by other programs on your computer, so Twilio won’t be able to talk to it. We need to solve this problem by making your application accessible over the internet.
While there are a lot of ways to do this, like deploying your application to Azure or AWS, you'll probably want a less laborious way to test your Twilio application. For a lightweight way to make your app available on the internet, we recommend a tool called ngrok. Ngrok listens on the same port that your local web server is running on and provides a unique URL on the ngrok.io domain, forwarding incoming requests to your local development environment. It works something like this:
After installing the Visual Studio extension, you will need to restart Visual Studio and reopen your project. Start your project again to bring up the home page. Then, while this is running, select "Start ngrok Tunnel" from the "Tools" menu.
Now we have a new external URL.
For Twilio to know where to look, you need to configure your Twilio phone number to call your webhook URL whenever a call comes in.
- Log into twilio.com and go to the Numbers page in the Console.
- Click on your voice-enabled phone number.
- Find the Voice & Fax section. The default “CONFIGURE WITH” is what you’ll need: "Webhooks, TwiML Bins, [etc.]"
- In the “A CALL COMES IN” section, select "Webhook" and paste in your URL: in this quickstart step above, it would be:
https://354f5b25.ngrok.io/voice- be sure to add
/voiceat the end, as this is the route to your
Save your changes - you’re ready!
Make sure you are running your project in Visual Studio, and your ngrok tunnel is running. If you restarted ngrok, you will have to update your webhook in the console to use the right URL.
With both of those servers running, we’re ready for the fun part - testing our new ASP.NET voice application!
Make a phone call from your mobile phone to your Twilio phone number. You should see an HTTP request in your ngrok console. Your ASP.NET app will process the incoming request and respond with your TwiML. Then you'll hear your message once the call connects.
Now you know the basics of making and responding to phone calls with C#.
Our ASP.NET app here only used the <Say> TwiML verb to read a message to the caller using text to speech, but you can do much more with different TwiML verbs like <Record>, <Gather>, and <Conference>.
Check out these pages to learn more:
- Gather user input via keypad (DTMF tones) in C#
- Learn how to record incoming and outgoing Twilio Voice phone calls using C#
- Create conference calls in C#
- Dive into the API Reference documentation for Twilio Programmable Voice
- Learn how to modify calls in progress with C#
- Looking to make a call from your browser or mobile application? Use Twilio Client to integrate high-quality VoIP calling.
- Retrieve information about in-progress and completed calls from your Twilio account using C#.
- Check out our full sample application tutorial on how to transfer calls from one support agent to another with C# and ASP.NET MVC
We can't wait to see what you build!