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.
What is Amazon SNS?
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
Let’s get started
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,
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
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.
Now Let’s dive into the coding part
Create an npm repository using
npm init command. Change the file name from
sendSMS.js while initializing the npm repository. Install
aws-sdk package using
npm install aws-sdk command.
Copy the following code to
region as your preferred region something like ap-south-1.
secretAccessKey use the ACCESS_KEY_ID and SECRET_ACCESS_KEY which we had got while we created the IAM user.
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.
setSMSTypePromise set the SMS attribute to AWS SNS.
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.
publishTextPromise sends the SMS.
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.
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 :)