How to Send an SMS with Node.js

March 16, 2021
Written by

header - How to Send an SMS with Node.js

Twilio is all about powering communication and doing it conveniently and fast in any language.  

With the help of Twilio and Node.js, you can send a quick SMS without having to pick up your mobile device.

Let's get started!

Prerequisites

  • A free or paid Twilio account. If you are new to Twilio get your free account now! (If you sign up through this link, Twilio will give you $10 credit when you upgrade.)
  • Node.js installed on your machine
  • npm or another package manager
  • A phone where you can receive SMS messages

Project set up

Run the following command from your terminal or command prompt to create a new directory for your project files on your computer:

mkdir node-sms

Change directory into this new folder, initiate a new Node.js project, and install the two packages you’ll need: the Twilio Node.js Helper Library and dotenv, for loading environment variables:

cd node-sms
npm init -y
npm install twilio dotenv

Create two new files inside node-sms: index.js and .env:

touch index.js .env

Get your Twilio account credentials and create a .env file

Open your new .env file in your favorite text editor and add the following two empty variables:

TWILIO_ACCOUNT_SID=
TWILIO_AUTH_TOKEN=

Now, navigate back to your account dashboard on the Twilio Console and find your Account SID and Auth Token.

Twilio Account Credentials

Copy your Account SID and add it as the value for the TWILIO_ACCOUNT_SID environment variable. Likewise, copy your Auth Token and add it as the value for the TWILIO_AUTH_TOKEN environment variable.

Save and close your .env file.

Buy a Twilio phone number

Now it’s time to buy a Twilio phone number. You’ll use the Twilio phone number to send an SMS to your personal phone number.

Still in the Twilio Console, select Phone Numbers, and then click on the red plus sign to buy a Twilio number. Note that if you are using a free account you will be using your trial credit for this purchase.

In the Buy a Number screen you can select your country and check SMS in the capabilities field. If you’d like to request a number from your region, you can enter your area code in the Number field.

Buy a phone number

Click the Search button to see what numbers are available, and then click “Buy” for the number that you like from the results. After you confirm your purchase, take note of your new phone number and click the Close button.

Send an SMS with Node.js

Back in your text editor, open your index.js file and copy and paste the following code, taking care to replace the value of the to  key on line 11 with your personal phone number, and the value for the from key on line 12 with your Twilio phone number. Note, both of these numbers should be in e1.64 format as demonstrated in this code snippet:


const twilio = require('twilio');
require('dotenv').config();

const accountSid = process.env.TWILIO_ACCOUNT_SID; 
const authToken = process.env.TWILIO_AUTH_TOKEN;  

const client = new twilio(accountSid, authToken);

client.messages.create({
    body: 'Ahoy, friend!',
    to: '+<YOUR_PHONE_NUMBER>', 
    from: '+<YOUR_TWILIO_NUMBER>' 
})
.then((message) => console.log(message.sid));

This code loads the Twilio library, and then uses dotenv to load your environment variables with your account credentials.

Then, it creates a new instance of the Twilio client, and uses the client to send an SMS from your Twilio phone number to your personal number with the message “Ahoy, friend!”.

Save this file, and switch to your terminal.

Run the following command:

node index.js

You’ll see the message SID of your newly generated message logged to your terminal, and in just a moment, you will get a ping on your personal phone with the SMS.

What's next for sending SMS with Node.js?

Congratulations on writing a short Node.js app to send a quick SMS to your phone! This was so wickedly quick, can you even believe it was possible?

There's so much more you can do with Twilio and code without even picking up your phone.

Explore some other neat functions you can play around with in JavaScript or test out Twilio's very own CLI:

Let me know about the projects you're building with JavaScript and Twilio SMS by reaching out to me over email!

Ashley is a JavaScript Editor for the Twilio blog. To work with her and bring your technical stories to Twilio, find her at @ahl389 on Twitter. If you can’t find her there, she’s probably on a patio somewhere having a cup of coffee (or glass of wine, depending on the time).