Looking for .NET Core? We have a quickstart for that too!
With just a few lines of code, your .NET Framework application can send and receive text messages with Twilio Programmable SMS.
This C# SMS Quickstart will teach you how to do this using our Communications REST API and the Twilio helper library for .NET.
In this Quickstart, you will learn how to:
- Sign up for Twilio and get your first SMS-enabled Twilio phone number
- Set up your development environment to send and receive messages
- Send your first SMS
- Receive inbound text messages
- Reply to incoming messages with an SMS
Prefer to get started by watching a video? Check out our C# SMS Quickstart video on Youtube.
If you already have a Twilio account and an SMS-enabled Twilio phone number, you’re all set here! Feel free to jump to the next step.
You can sign up for a free Twilio trial account here.
- When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test messages to your phone from your Twilio account while in trial mode.
- Once you verify your number, you'll be asked to create a project. For the sake of this tutorial, you can click on the "Learn and Explore" template. Give your project a name, or just click "skip remaining steps" to continue with the default.
- Once you get through the project creation flow, you'll arrive at your project dashboard in the Twilio Console. This is where you'll be able to access your Account SID, authentication token, find a Twilio phone number, and more.
If you don't currently own a Twilio phone number with SMS functionality, you'll need to purchase one. After navigating to the Buy a Number page, check the "SMS" 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 can start writing some code! To make things even easier, we'll next install Twilio's official helper for .NET Framework applications.
If you have Visual Studio installed, you are ready to get going. Otherwise, you should download and install it now.
Once you have Visual Studio, open it up and create a new Console project. Click the "Create a new project" button:
And then choose "Console App (.NET Framework)" from the list of project templates:
Once you have Visual Studio, open it up and create a new Console project. Select the "File" menu and choose "New" then "Project..." and select "Console App (.NET Framework)".
Then, select "Tools," "NuGet Package Manager," and "Package Manager Console" from the main menu in Visual Studio and type this command:
Now that we have .NET and the Twilio .NET library installed, we can send an outbound text message 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.
You’ll need to edit this file a little more before your message will send:
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 'view' link:
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. Other types of .NET applications could use environment variables.
Remember that SMS-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]
to phone number with your mobile phone number. This can be any phone number that can receive text messages, but it’s a good idea to test with your own phone, so you can see the magic happen! As above, you should use E.164 formatting for this value.
If you are on a Twilio Trial account, your outgoing SMS messages are limited to phone numbers that you have verified with Twilio. Phone numbers can be verified via your Twilio Console's Verified Caller IDs.
Save your changes and run your project in Visual Studio.
That's it! In a few moments, you should receive an SMS from your Twilio number on your phone.
Are your customers in the U.S. or Canada? You can also send them MMS messages by adding just one line of code. Check out this sending MMS tutorial to see how it's done.
When your Twilio number receives an incoming message, 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.
In Visual Studio, click "Create a new project":
Select "ASP.NET Web Application (.NET Framework)" from the list of templates:
When prompted, choose "MVC" as the project type and be sure to uncheck HTTPS:
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
SmsController.cs. (Right-click on the Controllers folder, select "Add >", "Controller..." and choose the "MVC 5 Controller - Empty" template.)
Use the following code to create an action that can handle incoming messages. Be sure to use the entire code sample (note, for example, how the
SmsController inherits from
You'll need to make your application accessible over the internet. While you can do that in any number of ways, we recommend a tool that provides an externally accessible URL called ngrok. We'll show you how to set that up next so your app can receive messages.
We’ve just built a small ASP.NET application to receive incoming messages. Before it will work, we need to make sure that Twilio can reach your application.
Most Twilio services use webhooks to communicate with your application. When Twilio receives an SMS, 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 new message comes in.
- Log into Twilio.com and go to the Console's Numbers page.
- Click on your SMS-enabled phone number.
- Find the Messaging section. The default “CONFIGURE WITH” is what you’ll need: "Webhooks, TwiML, [etc.]".
- In the “A MESSAGE COMES IN” section, select "Webhook" and paste in your URL: in this quickstart step above, it would be:
https://354f5b25.ngrok.io/sms- be sure to add
/smsat 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 SMS application!
Send an SMS from your mobile phone to your Twilio phone number that's configured with this webhook. You should see an HTTP request in your ngrok console. Your ASP.NET app will process the text message, and you’ll get your response back as an SMS.
Now that you know the basics of sending and receiving SMS and MMS text messages with C# and .NET, you might want to check out these resources.
- REST API documentation
- TwiML reference docs
- Tutorials with full sample applications in C#
- Secure your C# / ASP.NET app by validating incoming Twilio requests