With just a few lines of code, your .NET application can make and receive phone calls with Twilio Programmable Voice.
This quickstart teaches you how to do this using Twilio's REST API, the Twilio C# / .NET Helper Library, and the ASP.NET framework to ease development.
In this quickstart, you will learn how to:
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 don't already have a Twilio Account, create one by filling out the form on the Try Twilio page.
If you already have a Voice-enabled phone number, skip to the Set up your development environment section.
If you are sending SMS to the U.S. or Canada, please be aware of updated restrictions on the use of Toll-Free numbers for messaging purposes. These restrictions do not apply to Voice, or to other non-SMS uses of Twilio phone numbers. But if you obtain a Toll-Free Number for non-SMS purposes and then wish to use it as well for Messaging, please read this first.
Follow the steps below to purchase a Twilio Phone Number with Voice capabilities.
_10dotnet --version
If you have .NET installed, you should see something like the following:
_10$ dotnet --version_102.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.
If you are using Visual Studio, select the File menu and choose New > Project > Console App (either .NET Core or .NET Framework will work).
dotnet
command line tool, run these commands to create a new .NET project:
_10mkdir twilio-test_10cd twilio-test_10dotnet new console_10dotnet add package Twilio
Now that you have .NET and the Twilio .NET library installed, you need to add code to your project that makes an API request to Twilio.
Open the Program.cs file in your new project and replace the existing code with the following code sample:
_26// Download the twilio-csharp library from twilio.com/docs/libraries/csharp_26using System;_26using Twilio;_26using Twilio.Rest.Api.V2010.Account;_26using Twilio.Types;_26_26namespace YourNewConsoleApp_26{_26 class Program_26 {_26 static void Main(string[] args)_26 {_26 // To set up environmental variables, see http://twil.io/secure_26 const string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");_26 const string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");_26 TwilioClient.Init(accountSid, authToken);_26_26 var to = new PhoneNumber("+14155551212");_26 var from = new PhoneNumber("+15017122661");_26 var call = CallResource.Create(to, from,_26 url: new Uri("http://demo.twilio.com/docs/voice.xml"));_26_26 Console.WriteLine(call.Sid);_26 }_26 }_26}
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 accountSid
and 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:
Edit Program.cs
and replace the values for accountSid
and 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:
_10dotnet run
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 awebhook. When Twilio sends your application a request, it expects a response in theTwiML 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)."