Twilio SMS API Tutorial - Send SMS To Users From Your Website or Application

Sameer Saini January 28, 2022
Twilio SMS API Tutorial - Send SMS To Users From Your Website or Application

Do you want to send SMS notifications alerts or verifications to phone numbers of your users from your website or application Twilio offers SMS APIs in which you can reliably send and receive text messages.


So let's see how you can add Twilio SMS APIs to your application.


Creating an account with Twilio

To begin with, we need to create an account with Twilio so go to https://twilio.com and click on the signup page once you have given all the details 
and verified yourself you will come to this page where we want to configure the services we want to use with Twilio.

We will then select "SMS" from the first dropdown.

Then we will select the use case for which we want to integrate Twilio into our website or application. For this example, I have selected "Alerts and Notifications"  











The next question is how do you want to build with Twilio. 

I want to build it with ASP.NET and C# code so I'll select "With Code".

From the next option "What's your preferred language" question please select C#.

After making the selections, click on "Get Started With Twilio" and this will redirect you to https://console.twilio.com.

On the home page, we can see that Twilio has generated us an "Account SID" or Auth Id and an Auth Token which is also called an "API Key or Secret".

As it says on the home page, you should never share your auth token or api key with anyone else and should always store it securely in the server. 

If you store the API key in the client i.e. HTML, anyone would be able to see it and use it.

Get A Twilio Trial Phone Number

To use SMS, you will need a phone number from Twilio. On your trial account, you can get one free USA or Canada phone number. To get a local phone number outside of USA or Canada, you may need to upgrade your account and meet regulatory requirements.

Then we click on "Get a trial phone number"

Then we click on the get this number link


Install Twilio CLI

Then we need to install Twilio CLI onto our machine.

For that, we need to first check if we have npm installed.

You can check the version of node installed into your machine by the following command in your command prompt or terminal window:

Once we have confirmed that we have a version of node installed, we will go ahead and install Twilio CLI.

In command prompt or terminal window, type the below command:

npm install twilio-cli@latest -g

Press enter to install.



Twilio Login Using CLI

We will then go ahead and login to Twilio using the CLI, command prompt or terminal window.

Type the below command:

twilio login

Then enter your Account SID and Auth Token into the terminal window or command prompt to login.

Create C# Console Application

We will open visual studio 2022 and create a new console application.

Then, we will create a new Twilio config JSON and add the AccountSID and AuthToken into it so that we can read it in our application and it's stored in the settings and not hardcoded in the application.


We will then read the Twilio Config using the below C# code

Then we will install Twilio Nuget Package from Nuget.

The next step is to create a Twilio client inside the application using the Auth Id and Auth Token.

Then we will create a Message Resource and add a body, to phone number, and from phone number.

Checking Delivery Of SMS

If you want to check the status of the delivery of the SMS that was sent through the application, you can go back to console.twilio.com and on the messaging overview page see the delivery of the recent messages.

Full Video Tutorial