Programmable Messaging Quickstart for C# with .NET Framework

Ahoy there! All messaging transmitted using Twilio’s messaging channels is treated as Application-to-Person (A2P) messaging and subject to Twilio’s Messaging Policy. For detailed information on policy rules to ensure you remain compliant while using Twilio’s services, please see our Acceptable Use Policy.

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.


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

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

Show me how it's done!

Sign up for - or sign in to - Twilio

Already have a Twilio account? Go ahead and skip this section.

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 a series of questions to customize your experience.
  • Once you finish the onboarding 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.
I've got an account! What's next?

Install the Twilio CLI

We'll need to use the Twilio CLI (command line interface) for a few tasks, so let's install that next.

The suggested way to install twilio-cli on macOS is to use Homebrew. If you don’t already have it installed, visit the Homebrew site for installation instructions and then return here.

Once you have installed Homebrew, run the following command to install twilio-cli:

brew tap twilio/brew && brew install twilio

The suggested way to install twilio-cli is by using Scoop, a command-line installer for Windows. If you don’t already have it installed, visit the Scoop site for installation instructions and then return here.

Note PowerShell will need to be run as an administrator to avoid common permission issues when installing via Scoop.

  1. Add the twilio-cli Bucket:
    scoop bucket add twilio-scoop https://github.com/twilio/scoop-twilio-cli
  2. Install the app:
    scoop install twilio​

twilio-cli can be installed using the Advanced Package Tool (apt) on most distributions such as Debian, Ubuntu, and Mint.

To do so, run the following commands in your terminal:

wget -qO- https://twilio-cli-prod.s3.amazonaws.com/twilio_pub.asc \
  | sudo apt-key add -
sudo touch /etc/apt/sources.list.d/twilio.list
echo 'deb https://twilio-cli-prod.s3.amazonaws.com/apt/ /' \
  | sudo tee /etc/apt/sources.list.d/twilio.list
sudo apt update
sudo apt install -y twilio

For other installation methods, see the Twilio CLI Quickstart.

Run twilio login to get the Twilio CLI connected to your account. Visit https://www.twilio.com/console, and you’ll find your unique Account SID and Auth Token to provide to the CLI.



Now to get a phone number...

Get a phone number

If you don't currently own a Twilio phone number with SMS functionality, you'll need to purchase one. With the CLI, run:

twilio phone-numbers:buy:local --country-code US --sms-enabled

Replace US with your ISO-3166-1 country code if you would like a phone number in another country. If you aren't finding any SMS enabled numbers, try looking for a mobile number instead of a local number: twilio phone-numbers:buy:mobile --country-code DE --sms-enabled

Select a phone number to add it to your account.

Next, we need to install Visual Studio and the Twilio C# Helper Library.

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

Create a new project and add the Twilio NuGet package

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

Visual Studio - New .NET Framework Project

続いてVisual Studioのメインメニューから「ツール」を選択し、「NuGetパッケージマネージャー」->「パッケージマネージャーコンソール」をクリックし、下記コマンドを入力します:

Install-Package Twilio
All set! Let's send a text message.


これで.NETとTwilio .NETライブラリーがインストールできたので、今しがた購入したTwilio電話番号から、1回のAPIリクエストでSMSを送信できます。 新規プロジェクトでProgram.csという名前のファイルを開き、下記のコードサンプルを打ち込むかペーストし、すでにあるテンプレートコードと置き換えます。

        このコードはMessageリソースの新規リソースを作成し、MessageリソースURIにHTTP POST リクエストを送信します。


        このコードはMessageリソースの新規リソースを作成し、MessageリソースURIにHTTP POST リクエストを送信します。



        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 recommend using environment variables to store your user secrets.

        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 your project in Visual Studio.

        一丁上がり! ほどなくして、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.

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

        ASP.NET MVCを使用して受信SMSメッセージに返信する

        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.

        Visual Studioで新規ASP.NET MVCプロジェクトを作成する

        In Visual Studio, click "Create a new project":


        Select "ASP.NET Web Application (.NET Framework)" from the list of templates:


        Visual Studio 2019 0 Create a new ASP.NET Web Application

        When prompted, choose "MVC" as the project type:

        Visual Studio内で、「ファイル」メニューで「新規作成」を選択し、続いて「プロジェクト...」->「ASP.NET Webアプリケーション (.NET Framework) 」を選択します。

        Visual Studio - New ASP.NET MVC Project


        Visual Studio 新規 ASP.NET Webアプリケーション (.NET Framework) - MVC



        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 TwilioController).

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


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

              From Visual Studio, run the application (click the green "play" arrow). This should open a web browser with the ASP.NET welcome page showing with a URL something like https://localhost:44360. NOTE: your port number may be something other than 44360. You will need this URL in the next step to wire up your application to your Twilio phone number.

              I'm getting excited now - let's connect our code to Twilio!


              Now, you need to configure your Twilio phone number to call your webhook URL whenever a new message comes in. Just run this CLI command, replacing the phone number with your own Twilio phone number and ensuring the URL has your correct port number:

              twilio phone-numbers:update "+15017122661" --sms-url="https://localhost:44360/sms"

              The CLI will start an ngrok tunnel (so Twilio can reach your development machine) and wait patiently for incoming text messages!


              Make sure your project is running and your ngrok tunnel is running.

              上記の両方のサーバーの実行により、いよいよお楽しみの準備が整いました。 新しいASP.NET SMSアプリケーションをテストしましょう!

              Send an SMS from your mobile phone to your Twilio phone number that's configured with this webhook. Your ASP.NET app will process the text message, and you’ll get your response back as an SMS.

              It worked! All done - what's next?






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



                    Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

                    Sending your feedback...
                    🎉 Thank you for your feedback!
                    Something went wrong. Please try again.

                    Thanks for your feedback!

                    Refer us and get $10 in 3 simple steps!


                    Get link

                    Get a free personal referral link here


                    Give $10

                    Your user signs up and upgrade using link


                    Get $10

                    1,250 free SMSes
                    OR 1,000 free voice mins
                    OR 12,000 chats
                    OR more