Sending Sms To Your Phone Using Nodejs And Twilio
A simple introduction to the Twilio API
This is a simple tutorial that shows you how a node application can be used to send a text message to your phone using the Twilio API. This article is for anyone that wants to have a taste of using the Twilio API in a simple and non-threatening way with Javascript.
REQUIREMENTS
- Basic understanding of command-line usage
- Basic knowledge of NodeJS
TABLE OF CONTENT
- WHAT IS TWILIO
PROJECT SETUP
BUILDING OUR APPLICATION
- CONCLUSION
WHAT IS TWILIO
Twilio is a customer engagement platform. Through its cloud-hosted API, we are able to integrate voice, SMS(texting), and video functionalities into an application.
PROJECT SETUP
We will be using a few tools to build this app, so you will need to set them up before proceeding;
- Twilio: To send SMS. Don't have a Twilio account? sign up for free here.
- NodeJS: A platform built on Chrome's Javascript runtime for easily building fast, scalable network applications. Download NodeJS here, if you don't have it already on your machine.
- Virtual Phone Number: A virtual phone number that will enable us to receive SMS online(get one here) or you can use your personal phone number
After installing NodeJS, create and verify your Twilio account. You will be given a free trial number and some other details that we will use later.
Now create a folder on your machine called twilioSms
, this will be the directory of our application.
BUILDING THE APPLICATION
Now let's focus on building the application.
Navigate to the twilioSms
directory through the command line on your code editor or use gitbash to run the following commands:
npm init --y
: The command creates apackage.json
file. This file holds the meta-data of our application.npm i twilio
: This installs the Twilio npm package that enables us to use the Twilio API.
After you have executed the preceding commands your file structure will look like this:
twilioSms
|- node_modules
|- package-lock.json
|- package.json
Create an index.js
file
twilioSms
|- node_modules
+ |- index.js
|- package-lock.json
|- package.json
Now inside index.js
add the following code
const client = require('twilio')('account_sid' , 'auth_token');
client.messages.create({
body: 'This is message is sent from a node app',
from:'+16014882966', // use your twilio number here
to: '+13072205075' // use your phone number or receiver number
})
.then(message => console.log(message))
.catch((err) => console.log(err));
In the preceding code, the Twilio API is used to send a message to the mobile number. Let's understand the code better.
- The Twilio module connects the app to your Twilio account. The
account_sid
andauth_token
is provided by Twilio when you signup, copy and paste them here.
The
messages.create()
method is provided by the Twilio library, which is used to create the message sent. There are some important properties here you should take note of.body
: This is the content of the message.from
: The value of this property is the phone number provided by Twilio, this is the sender of the text.to
: This is the number the text is sent to, make sure the number is verified by Twilio to avoid errors. You can do that here.
Also, make sure you enable geo-location messaging on Twilio for the region the receiver of your text is because it can also be a source of errors. For example, if I am sending a text to a US number and permission is not enabled for the North American region the text won't go through. You can do that here.
Finally, run this command node index.js
This executes our code and an SMS is sent to our phone or the number provided.
CONCLUSION
You can now send a simple text message using the Twilio API, congratulations. This is just the tip of the iceberg of what Twilio is capable of but it is a good start. Check out the Twilio documentation to get a holistic view of its functionalities.
Additional Resources: