Sending Text Messages with Javascript and Twilio
![Sending Text Messages with Javascript and Twilio](/content/images/size/w2000/2022/01/Twilio-SMS.png)
Twilio has become a favorite among devs looking to integrate messaging, calls, and streaming into their web and mobile apps. Their APIs are some of the most comprehensive and simple on the market, making it great for bootstrapping projects.
In today’s tutorial, we’re going to be using Twilio’s messaging API to send text messages with Javascript. If you want us to cover any other Twilio services, let us know in the comments!
The Setup
Before you start writing your NodeJS application, you need to set some things up
- Install NodeJS on your computer.
- Before building your NodeJS application, you need to create a Twilio account to get data that will help you run your application.
To set up your Twilio account:
- Go to https://www.twilio.com/try-twilio.
- Enter the information required and accept the Terms of Service.
- Next, you will be asked a few questions by Twilio to aid you in using the platform. Answer them to finally reach your dashboard.
- Once you have signed up, the first thing you need to do is verify our email address.
- Add a phone number and verify that as well using OTP.
- Your dashboard will now display your “ACCOUNT SID” and “AUTH TOKEN”. Twilio hides “AUTH TOKEN” by default to keep your account safe from misuse.
- The dashboard will also display your account’s “TRIAL BALANCE”. This balance keeps decreasing as you keep using Twilio’s features.
- Twilio trial accounts only let you send texts to verified phone numbers. So you need to add any numbers and verify them using OTP before you can text them using the Twilio API.
- Generate a Twilio trial number. This number will act as the sender’s number inside your application.
The Application
Follow the steps below to create your NodeJS application for sending texts using the Twilio API.
- Create a folder with a Javascript file called
app.js
- Using the terminal, run
npm init
inside the folder you just created. Accept all the defaults that pop on the terminal. - Using the terminal again run the following command to install the Twilio dependency :
npm install twilio
- Our application will make use of the “ACCOUNT SID” and the “AUTH TOKEN” you received when you created the Twilio account. You can directly copy those inside your application but that’s not secure, hence not recommended. We will make use of JavaScript environment variables which will grab the values from a file for local use.
- To make use of environment variables you need to install a package called dotenv by running the following command in the terminal:
npm install dotenv
- Now create a file named .env inside your project folder. Add your Twilio “ACCOUNT SID” and “AUTH TOKEN” to this file as shown below
TWILIO_ACCOUNT_SID=<your account sid here>
TWILIO_AUTH_TOKEN=<your auth token here>
Note: Never share your account sid or auth token with anyone.
7. We’ll then invoke the Twilio API like so
https://gist.github.com/RanjanSushant/089462549bd2c4a059bc4420a53361ff
There you have it! When you run this javascript file (with ‘node app.js’) you should get a message on the number mentioned in the “to” field. Since this is a trial account your message will have “Sent from your Twilio trial account” prepended to it. Also, as mentioned before, you can only send texts to verified mobile numbers from a trial account. If you try to send a text to an unverified number the Twilio API will generate an error message. Every text you send will reduce the trial balance in your Twilio account.
Well, you did it. You just created a basic application using NodeJS with Twilio API to send texts. When you are ready to show off your web app to the world, deploy it on Codesphere, the first bottle-neck free cloud platform.
Let us know in the comments what you plan to build using the Twilio API!
Happy Coding!