How To Send Text Message To Phone Number From Node.js
The Complete Guide To Sending SMS from your Node / Express application.
There are at least two ways to send a text message to a phone number with AWS on Node.js
AWS services are not limited to Node.js but this tutorial specifically covers Node and Express.
(Note: you must be logged into your AWS IAM account to go to those dashboards.)
First you can use AWS SNS service which stands for Simple Notification Service.
You can also use AWS Pinpoint which can also send text messages to a phone number.
Each of these AWS services provides a sidebar with options and settings pages:
|
|
This tutorial will cover both but we'll start with SNS.
So why are we doing this?
Let's say you are building a social network or similar app that requires user registration.
To increase security you can verify registered users by sending a text message to their phone number. This can reduce number of fake accounts or prevent bots from registering.
Why sending text messages to a phone number is often overlooked by many apps?
A stand-alone Node application can send emails to an email address by installing a free email-sender module (for example NodeMailer). But Node servers cannot send text messages to a phone number out of the box. There are no free modules for that. To do that you have to use a cloud service like Amazon's AWS.
Here are the steps you need to take to start sending SMS to any phone number:
All of this will be explained in this Node.js SMS tutorial. We will use Express.js framework to create a simple endpoint that sends a phone message with a subject to any phone number.
- Setup Simple Notification Service in your AWS dashboard.
- Configure Node environment variables with your AWS secret keys.
- Install Node.js AWS SDK package (aws-sdk).
- Write a simple Express server that sends the SMS (phone text message).
Setup SNS in your AWS dashboard
Okay so let's get started with AWS SNS setup. Here's the first thing you want to do:
[ show screenshots with descriptions ]
Configure AWS secret keys in your Node.js .env file
There are different ways to set Node environment variables. You can assign them directly to process.env property from your application. But it's better to set them in Node server configuration file.
The easiest way to do this, however, is to simply create .env file (just the extension without filename) in the root folder of your Node application and use dotenv package to initialize it.
AWS_ACCESS_KEY_ID=your-iam-access-key
AWS_SECRET_ACCESS_KEY=your-iam-secret-key
AWS_REGION=your-region
Install Node.js AWS SDK package
We now need to include this module using require keyword at the top of your script. Follow instructions in the next section.
Write a simple Node.js Express server that sends a text by phone
And finally here is the actual program that will send the text message from Node:
[source code...]
Sending Text Messages With AWS Pinpoint
AWS Pinpoint is another service that can help you send SMS (and even voice messages.)
Like with many other AWS services your new Pinpoint service will be placed into sandbox mode. Until you request Production mode from AWS customer service it's going to have sandbox limitations.
In sandbox mode you can send text messages only to phone numbers you manually added and verified in your Pinpoint console dashboard. (Not you must be logged in to your AWS IAM account to access this page.)
Click on this image and then click Get Started with Amazon Pinpoint. This will bring you to a page where you will name and create your AWS Pinpoint. Just click Create a Project button.
For this tutorial I gave my Pinpoint project the named of SemicolonPinpoint.
Click on this image
Price Comparisons
Sending text messages is generally more expensive than sending email. Yes, it's possible to send email for free with a basic server (but your messages are more likely to land in Spam folder.) AWS Simple Email Service costs a fraction of what you'd pay for sending SMS text messages with Twilio, SNS or Pinpoint:
Twilio charges $0.0075
AWS SNS charges $0.00645
AWS Pinpoint charges $0.0012 but it depends (see below.)
AWS Pinpoint pricing varies. You pay per message but price depends on the country you are texting to. You can use this SMS pricing tool to determine the cost.
You always have to keep an eye on pricing because it frequently changes per country.
For example starting October 1, 2021 the per message price for sending an SMS to an Indian recipient will increase to $0.0413. This looks pricey and can add up quick. So be careful when using this service with overseas numbers.
In the United States AWS Pinpoint will charge you $0 for first 5000 endpoint calls. And after that $1.20 every 1000, which is where you get $0.0012 per text message from.
For more insight on Pinpoint pricing check out this page.
How much does it cost to send a phone text message with AWS?
AWS is relatively easy to setup. There are no monthly fees and it's cost-effective if the service is used sparcely. Bottom line is you only pay every time your Node application sends an SMS. Then every month you simply pay the total.
It costs $0.00645 to send one text message with AWS. (Costs have been dropping over the years to make the service accessible to more apps and it may have changed since I wrote this.)
This means you are going to pay $1 for every 156 text messages sent.
So if you want to learn how to start sending phone messages (SMS) directly from your Node.js application then this tutorial is for you. I will walk you through the process step by step.
We will use something Amazon calls SNS service (which stands for Simple Notification Service.) It is part of their AWS cloud services which includes hundreds of other useful APIs.
You can create bold text and lists as follows:
Example of an UL list
- item 1
- item 2
- item 2
Example of an OL list
- item 1
- item 2
- item 2
You can create source code tags by wrapping text in back-tick quotes.
You don't have to use p tags to create paragraphs, but you still can.
It's recommended to keep author tag so that readers who come from search engine traffic can also discover your social network links and bio.
Note: You can use regular HTML in this article but be careful not to break anything.