How to Send an SMS with Node.js
Time to read: 3 minutes
Twilio is all about powering communication and doing it conveniently and fast in any language.
Let's get started!
- 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
Run the following command from your terminal or command prompt to create a new directory for your project files on your computer:
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:
npm init -y
npm install twilio dotenv
Create two new files inside node-sms: index.js and .env:
touch index.js .env
Open your new .env file in your favorite text editor and add the following two empty variables:
Now, navigate back to your account dashboard on the Twilio Console and find your Account SID and Auth Token.
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.
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.
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.
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');
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = new twilio(accountSid, authToken);
body: 'Ahoy, friend!',
.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:
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.
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.
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.