What is Twilio?
An API that allows your apps to integrate SMS, MMS, video, two-way authentication and all the functionality of the mobile phone. Twilio is used by many companies to connect with their users and customers through voice and video and build applications that allow them to use their phone to stay connected.
Can I use Twilio with NodeJS?
- Sign up for a Twilio account.
- Configure a new Twilio phone number. This is the phone number from where messages will be sent from.
- For the purposes of getting started, we’ll use our own personal mobile number with Twilio to test our SMS application.
- Install Twilio, node, and express using npm install
- Open Sublime Text and open a new .js file. I named mine ahoy.js
The Require Statements
var twilio will allow our application to use the Twilio library
var key contains the ACCOUNT_SID and ACCOUNT_TOKEN. For security purposes, I placed my ACCOUNT_SID, ACCOUNT_TOKEN, and Twilio phone number in a separate file so that they are not visible in my application.
var TWILIO_ACCOUNT_SID is the way the application will be verified with Twilio
var AUTH_TOKEN is the token that verifies our identify with Twilio
var number is the Twilio phone number we set up in Step 2
var client is the how we we marry the Twilio library, account_SID and token to make requests.
The Client Request Using the Twilio API
As stated above, I save my Twilio number as a variable that points to a file path where the actual number is located. I then placed this file in my .gitignore file.
The body can be changed to any message you wish.
The Success/Error Message
If there is no error, we’ll see the message unique ID or message_sid and the date it was created in our terminal.
Running the Application
Type the following command into your terminal:
You should receive a message in the terminal that looks like this:
Here is what it looks like when it arrives on our phone.
Here’s the link to the complete code on Github. Feel free to fork it and contribute