AWS SNS with Node js, TypeScript- OTP(One Time Passcode) authentication

Arunachalam kalimuthu
3 min readJun 28, 2022

With the increase in the number of mobile devices around the globe today, and numerous mobile applications available to us, SMS is becoming the de facto standard for verification.

Before going directly into integration , we need to understand AWS SNS first.

Amazon Simple Notification Service (Amazon SNS) is a fully managed messaging service for both application-to-application (A2A) and application-to-person (A2P) communication.

The A2P functionality enables you to send messages to users at scale via SMS, mobile push, and email.

AWS SNS — How it works

Prerequisite tasks

To set up, you must first complete these tasks:

  • Install npm.
  • Set up the project environment to set up Node, typescripts, and install the required AWS SDK for JavaScript and third-party modules.
  • You need to provide credentials to AWS so that only your account and its resources are accessed by the SDK. The SDK automatically detects AWS credentials set as variables in your environment and uses them for SDK requests. This eliminates the need to manage credentials in your application. The environment variables that you set to provide your credentials are:

AWS_ACCESS_KEY_ID

AWS_SECRET_ACCESS_KEY

AWS_SESSION_TOKEN

Step 1: Installation

In your Node JS Project, After setting up with typescript, you need to install an AWS SDK

$ npm install aws-sdk$ yarn install aws-sdk

Step 2: Integrating AWS-SNS in our App

Create a new file in the project directory and name it index.ts. In the new file copy and paste the following code:

Generating AWS Access Key ID and Secret Access Key

Now you can easily access this account using your Access Key and Secret Access key of your AWS account. If you don't have one already then go to your account and click on My Security Credentials

And then select the Access keys (access key ID and secret access key) section.

Click on Create New Access Key

Download the Key pairs to your system for future use.

Click on Show Access key and you will get your Access Key ID and Secret Access Key.

You need to use this Access Key ID and Secret Access Key to connect to your AWS connect and acesss the AWS SNS.

Create a .env file at the root of your project and add your AWS access key,AWS secret key, and the region just as below:

AWS_SECRET_ACCESS_KEY=YOUR SECRET ACCESS KEY

AWS_REGION=YOUR REGION

AWS_ACCESS_KEY_ID=YOUR ACCESS KEY

Now we are ready to send SMS in our node.js application so let us test our work

Testing our application

Just before we send our first SMS, we need to add a verified phone number to our AWS SNS account. This is so because we are on the trial version. So, from your AWS dashboard, click on verify phone number and verify a phone.

for more information refer to this.

You should have at least one verified phone number already if you set up your account properly.
Save your work and start the server by running node index.js on the terminal

Congratulations! You’ve just successfully sent an SMS from a Node.js application.

AWS SNS has a lot more to offer than just sending SMS to get more of their offerings, kindly visit their official documentation here

--

--