Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Programmable SMS Quickstart for C# with .NET Core

Looking for .NET Framework? We have a quickstart for that too!

With just a few lines of code, your .NET Core 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 Core.


  1. Twilioにサインアップして、SMS機能を持ったはじめてのTwilio電話番号を入手する
  2. メッセージを送受信できるように開発環境をセットアップする
  3. 最初のSMSを送信する
  4. テキストメッセージを受信する
  5. 受信メッセージに対してSMSで返信する

ビデオを見ながらの入門がお好みですか? YouTube上のC# SMSクイックスタートビデオ(英語)をご覧ください。

Show me how it's done!

すでにTwilioアカウントとSMS機能を持ったTwilio電話番号をお持ちなら、すでに準備万端です! どうぞ次のステップまで読み飛ばしてください。

Before you can send an SMS from C#, you'll need to sign up for a Twilio account or sign into your existing account and purchase an SMS-capable phone number.

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


すると、利用可能な電話番号と、その機能の一覧が表示されます。 お好みに合った番号が見つかったら「購入」をクリックすると、それがアカウントに追加されます。


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 install Twilio's official helper for .NET Core applications.

バッチリです! セットアップ方法を教えてください。

Install .NET Core

You can check if you already have .NET Core installed on your machine by opening up a command prompt or terminal and running the following command:

dotnet --version

You should see something like 2.1.3. If you receive an error message, you can download .NET Core from Microsoft and install it.

Create a new project and add the Twilio NuGet package

Run these commands to create a new .NET project and install the Twilio NuGet package:

mkdir TwilioSend
cd TwilioSend
dotnet new console
dotnet add package Twilio
All set! Let's send a text message.

Send an outbound SMS message with C#

Now that we have .NET Core and the Twilio .NET NuGet package 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.

        This code creates a new instance of the Message resource.


        This code creates a new instance of the Message resource.



        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 'view' link:

        Reveal your Auth Token in the Twilio Console


        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. We've written blog posts on how to secure user secrets in a .NET Core Web App and a .NET Core Console App that should provide you with some good guidance.

        Replace the "from" phone number

        数分前に購入した、SMS機能対応の電話番号を覚えていますか? どうぞ、既存のfrom番号をご自身の番号に置き換えてください。 また、E.164形式が使用されていることも確認します。

        [+][country code][phone number including area code]


        Replace the 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.


        Save your changes and run this code:

        dotnet run

        一丁上がり! ほどなくして、Twilio電話番号から携帯電話にSMSが受信されるはずです。

        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.

        メッセージを送信しました! どのように受信しますか?

        Receive and reply to inbound SMS messages with ASP.NET Core

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

        Create a new ASP.NET Core project

        Run these commands to create a new ASP.NET Core project and install the Twilio NuGet package:

        mkdir TwilioReceive
        cd TwilioReceive
        dotnet new mvc
        dotnet add package Twilio.AspNet.Core

        Create a new controller


              電話番号にメッセージが着信すると、TwilioはあなたのサーバーにHTTPリクエストを送信します。 このコードは(TwiMLを使って)テキストメッセージにどうサーバーが返信すべきかを表しています。


              電話番号にメッセージが着信すると、TwilioはあなたのサーバーにHTTPリクエストを送信します。 このコードは(TwiMLを使って)テキストメッセージにどうサーバーが返信すべきかを表しています。

              You now 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.

              Let's set up my app to receive messages.

              Allow Twilio to talk to your ASP.NET Core application with ngrok

              We’ve just built a small ASP.NET Core application to receive incoming messages. Before it will work, we need to make sure that Twilio can reach your application.


              When you’re working on your ASP.NET Core 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:


              If you haven't done so already, download and install ngrok. For more info on ngrok, including some great tips and tricks, check out this in-depth blog post.

              Change your application's launch settings

              Once you have ngrok downloaded, you need to make one small change to the Properties/launchSettings.json file. Find the line that has:

              "applicationUrl": "https://localhost:5001;http://localhost:5000",

              And change it to:

              "applicationUrl": "http://localhost:5000",

              This change is needed because ngrok will create a secure tunnel for you but will not work with the self-signed certificate that ASP.NET Core provides.

              Start your ASP.NET Core application

              Start your application by running:

              dotnet run

              You should see Now listening on: http://localhost:5000. You can try opening http://localhost:5000/sms in the browser and see the TwiML response we will be sending to Twilio. As we discussed, however, Twilio can't find your localhost, so we need one more step.

              Start the ngrok tunnel

              Now, start up ngrok on port 5000:

              ngrok http 5000

              You should see something like the following:

              Ngrok Extensions for Visual Studio - 実行中



              Twilioにどこを参照して良いか知らせるため、メッセージの受信時にTwilio電話番号にWebhook URLを呼び出すよう構成することが必要です。

              1. jp.twilio.comにログインし、Console上の電話番号ページに移動します
              2. SMS機能対応の電話番号をクリックします。
              3. メッセージの送受信セクションを見つけます。 「構成内容 (CONFIGURE WITH) 」は、既定である「Webhook/TwiML」にしておく必要があります。
              4. 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 /sms at the end, as this is the route to your SmsController class.




              Make sure your project is running 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 Core 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 Core app will process the text message, and you’ll get your response back as an SMS.

              It worked! All done - what's next?




              Rate this page:


              We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.