Developer Quick-Start: A comprehensive guide to building a secure One-Time Password (OTP) system using Node.js, Express, and AWS SNS for SMS delivery, covering setup, implementation, security, and deployment. Want to skip 10DLC vetting queues, carrier surcharges, and multi-step SDK boilerplate? You can send the same SMS directly via SMSRoute's REST API with instant no-KYC provisioning.
Implement robust OTP/2FA in Node.js with Express and AWS SNS
One-Time Passwords (OTPs) sent via SMS are a common and effective way to add a second factor of authentication (2FA) or perform phone number verification in applications. This guide provides a complete walkthrough for building a secure and reliable OTP system using Node.js, the Express framework, and Amazon Simple Notification Service (AWS SNS) for SMS delivery.
We will build a simple REST API with two endpoints: one to request an OTP sent to a provided phone number and another to verify the submitted OTP. This guide covers everything from initial AWS setup and project configuration to implementing core logic, security best practices, error handling, and deployment considerations. By the end, you'll have a functional OTP service ready for integration into your applications.
Project overview and goals
Goal: To create a backend service using Node.js and Express that can:
- Generate a secure OTP.
- Send the OTP via SMS to a user's phone number using AWS SNS.
- Store the OTP temporarily with an expiry time and attempt limits.
- Verify an OTP submitted by the user against the stored value.
Problem Solved: This addresses the need for phone number verification or adding a second authentication factor (2FA) to enhance application security, using widely adopted and scalable cloud services.
Technologies:
- Node.js: A JavaScript runtime environment for building server-side applications.
- Express: A minimal and flexible Node.js web application framework.
- AWS SNS (Simple Notification Service): A fully managed messaging service used here to send SMS messages reliably.
- AWS SDK for JavaScript v3 (
@aws-sdk/client-sns): To interact with AWS services programmatically using the recommended modular client. - dotenv: To manage environment variables securely.
- (Optional but Recommended): A persistent data store like Redis or DynamoDB for OTP storage in production. This guide uses an in-memory store for simplicity.
Architecture:
graph LR
Client[Client Application] -- POST /request-otp --> API{Node.js/Express API};
Client -- POST /verify-otp --> API;
API -- Generate/Store OTP --> Store[(In-Memory Store / Redis)];
API -- Send SMS --> SNS[AWS SNS];
SNS -- Delivers SMS --> UserPhone[User's Phone];
API -- Verify OTP --> Store;
Prerequisites:
- An AWS account. Sign up here if you don't have one.
- Node.js and npm (or yarn) installed. Download Node.js here.
- Basic understanding of Node.js, Express, and REST APIs.
- Access to a phone number that can receive SMS for testing.
Setting up the project
Let's initialize our Node.js project and install the necessary dependencies.
- Create Project Directory: Open your terminal and create a new directory for the project, then navigate into it.
mkdir node-sns-otp-api
cd node-sns-otp-api
- Initialize Node.js Project: This creates a
package.jsonfile.
npm init -y
- Install Dependencies: We need Express for the server, the modular AWS SDK v3 client for SNS, and dotenv for environment variables.
npm install express @aws-sdk/client-sns dotenv
- express: Web framework.
- @aws-sdk/client-sns: AWS SDK v3 modular client for SNS. This is the recommended way to use the AWS SDK in Node.js.
- dotenv: Loads environment variables from a .env file.
- Create Project Structure: Organize the project for clarity.
mkdir src
mkdir src/routes
mkdir src/controllers
mkdir src/services
touch src/app.js
touch src/server.js
touch src/routes/otpRoutes.js
touch src/controllers/otpController.js
touch src/services/otpService.js
touch src/services/snsService.js
touch src/config.js
touch .env
touch .gitignore
- src/app.js: Express application setup (middleware, routes).
- src/server.js: Starts the HTTP server.
- src/routes/: Defines API endpoints.
- src/controllers/: Handles request logic and interacts with services.
- src/services/: Contains business logic (OTP generation/verification, SNS interaction).
- src/config.js: Loads and exports configuration from environment variables.
- .env: Stores environment variables (AWS keys, configuration). Do not commit this file.
- .gitignore: Specifies files/directories Git should ignore.
- Configure
.gitignore: Addnode_modulesand.envto prevent committing them.
node_modules/
.env
*.log
- Configure
.env: Create a.envfile in your project root.
Important Security Note: The values below are placeholders. You must
replace YOUR_AWS_ACCESS_KEY_ID, YOUR_AWS_SECRET_ACCESS_KEY, and
YOUR_AWS_REGION with your actual AWS credentials and chosen region. Never commit your
.env file containing real secrets to version control (like Git). Use the
.gitignore file to prevent this. For production environments, use more secure methods like AWS
Secrets Manager or environment variables provided by your hosting platform. .env
# AWS Credentials - REPLACE WITH YOUR ACTUAL CREDENTIALS
AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
AWS_REGION=YOUR_AWS_REGION # e.g., us-east-1
# Application Configuration
PORT=3000
OTP_LENGTH=6
OTP_EXPIRY_MINUTES=5
OTP_MAX_ATTEMPTS=3
# AWS SNS Configuration
SNS_SMS_TYPE=Transactional # Or Promotional
AWS_REGION: The AWS region where you'll operate SNS (choose one that supports SMS, like us-east-1, eu-west-1, ap-southeast-2). See AWS Regions and Endpoints documentation for details.SNS_SMS_TYPE: Set to Transactional for higher delivery priority, especially for OTPs, even to numbers on Do Not Disturb (DND) lists (may incur slightly higher costs). Promotional is for marketing messages.
AWS setup for SNS
To send SMS messages, we need to configure AWS credentials and SNS settings.
- Create an IAM User: It's best practice to create a dedicated IAM user with specific permissions rather than using root account keys.
- Navigate to the IAM console in your AWS account.
- Go to Users and click Add users.
- Enter a User name (e.g., sns-otp-service-user).
- Select Provide user access to the AWS Management Console (optional).
- Select I want to create an IAM user if creating a console user. Set a password.
- Click Next.
- Select Attach policies directly.
- Search for and select the AmazonSNSFullAccess policy. Note: For production, create a more restrictive custom policy granting only sns:Publish permissions.
- Click Next. Review the settings and click Create user.
- Crucial: On the success screen, navigate to the user you just created. Go to the Security credentials tab. Under Access keys, click Create access key.
- Select Application running outside AWS as the use case.
- Click Next, add an optional description tag, and click Create access key.
- Important: Copy the Access key ID and Secret access key immediately and securely. You won't be able to see the secret key again.
- Update
.envfile: Paste the copied credentials and your chosen region into your.envfile:
.env
AWS_ACCESS_KEY_ID=COPIED_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=COPIED_SECRET_ACCESS_KEY
AWS_REGION=us-east-1 # Replace with your chosen region
# ... other variables
- Configure AWS SNS Settings (AWS Console):
- Navigate to the Simple Notification Service (SNS) console in your chosen AWS region.
- Sandbox Limitations (Important): By default, your AWS account is in the SNS sandbox. This means:
- You can only send SMS messages to verified phone numbers.
- There's a low spending limit (e.g., $1.00 USD per month).
- Verify a Phone Number (for Sandbox Testing):
- In the SNS console left navigation pane, click Mobile > Text messaging (SMS).
- Scroll down to the Sandbox destination phone numbers section and click Add phone number.
- Enter the phone number you want to test with (including the country code in E.164 format, e.g., +12223334444).
- Select the language for the verification message and click Add phone number.
- You'll receive an SMS with a verification code. Enter it in the AWS console to verify the number.
- Moving out of the Sandbox (for Production):
- To send SMS to any number, you must request to move your account out of the SNS sandbox.
- In the SNS console, under Mobile > Text messaging (SMS), you should see a banner about the sandbox. Click the link to request a limit increase or production access.
- Alternatively, open a support case with AWS requesting ""SNS Text Messaging Spending Limit Increase"" and production access. Explain your use case (e.g., sending OTPs for application security). This process usually takes about 24 hours.
- Set Default SMS Type:
- Still under Mobile > Text messaging (SMS), click Edit in the Account spend limits and default message settings section.
- Under Default message settings, set the Default SMS message type to Transactional (recommended for OTPs) or Promotional based on your .env setting and use case.
- Click Save changes.
Implementing core functionality
Now let's write the code for OTP generation, storage, verification, and sending SMS via SNS.
Configuration Loader
require('dotenv').config();
const config = {
PORT: parseInt(process.env.PORT, 10) || 3000,
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY,
AWS_REGION: process.env.AWS_REGION,
OTP_LENGTH: parseInt(process.env.OTP_LENGTH, 10) || 6,
OTP_EXPIRY_MINUTES: parseInt(process.env.OTP_EXPIRY_MINUTES, 10) || 5,
OTP_MAX_ATTEMPTS: parseInt(process.env.OTP_MAX_ATTEMPTS, 10) || 3,
SNS_SMS_TYPE: process.env.SNS_SMS_TYPE || 'Transactional',
};
const requiredKeys = ['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_REGION'];
requiredKeys.forEach(key => {
if (!config[key]) {
console.error(`FATAL ERROR: Environment variable ${key} is not defined.`);
process.exit(1);
}
});
module.exports = config;
OTP Storage (In-Memory)
For this guide, we'll use a simple JavaScript Map as an in-memory store.
Remember: This is not suitable for production as data is lost on server restart. Use Redis,
DynamoDB, or a similar persistent store in a real application. The basic setTimeout cleanup used
here is also rudimentary and may not be perfectly reliable or efficient at scale.
const crypto = require('crypto');
const config = require('../config');
const otpStore = new Map();
function generateOtp() {
const otp = crypto.randomInt(0, Math.pow(10, config.OTP_LENGTH)).toString();
return otp.padStart(config.OTP_LENGTH, '0');
}
function storeOtp(phoneNumber, otp) {
const expiryTime = Date.now() + config.OTP_EXPIRY_MINUTES * 60 * 1000;
otpStore.set(phoneNumber, {
otp,
expiryTime,
attempts: config.OTP_MAX_ATTEMPTS
});
console.log(`Stored OTP ${otp} for ${phoneNumber}, expires at ${new Date(expiryTime).toISOString()}, attempts left: ${config.OTP_MAX_ATTEMPTS}`);
setTimeout(() => {
const record = otpStore.get(phoneNumber);
if (record && record.otp === otp && record.expiryTime <= Date.now()) {
otpStore.delete(phoneNumber);
console.log(`Expired OTP for ${phoneNumber} removed by timeout.`);
}
}, config.OTP_EXPIRY_MINUTES * 60 * 1000 + 1000);
}
function verifyOtp(phoneNumber, submittedOtp) {
const record = otpStore.get(phoneNumber);
if (!record) {
console.error(`Verification attempt for ${phoneNumber}: No OTP record found.`);
return { success: false, message: 'OTP not found or expired.' };
}
if (Date.now() > record.expiryTime) {
console.error(`Verification attempt for ${phoneNumber}: OTP expired.`);
otpStore.delete(phoneNumber);
return { success: false, message: 'OTP has expired.' };
}
if (record.attempts <= 0) {
console.error(`Verification attempt for ${phoneNumber}: No attempts left.`);
return { success: false, message: 'Maximum verification attempts exceeded.' };
}
if (record.otp !== submittedOtp) {
record.attempts -= 1;
otpStore.set(phoneNumber, record);
console.warn(`Verification attempt for ${phoneNumber}: Incorrect OTP. Attempts left: ${record.attempts}`);
return { success: false, message: `Incorrect OTP. ${record.attempts} attempts remaining.` };
}
console.log(`Verification successful for ${phoneNumber}`);
otpStore.delete(phoneNumber);
return { success: true, message: 'OTP verified successfully.' };
}
module.exports = {
generateOtp,
storeOtp,
verifyOtp,
};
AWS SNS Service
const { SNSClient, PublishCommand } = require('@aws-sdk/client-sns');
const config = require('../config');
const snsClient = new SNSClient({
region: config.AWS_REGION,
credentials: {
accessKeyId: config.AWS_ACCESS_KEY_ID,
secretAccessKey: config.AWS_SECRET_ACCESS_KEY,
},
});
async function sendSms(phoneNumber, message) {
if (!/^\+[1-9]\d{1,14}$/.test(phoneNumber)) {
console.error(`Invalid phone number format: ${phoneNumber}. Must be E.164.`);
throw new Error('Invalid phone number format. Must start with + and country code (E.164).');
}
const params = {
Message: message,
PhoneNumber: phoneNumber,
MessageAttributes: {
'AWS.SNS.SMS.SMSType': {
DataType: 'String',
StringValue: config.SNS_SMS_TYPE,
},
},
};
try {
const command = new PublishCommand(params);
const data = await snsClient.send(command);
console.log(`Successfully sent SMS to ${phoneNumber}. MessageID: ${data.MessageId}`);
return { success: true, messageId: data.MessageId };
} catch (err) {
console.error(`Error sending SMS via SNS to ${phoneNumber}:`, err);
if (err.name === 'InvalidParameterException') {
throw new Error(`Failed to send SMS: Invalid parameter, check phone number format (${phoneNumber}) or message content.`);
} else if (err.name === 'AuthorizationErrorException') {
throw new Error('Failed to send SMS: AWS credentials are invalid or lack permissions.');
}
throw new Error(`Failed to send SMS: ${err.message}`);
}
}
module.exports = {
sendSms,
};
Building the API layer
Now, let's connect the logic to Express endpoints.
OTP Controller
const otpService = require('../services/otpService');
const snsService = require('../services/snsService');
const config = require('../config');
const E164_REGEX = /^\+[1-9]\d{1,14}$/;
const OTP_REGEX = new RegExp(`^\\d{${config.OTP_LENGTH}}$`);
async function requestOtp(req, res) {
const { phoneNumber } = req.body;
if (!phoneNumber || !E164_REGEX.test(phoneNumber)) {
return res.status(400).json({ success: false, message: 'Valid E.164 phone number is required (e.g., +12223334444).' });
}
try {
const otp = otpService.generateOtp();
const message = `Your verification code for MyApp is: ${otp}. It expires in ${config.OTP_EXPIRY_MINUTES} minutes.`;
otpService.storeOtp(phoneNumber, otp);
await snsService.sendSms(phoneNumber, message);
console.log(`OTP requested for ${phoneNumber}. OTP: ${otp}`);
res.status(200).json({ success: true, message: `OTP sent successfully to ${phoneNumber}.` });
} catch (error) {
console.error('Error requesting OTP:', error);
res.status(500).json({ success: false, message: 'Failed to send OTP. Please try again later.' });
}
}
async function verifyOtp(req, res) {
const { phoneNumber, otp } = req.body;
if (!phoneNumber || !E164_REGEX.test(phoneNumber)) {
return res.status(400).json({ success: false, message: 'Valid E.164 phone number is required.' });
}
if (!otp || !OTP_REGEX.test(otp)) {
return res.status(400).json({ success: false, message: `A ${config.OTP_LENGTH}-digit OTP is required.` });
}
try {
const verificationResult = otpService.verifyOtp(phoneNumber, otp);
if (verificationResult.success) {
res.status(200).json({ success: true, message: verificationResult.message });
} else {
res.status(400).json({ success: false, message: verificationResult.message });
}
} catch (error) {
console.error('Error verifying OTP:', error);
res.status(500).json({ success: false, message: 'Failed to verify OTP due to an internal error. Please try again later.' });
}
}
module.exports = {
requestOtp,
verifyOtp,
};
OTP Routes
const express = require('express');
const otpController = require('../controllers/otpController');
const router = express.Router();
router.post('/request-otp', otpController.requestOtp);
router.post('/verify-otp', otpController.verifyOtp);
module.exports = router;
Express App Setup
const express = require('express');
const otpRoutes = require('./routes/otpRoutes');
const config = require('./config');
const app = express();
app.use(express.json());
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.originalUrl}`);
next();
});
app.use('/api/otp', otpRoutes);
app.get('/health', (req, res) => {
res.status(200).json({ status: 'UP', timestamp: new Date().toISOString() });
});
app.use((req, res, next) => {
res.status(404).json({ success: false, message: 'Resource not found.' });
});
app.use((err, req, res, next) => {
console.error(""Unhandled Error:"", err.stack || err);
res.status(500).json({ success: false, message: 'An unexpected internal server error occurred.' });
});
module.exports = app;
Server Entry Point
const app = require('./app');
const config = require('./config');
const PORT = config.PORT;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`AWS Region: ${config.AWS_REGION}`);
console.log(`OTP Length: ${config.OTP_LENGTH}, Expiry: ${config.OTP_EXPIRY_MINUTES} mins, Max Attempts: ${config.OTP_MAX_ATTEMPTS}`);
console.log(`SNS SMS Type: ${config.SNS_SMS_TYPE}`);
console.warn('--- WARNING: Using in-memory OTP store. Data will be lost on restart. Use Redis/DB for production. ---');
});
Update package.json start script
Add a start script to package.json for easy execution.
{
""name"": ""node-sns-otp-api"",
""version"": ""1.0.0"",
""description"": ""Node.js Express API for OTP/2FA using AWS SNS"",
""main"": ""src/server.js"",
""scripts"": {
""start"": ""node src/server.js"",
""test"": ""echo \""Error: no test specified\"" && exit 1""
},
""keywords"": [
""otp"",
""2fa"",
""sms"",
""aws"",
""sns"",
""node"",
""express""
],
""author"": """",
""license"": ""ISC"",
""dependencies"": {
""@aws-sdk/client-sns"": ""^3.XXX.X"",
""dotenv"": ""^16.X.X"",
""express"": ""^4.X.X""
},
""devDependencies"": {}
}
(Note: Update dependency versions like ^3.XXX.X in package.json based on your
actual installation)
Implementing error handling and logging
- Consistent Error Strategy: Our controllers return JSON responses with a success flag and a message. We use appropriate HTTP status codes (200 for success, 400 for bad requests/invalid OTPs, 404 for not found, 429 for rate limits, 500 for server errors).
- Logging: We added basic console.log, console.warn, and console.error statements. For production, use a structured logger like winston or pino to log in JSON format, include request IDs, control log levels (info, warn, error), and potentially ship logs to a centralized logging service (like CloudWatch Logs).
- SNS Errors: The snsService.js includes specific checks for InvalidParameterException and AuthorizationErrorException, providing clearer error messages when sending SMS fails.
- Global Error Handler: The final app.use((err, req, res, next) => ...) in app.js catches any unhandled errors that occur during request processing, logs them, and returns a generic 500 error to the client.
- Retry Mechanisms: For transient network issues when calling AWS SNS, you could implement a simple retry mechanism using libraries like async-retry. However, for OTPs, automatically retrying might send multiple SMS messages, which is usually undesirable. It's often better to return an error and let the user trigger a retry manually.
Database schema and data layer (Conceptual)
As mentioned, the in-memory store is unsuitable for production. If using Redis (recommended for OTPs due to speed and TTL features):
- Schema: Store data using keys like otp:{phoneNumber} (e.g., otp:+12223334444).
- Data: Store a JSON string or a Redis Hash containing { otp: '123456', attempts: 3 }.
- Expiry: Set a Time-To-Live (TTL) on the Redis key equal to config.OTP\_EXPIRY\_MINUTES \* 60 seconds. Redis handles automatic expiry efficiently.
If using a SQL database:
- Schema:
CREATE TABLE otps (
phone_number VARCHAR(20) PRIMARY KEY,
otp_code VARCHAR(10) NOT NULL,
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
attempts_remaining INT NOT NULL DEFAULT 3,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_otps_expires_at ON otps (expires_at);
- Data Layer: Use an ORM (like Sequelize, Prisma) or a query builder (like Knex.js) to
interact with the table, handling insertions, lookups (finding by
phone_number), updates (decrementingattempts_remaining), and deletions (on successful verification or expiry). Implement a background job or scheduled task to periodically delete rows whereexpires_atis in the past.
Adding security features
- Input Validation: We added basic validation for phone number (E.164) and OTP format using
regex. For more robust validation (checking types, lengths, allowed characters, sanitizing inputs), use
libraries like
express-validatororjoi. - Rate Limiting: Crucial to prevent SMS pumping fraud (maliciously triggering SMS costs)
and brute-force attacks on both requesting and verifying OTPs. Use
express-rate-limit:
npm install express-rate-limit
Update src/app.js to include and apply the limiters:
const express = require('express');
const rateLimit = require('express-rate-limit');
const otpRoutes = require('./routes/otpRoutes');
const config = require('./config');
const app = express();
app.use(express.json());
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.originalUrl}`);
next();
});
const otpRequestLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 10,
message: { success: false, message: 'Too many OTP requests from this IP, please try again after 15 minutes' },
standardHeaders: true,
legacyHeaders: false,
keyGenerator: (req) => req.ip,
});
const otpVerifyLimiter = rateLimit({
windowMs: 5 * 60 * 1000,
max: 10,
message: { success: false, message: 'Too many verification attempts from this IP, please try again after 5 minutes' },
standardHeaders: true,
legacyHeaders: false,
keyGenerator: (req) => req.ip,
});
app.use('/api/otp/request-otp', otpRequestLimiter);
app.use('/api/otp/verify-otp', otpVerifyLimiter);
app.use('/api/otp', otpRoutes);
app.get('/health', (req, res) => {
res.status(200).json({ status: 'UP', timestamp: new Date().toISOString() });
});
app.use((req, res, next) => {
res.status(404).json({ success: false, message: 'Resource not found.' });
});
app.use((err, req, res, next) => {
console.error(""Unhandled Error:"", err.stack || err);
res.status(500).json({ success: false, message: 'An unexpected internal server error occurred.' });
});
module.exports = app;
- Brute Force Protection (Verification): The
OTP_MAX_ATTEMPTSlogic implemented inotpService.jsprovides specific protection against guessing the OTP for a given phone number. - HTTPS: Always deploy your application behind a reverse proxy (like Nginx) or load balancer (like AWS ELB/ALB) that terminates SSL/TLS, ensuring all traffic is encrypted via HTTPS. Do not handle TLS directly in Node.js unless necessary.
- Secure Credential Handling: Reiterate: Use
.envfor local development only. In production, inject secrets securely via your hosting environment's mechanisms (e.g., AWS Secrets Manager, Parameter Store, platform environment variables). Never commit secrets to Git. - OTP Security Best Practices:
- Keep OTP expiry times reasonably short (e.g., 2-10 minutes).
- Use a cryptographically secure pseudo-random number generator (CSPRNG) like crypto.randomInt.
- Do not log the actual OTP value in production environments. Our example logs it but includes a comment warning against it. Remove or conditionally disable this logging based on NODE\_ENV.
- Ensure OTPs are single-use (our otpService.js deletes the OTP upon successful verification).
Handling special cases
- Phone Number Formatting: Strictly enforce and normalize phone numbers to the E.164 format (+ followed by country code and number, no spaces or symbols) before storing or sending to SNS. The validation regex /^\\+[1-9]\\d{1,14}$/ and checks in snsService.js help enforce this.
- Internationalization (i18n): The SMS message content (message variable in otpController.js) is currently hardcoded in English. For multi-language support, you would need a localization library (like i18next) and potentially pass a language preference from the client or detect it based on the phone number prefix to select the correct message template.
- SNS Delivery Failures: Monitor SNS delivery status logs (see Section 10 - Note: Section 10 was not provided in the input, but mentioned here). Failures can occur due to invalid numbers, carrier issues, destination country regulations, or exceeding spending limits. Implement application logic to handle these failures gracefully, potentially informing the user or triggering alerts.
- Time Zones: Use UTC for storing and comparing expiry times (Date.now() provides milliseconds since epoch, which is timezone-agnostic). If using a database, ensure TIMESTAMP WITH TIME ZONE data types are used to avoid ambiguity.
Implementing performance optimizations
- Persistent Data Store: Switching from the in-memory Map to Redis for OTP storage will significantly improve performance and scalability, especially under load, due to Redis's optimized in-memory operations and built-in TTL handling.
- AWS SDK Client Reuse: The SNSClient is instantiated once in snsService.js and reused for all sendSms calls. This avoids the overhead of creating a new client and establishing connections for every request.
- Asynchronous Operations: All potentially blocking I/O operations (like the snsClient.send call) correctly use async/await, preventing the Node.js event loop from being blocked and ensuring the server remains responsive.
Simpler Alternative: Send the Same SMS With SMSRoute (No-KYC, Direct Routes)
If you want to replace legacy aggregator SDK boilerplate, multi-week compliance queues, and opaque carrier surcharges with a single HTTP request, SMSRoute delivers global SMS over direct Tier-1 carrier interconnects with instant API access and transparent per-country pricing.
curl -X POST https://api.sendsmsnokyc.com/messages \
-H "x-api-key: $SMSROUTE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": ["+14155551234"],
"channel": ["sms"],
"body": "Your verification code is 482910"
}'
- Instant onboarding: No KYC paperwork or sales queues β grab an API key and fund with crypto (BTC/USDT) or card.
- Direct Tier-1 routes: Verify real handset delivery, DLR latency, and Sender ID preservation before launch via SMSRoute Live Carrier Testing.
- Transparent global pricing: Compare exact per-country termination rates on our live pricing page.
Ship Global SMS in One API Call
Skip legacy SDK sprawl and carrier bottlenecks. Send OTPs, alerts, and bulk campaigns across 190+ countries with SMSRoute.
Explore SMSRoute Developers β