Sending text message using Amazon SNS and Node.js

Hey guys, Do you want to know how to send an SMS to a phone number.

nowadays many of the websites and apps use text messaging service for important notifications and promotions. In this medium tutorial, I will be showing you how to use Amazon SNS to send text messages to a specific phone number.

Photo by Christian Wiediger on Unsplash

Amazon Simple Notification Service(Amazon SNS) is a web service provided by AWS for sending messages to subscribers or endpoints. Amazon SNS has two parts -publishers and subscribers. Publishers send messages to a topic and subscribers who are subscribed to that topic will receive those messages.

You need to have an AWS account to use Amazon SNS

First, we will need to create a user role for getting the ACCESS_KEY_ID and the SECRET_ACCESS_KEY for working with SNS. So let’s create the user role. For this log into your AWS console and head towards the IAM management console. In the IAM console click on the ‘Users’ link in the left pane. Then click the blue ‘Add User’ button. you will be redirected to the bottom page.

Provide a username of your choice, here I had given SNSUser as the username. and also check the Programmatic access. Then click the Next Permisions button. In the next page,

Select the Attach existing policies directly option. Type SNS in the search field. Then select the AmazonSNSFullAccess policy. The best practice is to create a group and attach policies to the group and then add users to the group. but for this tutorial, I am going with the previous one. Then click the next button.

Skip the add tags page by clicking the next button. Now you’ll be in the review page. Click the Create user button to create the user.

Once the user is created you will be presented with the summary page

From here you can copy the ACCESS_KEY_ID and SECRET_ACCESS_KEY. you can also download the CSV file if needed.

Create an npm repository using npm init command. Change the file name from index.js to sendSMS.js while initializing the npm repository. Install aws-sdk package using npm install aws-sdk command.

Copy the following code to sendSMS.js

Set region as your preferred region something like ap-south-1.

For accessKeyId and secretAccessKey use the ACCESS_KEY_ID and SECRET_ACCESS_KEY which we had got while we created the IAM user.

The attributes inside the params1 object specifies whether you want to send a Transactional or Promotional SMS.

Transactional SMS is used for messages which are important to the customer like one-time-passwords. Amazon SNS optimizes the message delivery to achieve the highest reliability.

Promotional SMS is used for marketing messages and other non-important messages. Amazon SNS optimizes the message delivery to incur the lowest cost.

The setSMSTypePromise set the SMS attribute to AWS SNS.

The params2 object specifies the message you want to send and phone number to which you want to send the message.

message can be up to 140 bytes and character quota depends upon the encoding format.

  • 160 GSM characters
  • 140 ASCII characters
  • 70 UCS-2 characters

phone number should be specified using the E.164 format. i.e Phone number should start with plus character (+) and country code.

The publishTextPromise sends the SMS.

Run sendSMS.js using node sendSMS. A message will be received on the mobile number you provided.

For more information on sending an SMS message, you can refer to the official Amazon documentation here.

For Amazon SNS JavaScript SDK, you can refer here

And for the official Amazon documentation on Amazon SNS, you can refer here

If you find this helpful and interesting, consider giving a clap for this tutorial and please share this with others who are in need.

Good Day :)

Golang | Node.js | albinjose.me