DEV Community

Cover image for Sending Text Messages with Javascript and Twilio
Simon Pfeiffer for Codesphere Inc.

Posted on

Sending Text Messages with Javascript and Twilio

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:

  1. Go to https://www.twilio.com/try-twilio.
  2. Enter the information required and accept the Terms of Service.
  3. Next, you will be asked a few questions by Twilio to aid you in using the platform. Answer them to finally reach your dashboard.
  4. Once you have signed up, the first thing you need to do is verify our email address.
  5. Add a phone number and verify that as well using OTP.
  6. Your dashboard will now display your “ACCOUNT SID” and “AUTH TOKEN”. Twilio hides “AUTH TOKEN” by default to keep your account safe from misuse.
  7. The dashboard will also display your account’s “TRIAL BALANCE”. This balance keeps decreasing as you keep using Twilio’s features.
  8. 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.
  9. 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.

  1. Create a folder with a Javascript file called app.js
  2. Using the terminal, run npm init inside the folder you just created. Accept all the defaults that pop on the terminal.
  3. Using the terminal again run the following command to install the Twilio dependency: npm install twilio
  4. 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.
  5. 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
  6. 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= TWILIO_AUTH_TOKEN= Note: Never share your account sid or auth token with anyone.
  7. We’ll then invoke the Twilio API like so

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!

Top comments (1)

Collapse
 
bobbyqgard profile image
Rob Gardner

Awesome! Do phone calls next!