Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Programmable Voice Quickstart for C# / .NET


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(link takes you to an external page), and the ASP.NET(link takes you to an external page) framework to ease development.

In this quickstart, you will learn how to:

  1. Buy a voice-enabled Twilio phone number
  2. Set up your development environment
  3. Make an outbound phone call that plays an MP3
  4. Receive and respond to an inbound phone call that 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(link takes you to an external page).


Before you begin

before-you-begin page anchor

If you don't already have a Twilio Account, create one by filling out the form on the Try Twilio page(link takes you to an external page).


1. Buy a Twilio Voice phone number

1-buy-a-twilio-voice-phone-number page anchor

If you already have a Voice-enabled phone number, skip to the Set up your development environment section.

(warning)

Warning

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(link takes you to an external page).

Follow the steps below to purchase a Twilio Phone Number with Voice capabilities.

  1. Log in to your Twilio Console(link takes you to an external page) .
  2. Navigate to the Buy a Number Console page: Search for "Buy a number" in the search bar at the top of your Console, or in the navigation sidebar, click on Phone Numbers > Manage > Buy a number .
  3. Under Capabilities , make sure that the Voice checkbox is checked.
  4. Under Search criteria , enter any criteria you want in your phone number and click the Search button.
  5. Choose a number from the list and click its associated Buy button.
Console Screenshot - Buy a Phone Number page.
  1. On the Review Phone Number pop-up, click the Buy (XXX) XXX-XXXX button to confirm your purchase.
Console Screenshot - Review Phone Number Modal.

2. Set up your development environment

2-set-up-your-development-environment page anchor

To make your first phone call, you'll need to have .NET and the Twilio .NET package installed.

2.1 - Install Visual Studio or .NET Core

21---install-visual-studio-or-net-core page anchor

If you have Visual Studio(link takes you to an external page) installed, you can move to step 2.2.

If you plan to use .NET Core(link takes you to an external page) 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:


_10
dotnet --version

If you have .NET installed, you should see something like the following:


_10
$ dotnet --version
_10
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(link takes you to an external page).

(information)

Info

The Twilio .NET Package supports the .NET Framework 3.5 and above as well as any framework that supports .NET Standard 1.4(link takes you to an external page).

2.2 - Install the Twilio .NET Package

22---install-the-twilio-net-package page anchor

The easiest way to install the library is using NuGet.

Visual Studio

visual-studio page anchor

If you are using Visual Studio, select the File menu and choose New > Project > Console App (either .NET Core or .NET Framework will work).

Visual Studio New Console App.

Then, from the main menu in Visual studio, select Tools > NuGet Package Manager > Package Manager Console. Type this command:


_10
Install-Package Twilio

.NET Core Command Line

net-core-command-line page anchor

If you are using the dotnet command line tool, run these commands to create a new .NET project:


_10
mkdir twilio-test
_10
cd twilio-test
_10
dotnet new console
_10
dotnet add package Twilio


3. Make an outgoing phone call with C#

3-make-an-outgoing-phone-call-with-c page anchor

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:

Make an outgoing call using Twilio and C#

make-an-outgoing-call-using-twilio-and-c page anchor

_26
// Download the twilio-csharp library from twilio.com/docs/libraries/csharp
_26
using System;
_26
using Twilio;
_26
using Twilio.Rest.Api.V2010.Account;
_26
using Twilio.Types;
_26
_26
namespace 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.

Replace the placeholder credential values

replace-the-placeholder-credential-values page anchor

Swap the placeholder values for accountSid and authToken with your personal Twilio credentials. Go to https://www.twilio.com/console(link takes you to an external page) 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.

(error)

Danger

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(link takes you to an external page) or ASP.NET Core(link takes you to an external page). Other types of .NET applications could use environment variables(link takes you to an external page).

Replace the to and from phone numbers

replace-the-to-and-from-phone-numbers page anchor

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:


_10
dotnet run

That's it! Your phone should ring with a call from your Twilio number, and you'll hear our short message for you. 😉

(warning)

Warning

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(link takes you to an external page). For other trial account restrictions and limitations, check out our guide on how to work with your free Twilio trial account.


Set up ASP.NET MVC to receive calls

set-up-aspnet-mvc-to-receive-calls page anchor

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.

Create a New ASP.NET MVC Project in Visual Studio

create-a-new-aspnet-mvc-project-in-visual-studio page anchor

In Visual Studio, select the "File" menu and choose "New" then "Project..." and select "ASP.NET Web Application (.NET Framework)."

Visual Studio New ASP.NET Web Application (.NET Framework).

Next, choose the "MVC" template.

Visual Studio New ASP.NET Web Application (.NET Framework) - MVC.

Install the Twilio.AspNet.Mvc Package

install-the-twilioaspnetmvc-package page anchor

Select "Tools," "NuGet Package Manager," and "Package Manager Console" from the main menu in Visual Studio and type the following command:


_10
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.

Receive an incoming call using Twilio, C#, and ASP.NET MVC

receive-an-incoming-call-using-twilio-c-and-aspnet-mvc page anchor

_21
// In Package Manager, run:
_21
// Install-Package Twilio.AspNet.Mvc -DependencyVersion HighestMinor
_21
_21
using System.Web.Mvc;
_21
using Twilio.AspNet.Mvc;
_21
using Twilio.TwiML;
_21
_21
namespace YourNewWebProject.Controllers
_21
{
_21
public class VoiceController : TwilioController
_21
{
_21
[HttpPost]
_21
public ActionResult Index()
_21
{
_21
var response = new VoiceResponse();
_21
response.Say("Thank you for calling! Have a great day.");
_21
_21
return TwiML(response);
_21
}
_21
}
_21
}

Run the project from Visual Studio. You should see the ASP.NET MVC home page running on localhost and a random port number.

Visual Studio New ASP.NET Web Application - Home page.

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.


Allow Twilio to Talk to Your ASP.NET Application with ngrok

allow-twilio-to-talk-to-your-aspnet-application-with-ngrok page anchor

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 calledngrok(link takes you to an external page). 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:

How ngrok helps Twilio reach your local server.

If you haven't done so already, install ngrok Extensions for Visual Studio(link takes you to an external page). For more info on ngrok, including some great tips and tricks, check out this in-depth blog post(link takes you to an external page).

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.

Ngrok Extensions for Visual Studio.
Ngrok Extensions for Visual Studio - running.

Now we have a new external URL.

Configure Your Webhook URL

configure-your-webhook-url page anchor

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.

  1. Log into twilio.com and go to the Numbers page in the Console(link takes you to an external page) .
  2. Click on your voice-enabled phone number.
  3. Find the Voice & Fax section. The default "CONFIGURE WITH" is what you'll need: "Webhooks, TwiML Bins, [etc.]"
  4. 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 /voice at the end, as this is the route to your VoiceController class.
Voice Webhook ngrok/voice.

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:

We can't wait to see what you build!


Rate this page: