Developer Quick-Start: A comprehensive guide on building a system to send bulk SMS messages using NestJS and AWS Simple Notification Service (SNS), covering setup, implementation, error handling, and optional database logging. 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.
This guide provides a complete walkthrough for building a robust system capable of sending bulk SMS messages using NestJS and AWS Simple Notification Service (SNS). We will cover everything from initial project setup to deployment and monitoring, enabling you to reliably send SMS messages at scale directly from your application.
This implementation addresses the need for programmatic, high-throughput SMS communication, often required for notifications, alerts, marketing campaigns, or multi-factor authentication. We chose NestJS for its structured, scalable architecture based on TypeScript and Node.js, and AWS SNS for its proven reliability, scalability, and cost-effectiveness in delivering SMS messages globally. By the end of this guide, you will have a functional API endpoint capable of accepting a list of phone numbers and a message, sending SMS to each recipient via SNS, and handling results and potential errors gracefully.
System Architecture:
+-------------+ +---------------------+ +-----------+ +-----------------+
| Client | ----> | NestJS API Server | ----> | AWS SNS | ----> | User Mobile Phones|
| (Web/Mobile)| | (Bulk SMS Endpoint) | | | | (SMS Recipients)|
+-------------+ +---------------------+ +-----------+ +-----------------+
| | |
| HTTP Request | AWS SDK Call | SMS Delivery
| (POST /sms/bulk-send) | (PublishCommand) |
| | |
v v v
+-------------+ +---------------------+ +-----------------+
| Request Body| | SnsService | | CloudWatch Logs |
| {numbers,msg}| | (Handles iteration | | & Metrics |
+-------------+ | & individual sends) | +-----------------+
+---------------------+
Prerequisites:
- An active AWS account with permissions to manage SNS and IAM.
- AWS Access Key ID and Secret Access Key configured for programmatic access. See AWS Docs: Managing access keys
- Node.js (LTS version recommended) and npm/yarn installed.
- Basic understanding of NestJS concepts (modules, controllers, services). See NestJS Docs
- Familiarity with TypeScript.
1. Project Setup
First, we establish the foundation for our NestJS application, installing necessary dependencies and configuring the environment.
- Install NestJS CLI: If you don't have it, install the NestJS command-line interface globally.
npm install -g @nestjs/cli
- Create New NestJS Project: Generate a new NestJS project.
nest new nestjs-sns-bulk-sms
cd nestjs-sns-bulk-sms
Choose your preferred package manager (npm or yarn) when prompted.
- Install Dependencies: We need the AWS SDK v3 for SNS, configuration management, and validation.
npm install @nestjs/config @aws-sdk/client-sns class-validator class-transformer
Or using yarn:
yarn add @nestjs/config @aws-sdk/client-sns class-validator class-transformer
- @nestjs/config: Handles environment variables securely.
- @aws-sdk/client-sns: The official AWS SDK v3 module for interacting with SNS.
- class-validator & class-transformer: Used for validating incoming request data (DTOs).
- Configure Environment Variables: Create a
.envfile in the project root to store AWS credentials and configuration. Never commit this file to version control.
.env
# .env
# --- AWS Credentials ---
# Replace these placeholder values with your actual AWS credentials.
AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
AWS_REGION=us-east-1 # Or your preferred AWS region
# --- SNS Configuration (Optional) ---
# Default SNS message type (Transactional for high reliability, Promotional for lower cost)
# See: https://docs.aws.amazon.com/sns/latest/dg/sms_publish-to-phone.html#sms_publish_sdk
SNS_DEFAULT_SENDER_ID=MyCompany # Optional: Custom Sender ID (requires registration in some countries)
SNS_DEFAULT_SMS_TYPE=Transactional # Or Promotional
- Important: Replace YOUR\_AWS\_ACCESS\_KEY\_ID and YOUR\_AWS\_SECRET\_ACCESS\_KEY with your actual AWS credentials.
- Choose the AWS region where you want to operate SNS. SMS sending availability and pricing vary by region.
- Load Environment Variables Globally: Modify
src/app.module.tsto load the.envfile and make configuration accessible throughout the application.
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { SmsModule } from './sms/sms.module';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: '.env',
}),
SmsModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
- Enable Global Validation Pipe: Configure automatic validation for incoming request bodies
using DTOs. Modify
src/main.ts.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe({
whitelist: true,
transform: true,
}));
await app.listen(3000);
console.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();
- Project Structure: We'll organize our SMS logic within a dedicated
smsmodule. Create the necessary folders:
mkdir src/sms
mkdir src/sms/dto
2. Implementing Core Functionality: The SNS Service
The SnsService will encapsulate the logic for interacting with AWS SNS, including initializing
the client and sending messages.
- Create the SNS Service File:
touch src/sms/sns.service.ts
- Implement the
SnsService:
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
SNSClient,
PublishCommand,
PublishCommandInput,
PublishCommandOutput,
SetSMSAttributesCommand,
} from '@aws-sdk/client-sns';
@Injectable()
export class SnsService {
private readonly logger = new Logger(SnsService.name);
private readonly snsClient: SNSClient;
private readonly defaultSmsType: string;
private readonly defaultSenderId?: string;
constructor(private configService: ConfigService) {
const region = this.configService.get<string>('AWS_REGION');
const accessKeyId = this.configService.get<string>('AWS_ACCESS_KEY_ID');
const secretAccessKey = this.configService.get<string>(
'AWS_SECRET_ACCESS_KEY',
);
if (!region || !accessKeyId || !secretAccessKey || accessKeyId === 'YOUR_AWS_ACCESS_KEY_ID' || secretAccessKey === 'YOUR_AWS_SECRET_ACCESS_KEY') {
throw new Error('AWS credentials or region not configured properly in .env. Ensure placeholders are replaced.');
}
this.snsClient = new SNSClient({
region: region,
credentials: {
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
},
});
this.defaultSmsType = this.configService.get<string>(
'SNS_DEFAULT_SMS_TYPE',
'Transactional',
);
this.defaultSenderId = this.configService.get<string>(
'SNS_DEFAULT_SENDER_ID',
);
this.logger.log(
`SNS Service initialized. Region: ${region}, Default SMS Type: ${this.defaultSmsType}`,
);
}
async sendBulkSms(phoneNumbers: string[], message: string) {
this.logger.log(
`Attempting to send SMS to ${phoneNumbers.length} numbers.`,
);
const results = await Promise.allSettled(
phoneNumbers.map((phoneNumber) => this.sendSingleSms(phoneNumber, message)),
);
const successfulSends: { phoneNumber: string; messageId: string }[] = [];
const failedSends: { phoneNumber: string; error: string }[] = [];
results.forEach((result, index) => {
const phoneNumber = phoneNumbers[index];
if (result.status === 'fulfilled') {
successfulSends.push({ phoneNumber, messageId: result.value.MessageId });
this.logger.log(`Successfully sent SMS to ${phoneNumber}`);
} else {
const errorMessage = result.reason instanceof Error ? result.reason.message : String(result.reason);
failedSends.push({ phoneNumber, error: errorMessage });
this.logger.error(
`Failed to send SMS to ${phoneNumber}: ${errorMessage}`,
result.reason instanceof Error ? result.reason.stack : undefined,
);
}
});
this.logger.log(
`Bulk SMS send complete. Success: ${successfulSends.length}, Failed: ${failedSends.length}`,
);
return {
successCount: successfulSends.length,
failCount: failedSends.length,
successful: successfulSends,
failed: failedSends,
};
}
private async sendSingleSms(
phoneNumber: string,
message: string,
): Promise<PublishCommandOutput> {
const params: PublishCommandInput = {
PhoneNumber: phoneNumber,
Message: message,
MessageAttributes: {
'AWS.SNS.SMS.SMSType': {
DataType: 'String',
StringValue: this.defaultSmsType,
},
...(this.defaultSenderId && {
'AWS.SNS.SMS.SenderID': {
DataType: 'String',
StringValue: this.defaultSenderId,
}
}),
},
};
const command = new PublishCommand(params);
try {
const data = await this.snsClient.send(command);
if (!data.MessageId) {
throw new Error('SNS did not return a MessageId');
}
return data;
} catch (error) {
this.logger.error(`SNS Publish error for ${phoneNumber}: ${error.message}`, error.stack);
throw error;
}
}
}
SNSClientInitialization: We create an instance of the SNSClient using credentials and region from ConfigService. An error is thrown if essential variables are missing or still contain placeholder values.sendBulkSmsMethod:- Accepts an array of phoneNumbers and the message.
- Uses Promise.allSettled to send messages concurrently. This is crucial because it allows individual sends to fail without stopping the entire batch, unlike Promise.all.
- Calls sendSingleSms for each number.
- Iterates through the results array (Promise.allSettled returns an array of objects describing the outcome of each promise: { status: 'fulfilled', value: ... } or { status: 'rejected', reason: ... }).
- Builds successfulSends and failedSends arrays based on the outcomes.
- Logs success or failure for each number.
- Returns a summary object containing counts and lists of successful and failed sends, including MessageIDs for successes and error reasons for failures.
sendSingleSmsMethod:- Constructs the PublishCommandInput including the PhoneNumber, Message, and crucial MessageAttributes.
- AWS.SNS.SMS.SMSType: Set to Transactional (default, optimizes for reliability) or Promotional (optimizes for cost) based on the .env variable.
- AWS.SNS.SMS.SenderID: Optionally included if SNS\_DEFAULT\_SENDER\_ID is set in .env. Note that Sender ID support and registration requirements vary by country. See AWS Docs: Sender ID
- Sends the command using this.snsClient.send().
- Includes basic logging and error handling, re-throwing the error to be caught by sendBulkSms.
3. Building the API Layer
We need a controller and a Data Transfer Object (DTO) to handle incoming API requests for sending bulk SMS.
- Create the DTO File:
touch src/sms/dto/send-bulk-sms.dto.ts
- Define the
SendBulkSmsDto: Useclass-validatordecorators to enforce input validation.
import {
IsArray,
IsString,
IsNotEmpty,
ArrayNotEmpty,
Matches,
MaxLength,
} from 'class-validator';
export class SendBulkSmsDto {
@IsArray()
@ArrayNotEmpty()
@IsString({ each: true })
@Matches(/^\+[1-9]\d{1,14}$/, {
each: true,
message: 'Each phone number must be in E.164 format (e.g., +12223334444)',
})
phoneNumbers: string[];
@IsString()
@IsNotEmpty()
@MaxLength(1600)
message: string;
}
- phoneNumbers: Must be a non-empty array of strings, and each string must conform to a basic E.164 format regex.
- message: Must be a non-empty string with a maximum length (SNS has limits, usually around 1600 bytes, but SMS segments are smaller β see Caveats).
- Create the Controller File:
touch src/sms/sms.controller.ts
- Implement the
SmsController:
import { Controller, Post, Body, HttpCode, HttpStatus, Logger } from '@nestjs/common';
import { SnsService } from './sns.service';
import { SendBulkSmsDto } from './dto/send-bulk-sms.dto';
@Controller('sms')
export class SmsController {
private readonly logger = new Logger(SmsController.name);
constructor(private readonly snsService: SnsService) {}
@Post('bulk-send')
@HttpCode(HttpStatus.ACCEPTED)
async sendBulkSms(@Body() sendBulkSmsDto: SendBulkSmsDto) {
this.logger.log(`Received bulk SMS request for ${sendBulkSmsDto.phoneNumbers.length} numbers.`);
const result = await this.snsService.sendBulkSms(
sendBulkSmsDto.phoneNumbers,
sendBulkSmsDto.message,
);
return result;
}
}
- Defines a route POST /sms/bulk-send.
- Injects SnsService.
- Uses the SendBulkSmsDto with the @Body() decorator, triggering validation via the global ValidationPipe.
- Sets HttpCode to 202 Accepted β this is appropriate as the request is accepted, but individual message delivery happens asynchronously and might fail later.
- Calls snsService.sendBulkSms with validated data.
- Returns the result summary from the service.
- Create and Configure the
SmsModule: Tie the controller and service together.
touch src/sms/sms.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { SnsService } from './sns.service';
import { SmsController } from './sms.controller';
@Module({
imports: [ConfigModule],
controllers: [SmsController],
providers: [SnsService],
})
export class SmsModule {}
- Ensure this module is imported in src/app.module.ts (as done in Step 1.5).
Testing the Endpoint:
You can now run the application (npm run start:dev) and test the endpoint using
curl or a tool like Postman:
curl -X POST http://localhost:3000/sms/bulk-send \
-H "Content-Type: application/json" \
-d '{
"phoneNumbers": ["+15551112222", "+15553334444", "+invalidNumber"],
"message": "Hello from NestJS SNS Bulk Sender!"
}'
Expected JSON Request:
{
"phoneNumbers": ["+15551112222", "+15553334444"],
"message": "This is a test message."
}
Example JSON Response (Success):
{
"successCount": 2,
"failCount": 0,
"successful": [
{ "phoneNumber": "+15551112222", "messageId": "uuid-for-sms-1" },
{ "phoneNumber": "+15553334444", "messageId": "uuid-for-sms-2" }
],
"failed": []
}
Example JSON Response (Partial Failure):
{
"successCount": 1,
"failCount": 1,
"successful": [
{
"phoneNumber": "+15551112222",
"messageId": "b1a2b3c4-abcd-efgh-ijkl-1234567890ab"
}
],
"failed": [
{
"phoneNumber": "+19998887777",
"error": "Invalid phone number format or destination is opted out"
}
]
}
4. Integrating with AWS SNS
This section focuses on the specific integration points covered in the SnsService setup.
- AWS Credentials:
- Obtaining Keys: Generate an Access Key ID and Secret Access Key for an IAM user in the AWS Management Console. Go to IAM > Users > [Your User] > Security credentials > Create access key.
- Secure Storage: Store these keys securely in the .env file in your project root. Do not hardcode them in your source code. Use environment variables in production environments (e.g., set via ECS Task Definitions, Lambda environment variables, EC2 instance profiles, or secrets management services like AWS Secrets Manager). Remember to replace the placeholder values in the .env file.
- Permissions: Ensure the IAM user associated with the keys has the necessary permissions. A minimal policy would be:
{
""Version"": ""2012-10-17"",
""Statement"": [
{
""Effect"": ""Allow"",
""Action"": ""sns:Publish"",
""Resource"": ""*""
}
]
}
Optionally add permissions for sns:SetSMSAttributes or sns:CheckIfPhoneNumberIsOptedOut if needed, restricting the Resource if possible. Apply the principle of least privilege.
- Configuration (
.env):
- AWS\_ACCESS\_KEY\_ID: Your IAM user's access key ID (must be replaced).
- AWS\_SECRET\_ACCESS\_KEY: Your IAM user's secret access key (must be replaced).
- AWS\_REGION: The AWS region where SNS will operate (e.g., us-east-1, eu-west-1). Must match the region configured in the SNSClient.
- SNS\_DEFAULT\_SMS\_TYPE: (Optional, defaults to Transactional) Sets the AWS.SNS.SMS.SMSType message attribute. Transactional prioritizes delivery speed and reliability, while Promotional prioritizes lower cost. Choose based on your use case.
- SNS\_DEFAULT\_SENDER\_ID: (Optional) A custom ID (alphanumeric, up to 11 chars; or a phone number you own) displayed on the recipient's device. Support varies by country, and registration might be required. See AWS Docs
- SNS Client Initialization (
SnsService): TheSNSClientfrom@aws-sdk/client-snsis instantiated with the region and credentials loaded viaConfigService. This ensures the SDK communicates with the correct AWS region and authenticates properly. - Fallback Mechanisms: AWS SNS is highly available, but consider application-level resilience:
- Retries: Implement retries for transient errors (see Section 5).
- Cross-Region Failover: For critical systems, you could potentially configure a secondary SNSClient instance pointing to a different region and failover if the primary region experiences issues (adds complexity).
- Alternative Providers: In extreme cases, integrating with a second SMS provider as a fallback could be considered, but significantly increases complexity.
5. Error Handling, Logging, and Retry Mechanisms
Robust error handling and logging are essential for a production system.
- Consistent Error Handling Strategy:
- Service Level: The SnsService catches errors during individual snsClient.send() calls within the sendSingleSms method. It logs the specific error and re-throws it. Promise.allSettled in sendBulkSms captures these rejections without halting the loop, and the results are processed to build the final summary.
- Controller Level: The SmsController relies on NestJS's built-in exception filters for unhandled errors (e.g., configuration issues during service initialization). Validation errors are handled globally by the ValidationPipe.
- Logging: Use the built-in Logger (@nestjs/common) for structured logging. Log key events: request received, bulk send initiated, individual send success/failure (with error details), bulk send completion summary.
- Logging Levels and Formats:
- Use this.logger.log() for informational messages.
- Use this.logger.error() for errors, including the error message and stack trace (error.stack).
- Use this.logger.warn() for potential issues (e.g., high failure rate).
- Use this.logger.debug() for verbose information useful during development (e.g., full SNS responses).
- Consider using a dedicated logging library (like pino with nestjs-pino) in production for better performance and structured JSON logging, which integrates well with log aggregation services (CloudWatch Logs, Datadog, Splunk).
- Retry Strategies:
- SNS Internal Retries: SNS itself has some internal retry mechanisms for transient delivery issues.
- Application-Level Retries: The current implementation doesn't include explicit application-level retries for the PublishCommand. For transient AWS SDK errors (e.g., network timeouts, throttling), you could add a simple retry loop within sendSingleSms:
let attempts = 0;
const maxAttempts = 3;
while (attempts < maxAttempts) {
try {
const data = await this.snsClient.send(command);
if (!data.MessageId) {
throw new Error('SNS did not return a MessageId');
}
return data;
} catch (error) {
attempts++;
const isRetryable = error.name === 'ThrottlingException' || error.name === 'EndpointConnectionError';
if (isRetryable && attempts < maxAttempts) {
const delay = Math.pow(2, attempts) * 100;
this.logger.warn(`Attempt ${attempts} failed for ${phoneNumber}, retrying in ${delay}ms... Error: ${error.message}`);
await new Promise(resolve => setTimeout(resolve, delay));
} else {
if (isRetryable) {
this.logger.warn(`SNS Publish failed after ${maxAttempts} attempts for ${phoneNumber}: ${error.message}`);
}
throw error;
}
}
}
throw new Error(`Retry logic failed unexpectedly after ${maxAttempts} attempts for ${phoneNumber}.`);
This adds complexity; evaluate if necessary based on observed errors.
- Queue-Based Retries (Advanced): For maximum robustness, especially with very large batches or frequent transient errors, decouple sending. The API endpoint could push each SMS job (phone number + message) onto an SQS queue. A separate NestJS worker process would consume jobs from the queue, attempt to send via SNS, and implement sophisticated retry logic (including dead-letter queues for persistent failures). This prevents API timeouts and handles failures more gracefully.
- Testing Error Scenarios:
- Provide invalid E.164 numbers in the request.
- Provide a message exceeding length limits.
- Temporarily revoke sns:Publish permissions from the IAM user to test authorization errors.
- Send to known opted-out numbers (if available for testing).
- Simulate throttling (hard to do reliably without high volume, but be aware of the ThrottlingException).
- Unit test error paths by mocking snsClient.send() to throw specific errors.
- Log Analysis: Use CloudWatch Logs Insights or your chosen log aggregation tool to query logs. Examples:
- Find all failed sends: fields @timestamp, @message | filter @message like /Failed to send SMS/
- Count errors per phone number: fields @timestamp, phoneNumber, error | filter @message like /Failed to send SMS to/ | parse @message /Failed to send SMS to (?<phoneNumber>\\+\\S+) \*: (?<error>.\*)/ | stats count(\*) by phoneNumber, error
- Monitor overall success/failure rates: fields successCount, failCount | filter @message like /Bulk SMS send complete/ | parse @message /Success: (?<successCount>\\d+), Failed: (?<failCount>\\d+)/ | stats sum(successCount), sum(failCount)
6. Creating a Database Schema and Data Layer (Optional Enhancement)
While not strictly necessary for sending, storing logs of sent messages provides valuable auditing, tracking, and debugging capabilities.
- Rationale: A database log allows you to:
- Audit exactly which messages were sent to whom and when.
- Track the AWS SNS MessageId for correlation with delivery status reports (if configured).
- Analyze send success/failure trends over time.
- Provide users with a history of sent messages.
- Technology Choice: PostgreSQL, MySQL, or even a NoSQL database like DynamoDB could be used. For simplicity, we'll outline a TypeORM setup with SQLite (suitable for development/small scale). For production, use PostgreSQL or MySQL.
- Install Dependencies (if not already present):
npm install @nestjs/typeorm typeorm sqlite3
Or using yarn:
yarn add @nestjs/typeorm typeorm sqlite3
- Entity Relationship Diagram (Simple):
+--------------------+
| SmsLog |
+--------------------+
| id (PK) | --> Auto-incrementing ID (or UUID)
| phoneNumber | --> Recipient phone number (string, indexed)
| messageContent | --> The message text (text)
| status | --> 'SUCCESS' | 'FAILED' (string, indexed)
| snsMessageId (nullable) | --> AWS SNS Message ID (string, indexed, unique if desired)
| failureReason (nullable) | --> Error message if status is 'FAILED' (text)
| createdAt | --> Timestamp (indexed)
+--------------------+
- Create
SmsLogEntity:
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, Index } from 'typeorm';
export enum SmsStatus {
SUCCESS = 'SUCCESS',
FAILED = 'FAILED',
}
@Entity('sms_logs')
export class SmsLog {
@PrimaryGeneratedColumn('uuid')
id: string;
@Index()
@Column()
phoneNumber: string;
@Column('text')
messageContent: string;
@Index()
@Column({
type: 'enum',
enum: SmsStatus,
})
status: SmsStatus;
@Index()
@Column({ nullable: true, unique: false })
snsMessageId?: string;
@Column('text', { nullable: true })
failureReason?: string;
@CreateDateColumn()
@Index()
createdAt: Date;
}
- Configure TypeORM: Update
src/app.module.ts(replace SQLite with your production DB config).
import { TypeOrmModule } from '@nestjs/typeorm';
import { SmsLog } from './sms/entities/sms-log.entity';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true, envFilePath: '.env' }),
TypeOrmModule.forRoot({
type: 'sqlite',
database: 'sms_logs.db',
entities: [SmsLog],
synchronize: true,
}),
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 β