Developer Quick-Start: A step-by-step guide for integrating AWS SNS into a NestJS application to send SMS messages, covering setup, implementation, security, and testing. 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 step-by-step walkthrough for integrating AWS Simple Notification Service (SNS) into a NestJS application to send SMS messages directly to phone numbers. We'll cover everything from project setup and AWS configuration to implementation, error handling, security, and testing.
By the end of this tutorial, you'll have a functional NestJS API endpoint capable of accepting a phone number and message, and using AWS SNS to deliver that message as an SMS.
Project Overview and Goals
What we're building: A simple NestJS application with a single API endpoint
(POST /sms/send) that accepts a phone number and a message body, then uses AWS SNS to send the
message as an SMS.
Problem solved: Provides a robust, scalable, and cloud-native way to programmatically send SMS messages (like OTPs, notifications, alerts) from your NestJS backend without managing complex telephony infrastructure.
Technologies used:
- NestJS: A progressive Node.js framework for building efficient, reliable, and scalable server-side applications using TypeScript. Chosen for its modular architecture, dependency injection, and built-in tooling.
- AWS SNS: A fully managed messaging service for both application-to-application (A2A) and application-to-person (A2P) communication. Chosen for its direct SMS sending capability, scalability, reliability, and integration with the AWS ecosystem.
- AWS SDK for JavaScript v3: The official AWS SDK for interacting with AWS services, including SNS, from Node.js/TypeScript applications.
- TypeScript: Superset of JavaScript adding static types, enhancing code quality and maintainability.
- dotenv / @nestjs/config: For managing environment variables securely.
System Architecture:
graph LR
Client[Client Application / Postman] -- HTTP POST Request --> API{NestJS API Endpoint (/sms/send)}
API -- Uses --> SmsService{SmsService (NestJS)}
SmsService -- Reads Config --> Config{Environment Variables (.env)}
SmsService -- Calls AWS SDK --> SNSClient[AWS SNS Client (@aws-sdk/client-sns)]
SNSClient -- Sends SMS Request --> AWS_SNS[AWS SNS Service]
AWS_SNS -- Delivers SMS --> Phone[(User's Phone)]
Config -- Stores --> AWSCreds[AWS Credentials & Region]
Prerequisites:
- Node.js (LTS version recommended, check AWS SDK v3 requirements)
- npm or yarn package manager
- An active AWS account
- AWS Access Key ID and Secret Access Key with permissions to use SNS (we'll cover creating this)
- A text editor or IDE (like VS Code)
- Basic understanding of TypeScript, NestJS, and REST APIs
- AWS CLI (Optional, but helpful for configuration and testing)
1. Setting up the NestJS Project
Let's start by creating a new NestJS project and installing the necessary dependencies.
- Create a new NestJS project: Open your terminal and run the Nest CLI command:
npx @nestjs/cli new nestjs-sns-sms
cd nestjs-sns-sms
Choose your preferred package manager (npm or yarn) when prompted.
- Install required dependencies: We need the AWS SDK v3 client for SNS, NestJS config module for environment variables, and class-validator/class-transformer for input validation.
npm install @aws-sdk/client-sns @nestjs/config class-validator class-transformer
yarn add @aws-sdk/client-sns @nestjs/config class-validator class-transformer
- @aws-sdk/client-sns: The modular AWS SDK v3 package specifically for SNS interactions.
- @nestjs/config: Handles environment variable loading and access in a structured way.
- class-validator & class-transformer: Used for validating incoming request data (DTOs).
- Configure Environment Variables: NestJS encourages using a
.envfile for environment-specific configurations, especially sensitive data like AWS credentials.
- Create a
.envfile in the project root (nestjs-sns-sms/.env):
.env
# .env
# AWS Credentials - Obtain from IAM User setup
AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
# AWS Region - Choose one that supports SMS, e.g., us-east-1
AWS_REGION=us-east-1
# Optional: Default SMS Type (Transactional or Promotional)
# Transactional is better for OTPs/critical alerts, may bypass DND
# Promotional is cheaper, better for marketing
AWS_SNS_DEFAULT_SMS_TYPE=Transactional
Important: Replace YOUR_AWS_ACCESS_KEY_ID and
YOUR_AWS_SECRET_ACCESS_KEY with the actual credentials you'll generate in the next step. Add this
.env file to your .gitignore to prevent committing secrets.
- Load the configuration module in
src/app.module.ts:
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import { SmsModule } from './sms/sms.module';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: '.env',
}),
SmsModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
ConfigModule.forRoot({ isGlobal: true }) makes the ConfigService available
throughout the application without needing to import ConfigModule everywhere.
2. AWS Setup: IAM User and SNS Configuration
To interact with AWS SNS securely, we need an IAM (Identity and Access Management) user with specific permissions.
- Create an IAM User:
- Log in to your AWS Management Console.
- Navigate to the IAM service.
- In the left navigation pane, click Users, then click Create user.
- Enter a User name (e.g., nestjs-sns-sender).
- Select Provide user access to the AWS Management Console - Optional (Only needed if this user needs console access. For programmatic access only, leave unchecked).
- Select I want to create an IAM user. If selected console access, choose a password method.
- Click Next.
- On the Set permissions page, select Attach policies directly.
- Best Practice (Production): Click Create policy. Use the JSON editor and provide a policy granting only the necessary permissions. This adheres to the principle of least privilege. A minimal policy requires sns:Publish. Optionally, add sns:SetSMSAttributes if setting type per message or other attributes, and sns:CheckIfPhoneNumberIsOptedOut if checking opt-out status. You can restrict the Resource from "\*" to specific topic ARNs if not sending directly to phones.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sns:Publish",
"sns:SetSMSAttributes",
"sns:CheckIfPhoneNumberIsOptedOut"
],
"Resource": "*"
}
]
}
Give the policy a name (e.g., NestJsSnsSmsPublishOnly) and create it. Then, back on the "Set permissions" page for the user, search for and attach this custom policy.
- Simpler Alternative (Guide/Testing): For simplicity during this guide or initial testing only, you can search for and select the AWS managed policy AmazonSNSFullAccess. Be aware this grants broad SNS permissions (publish, manage topics, subscriptions, etc.) and is not recommended for production.
- Click Next.
- Review the user details and permissions, then click Create user.
- Crucial Step: On the success page, click on the username you just created. Navigate 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. Click Create access key.
- Immediately copy the
Access key IDandSecret access key. The secret key is only shown once. Store them securely. - Paste these keys into your .env file for the AWS\_ACCESS\_KEY\_ID and AWS\_SECRET\_ACCESS\_KEY variables.
- Choose an AWS Region: Not all AWS regions support sending SMS messages directly via SNS.
Regions like
us-east-1(N. Virginia),us-west-2(Oregon),eu-west-1(Ireland), andap-southeast-1(Singapore) generally do. Check the AWS documentation for the latest list and ensure the region specified in your.env(AWS_REGION) supports SMS.us-east-1is often a safe default choice if you don't have specific region requirements. - Set Default SMS Type (Optional but Recommended): SNS allows you to optimize SMS delivery
for cost (
Promotional) or reliability (Transactional). Transactional messages have higher delivery priority and may bypass Do-Not-Disturb (DND) registries in some countries, making them suitable for critical alerts or OTPs.
- You can set this default for your entire AWS account in the chosen region.
- Navigate to the SNS service in the AWS Management Console.
- Make sure you are in the correct Region (the one specified in your .env file).
- In the left navigation pane, click Mobile -> Text messaging (SMS).
- Click Edit in the "Account spending limit and default message type" section.
- Under Default message type, select Transactional or Promotional.
- Set a Monthly SMS spend limit (USD) to prevent unexpected costs (e.g., 1.00 for testing).
- Click Save changes.
- Alternatively, we can set this attribute programmatically when sending a message if needed, or rely on the AWS\_SNS\_DEFAULT\_SMS\_TYPE variable from .env if we implement reading it later. For simplicity now, setting it in the console is sufficient.
3. Implementing the SMS Service
Now, let's create the core logic for sending SMS messages within our NestJS application.
- Generate the SMS Module and Service: Use the Nest CLI to generate a module and service for SMS functionality.
npx @nestjs/cli generate module sms
npx @nestjs/cli generate service sms --no-spec
This creates src/sms/sms.module.ts and src/sms/sms.service.ts. The
SmsModule was already imported into AppModule earlier.
- Implement the
SmsService: Opensrc/sms/sms.service.tsand add the logic to interact with AWS SNS.
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
SNSClient,
PublishCommand,
PublishCommandInput,
} from '@aws-sdk/client-sns';
import { AwsServiceUnavailableException } from './exceptions/aws-service-unavailable.exception';
@Injectable()
export class SmsService {
private readonly logger = new Logger(SmsService.name);
private readonly snsClient: SNSClient;
private readonly defaultSmsType: string;
constructor(private readonly configService: ConfigService) {
this.snsClient = new SNSClient({
region: this.configService.get<string>('AWS_REGION'),
credentials: {
accessKeyId: this.configService.get<string>('AWS_ACCESS_KEY_ID'),
secretAccessKey: this.configService.get<string>(
'AWS_SECRET_ACCESS_KEY',
),
},
});
this.logger.log('AWS SNS Client Initialized');
this.defaultSmsType = this.configService.get<string>(
'AWS_SNS_DEFAULT_SMS_TYPE',
'Transactional',
);
}
async sendSms(phoneNumber: string, message: string): Promise<string> {
const params: PublishCommandInput = {
PhoneNumber: phoneNumber,
Message: message,
MessageAttributes: {
'AWS.SNS.SMS.SMSType': {
DataType: 'String',
StringValue: this.defaultSmsType
}
}
};
try {
this.logger.log(
`Sending SMS to ${phoneNumber} with type ${this.defaultSmsType}`,
);
const command = new PublishCommand(params);
const response = await this.snsClient.send(command);
this.logger.log(`SMS sent successfully! Message ID: ${response.MessageId}`);
return response.MessageId;
} catch (error) {
this.logger.error(`Failed to send SMS to ${phoneNumber}`, error.stack);
throw new AwsServiceUnavailableException(
`Failed to send SMS via AWS SNS: ${error.message}`,
);
}
}
}
- Create a Custom Exception (Optional but Good Practice): Create a file
src/sms/exceptions/aws-service-unavailable.exception.ts:
import { HttpException, HttpStatus } from '@nestjs/common';
export class AwsServiceUnavailableException extends HttpException {
constructor(message?: string) {
super(
message || 'AWS Service is temporarily unavailable. Please try again later.',
HttpStatus.SERVICE_UNAVAILABLE,
);
}
}
This helps in providing a more specific HTTP status code if the SNS service fails. We'll need an exception filter later to handle this properly, or rely on NestJS defaults for now.
- Ensure
ConfigServiceis Available: Make sureConfigModuleis imported correctly insrc/app.module.tsand configured asisGlobal: true. TheSmsServiceuses@nestjs/config'sConfigServicevia dependency injection to securely retrieve the AWS credentials and region from the environment variables loaded from.env.
Explanation:
- The SNSClient is initialized in the constructor using credentials and region fetched from ConfigService.
- The sendSms method constructs the PublishCommandInput required by the AWS SDK v3.
- PhoneNumber: Must be in E.164 format (e.g., +12223334444). Validation is now expected to happen at the API layer (DTO).
- Message: The content of the SMS.
- MessageAttributes (Optional): We explicitly set the AWS.SNS.SMS.SMSType attribute here based on our configuration. This ensures the message is treated as Transactional or Promotional as intended.
- The snsClient.send() method sends the command to AWS SNS.
- Error handling is included using a try...catch block, logging errors and throwing a custom AwsServiceUnavailableException.
4. Building the API Layer
Let's expose the SMS sending functionality through a REST API endpoint.
- Generate the SMS Controller:
npx @nestjs/cli generate controller sms --no-spec
This creates src/sms/sms.controller.ts.
- Create a Data Transfer Object (DTO) for Validation: Create a file
src/sms/dto/send-sms.dto.tsto define the expected request body structure and apply validation rules.
import { IsNotEmpty, IsString, Matches, MaxLength } from 'class-validator';
export class SendSmsDto {
@IsString()
@IsNotEmpty()
@Matches(/^\+[1-9]\d{1,14}$/, {
message: 'Phone number must be in E.164 format (e.g., +12065550100)',
})
phoneNumber: string;
@IsString()
@IsNotEmpty()
@MaxLength(1600)
message: string;
}
- @IsString(), @IsNotEmpty(): Ensures the fields are non-empty strings.
- @Matches(): Validates the phoneNumber against the E.164 regex pattern.
- @MaxLength(): Basic check for message length (SNS has limits, typically 140 bytes for GSM-7, less for UCS-2).
- Implement the
SmsController: Opensrc/sms/sms.controller.tsand define the endpoint.
import {
Controller,
Post,
Body,
HttpCode,
HttpStatus,
Logger,
UsePipes,
} from '@nestjs/common';
import { SmsService } from './sms.service';
import { SendSmsDto } from './dto/send-sms.dto';
import { AwsServiceUnavailableException } from './exceptions/aws-service-unavailable.exception';
@Controller('sms')
export class SmsController {
private readonly logger = new Logger(SmsController.name);
constructor(private readonly smsService: SmsService) {}
@Post('send')
@HttpCode(HttpStatus.ACCEPTED)
async sendSms(
@Body() sendSmsDto: SendSmsDto,
): Promise<{ messageId: string; status: string }> {
this.logger.log(`Received request to send SMS to ${sendSmsDto.phoneNumber}`);
try {
const messageId = await this.smsService.sendSms(
sendSmsDto.phoneNumber,
sendSmsDto.message,
);
return { messageId, status: 'SMS request accepted for delivery.' };
} catch (error) {
if (error instanceof AwsServiceUnavailableException) {
throw error;
}
this.logger.error('Unhandled error in sendSms endpoint', error);
throw new Error('An unexpected error occurred while processing the SMS request.');
}
}
}
- Enable Global Validation Pipe: For DTO validation to work automatically, enable the
ValidationPipeglobally insrc/main.ts. This is the recommended approach.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
const logger = new Logger('Bootstrap');
app.useGlobalPipes(new ValidationPipe({
whitelist: true,
transform: true,
forbidNonWhitelisted: true,
transformOptions: {
enableImplicitConversion: true,
},
}));
const port = configService.get<number>('PORT', 3000);
await app.listen(port);
logger.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();
Now, any incoming request to the sendSms endpoint will have its body automatically validated
against the SendSmsDto. If validation fails, NestJS will return a 400 Bad Request response
automatically.
API Endpoint Testing:
You can now test the endpoint using curl or Postman. Make sure your NestJS application is
running (npm run start:dev).
- Using
curl:
curl --location --request POST 'http://localhost:3000/sms/send' \
--header 'Content-Type: application/json' \
--data-raw '{
"phoneNumber": "+12065550100",
"message": "Hello from NestJS and AWS SNS! (Test)"
}'
(Replace +12065550100 with a valid E.164 test phone number)
- Expected Success Response (202 Accepted):
{
"messageId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"status": "SMS request accepted for delivery."
}
- Expected Validation Error Response (400 Bad Request): If you send an invalid phone number format:
{
"message": [
"Phone number must be in E.164 format (e.g., +12065550100)"
],
"error": "Bad Request",
"statusCode": 400
}
5. Error Handling and Logging
We've already implemented basic logging and error handling, but let's refine it.
- Logging: NestJS's built-in Logger is used in both the service and controller. It logs information about initialization, incoming requests, successful sends, and errors. In a production environment, you'd typically configure more robust logging (e.g., JSON format, sending logs to CloudWatch or another aggregation service).
- Specific Error Handling: The SmsService catches errors from the snsClient.send() call. It logs the error stack and throws a custom AwsServiceUnavailableException. The controller catches this specific exception and re-throws it, allowing NestJS's default exception filter (or a custom one) to handle generating the 503 response.
- Why
HttpStatus.ACCEPTED(202)? SNS Publish is asynchronous. A successful API call means SNS accepted the request, not that the SMS was delivered. Returning 202 reflects this. Delivery status can be tracked via SNS Delivery Status Logging (an advanced topic). - Why
AwsServiceUnavailableException(503)? If we fail to communicate with SNS due to network issues, credential problems caught late, or throttling on the AWS side, it indicates our service's dependency is unavailable. 503 is appropriate. Validation errors result in 400 Bad Request thanks to the global ValidationPipe. - Retry Mechanisms: AWS SDK v3 has built-in retry mechanisms with exponential backoff for many transient network errors or throttled requests. For critical operations, you might consider adding application-level retries using libraries like async-retry for specific error types if the default SDK behavior isn't sufficient, but often it is. For sending an SMS, if the initial Publish fails critically (e.g., invalid credentials), retrying won't help. If it's throttling, the SDK handles it.
6. Security Considerations
Securing the application and credentials is vital.
- Input Validation: Done via class-validator in the SendSmsDto and enforced by the global ValidationPipe. This prevents invalid data from reaching the service layer and mitigates risks like injection attacks if the message content were used insecurely elsewhere (though less likely for SMS).
- Secure Credential Management:
- AWS credentials (Access Key ID, Secret Access Key) are stored in the .env file for local development.
- Crucially, ensure
.envis listed in your.gitignorefile to prevent accidentally committing secrets to version control. - In production environments, avoid storing credentials directly in files. Use more secure methods like:
- IAM Roles for EC2/ECS/Lambda: If deploying on AWS compute services, assign an IAM Role with the required SNS permissions to the instance/task/function. The SDK will automatically retrieve temporary credentials. This is the most secure method.
- AWS Secrets Manager or Parameter Store: Store credentials securely and fetch them at runtime.
- Environment Variables in Deployment Platform: Platforms like Heroku, Vercel, or CI/CD systems provide secure ways to inject environment variables.
- Rate Limiting: Protect your API from abuse and control costs by implementing rate limiting.
- Install the throttler module:
npm install --save @nestjs/throttler
yarn add @nestjs/throttler
- Configure it in src/app.module.ts:
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import { SmsModule } from './sms/sms.module';
import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler';
import { APP_GUARD } from '@nestjs/core';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true, envFilePath: '.env' }),
ThrottlerModule.forRoot([{
ttl: 60000,
limit: 10,
}]),
SmsModule,
],
controllers: [AppController],
providers: [
AppService,
{
provide: APP_GUARD,
useClass: ThrottlerGuard,
},
],
})
export class AppModule {}
This configuration limits each IP address to 10 requests per 60 seconds across the entire application. You can apply more granular limits per-route if needed.
- IAM Least Privilege: As mentioned in Section 2, ensure the IAM user or role used has only the permissions required (sns:Publish, potentially sns:SetSMSAttributes, sns:CheckIfPhoneNumberIsOptedOut if used) and not broad permissions like AmazonSNSFullAccess, especially in production.
7. Testing
Testing ensures the different parts of the application work correctly.
- Unit Testing
SmsService: Mock theSNSClientto avoid making actual AWS calls.
import { Test, TestingModule } from '@nestjs/testing';
import { SmsService } from './sms.service';
import { ConfigService } from '@nestjs/config';
import { SNSClient, PublishCommand } from '@aws-sdk/client-sns';
import { AwsServiceUnavailableException } from './exceptions/aws-service-unavailable.exception';
import { mockClient } from 'aws-sdk-client-mock';
import 'aws-sdk-client-mock-jest';
const mockConfigService = {
get: jest.fn((key: string, defaultValue?: any) => {
if (key === 'AWS_REGION') return 'us-east-1';
if (key === 'AWS_ACCESS_KEY_ID') return 'test-key-id';
if (key === 'AWS_SECRET_ACCESS_KEY') return 'test-secret-key';
if (key === 'AWS_SNS_DEFAULT_SMS_TYPE') return 'Transactional';
return defaultValue;
}),
};
const snsMock = mockClient(SNSClient);
describe('SmsService', () => {
let service: SmsService;
beforeEach(async () => {
snsMock.reset();
const module: TestingModule = await Test.createTestingModule({
providers: [
SmsService,
{ provide: ConfigService, useValue: mockConfigService },
],
}).compile();
service = module.get<SmsService>(SmsService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
it('should send an SMS successfully', async () => {
const phoneNumber = '+15551234567';
const message = 'Test message';
const expectedMessageId = 'mock-message-id-123';
snsMock.on(PublishCommand).resolves({ MessageId: expectedMessageId });
const messageId = await service.sendSms(phoneNumber, message);
expect(messageId).toEqual(expectedMessageId);
expect(snsMock).toHaveReceivedCommandWith(PublishCommand, {
PhoneNumber: phoneNumber,
Message: message,
MessageAttributes: {
'AWS.SNS.SMS.SMSType': { DataType: 'String', StringValue: 'Transactional' },
},
});
});
it('should throw AwsServiceUnavailableException on SNS error', async () => {
const phoneNumber = '+15551234567';
const message = 'Test message';
const errorMessage = 'SNS simulated error';
snsMock.on(PublishCommand).rejects(new Error(errorMessage));
await expect(service.sendSms(phoneNumber, message))
.rejects.toThrow(new AwsServiceUnavailableException(`Failed to send SMS via AWS SNS: ${errorMessage}`));
});
});
(Note: You'll need to install aws-sdk-client-mock and potentially @types/jest:
npm install --save-dev aws-sdk-client-mock jest @types/jest ts-jest or
yarn add --dev aws-sdk-client-mock jest @types/jest ts-jest and configure Jest if not already
set up by Nest CLI)
- Unit Testing
SmsController: Mock theSmsServiceand any global guards applied.
import { Test, TestingModule } from '@nestjs/testing';
import { SmsController } from './sms.controller';
import { SmsService } from './sms.service';
import { SendSmsDto } from './dto/send-sms.dto';
import { AwsServiceUnavailableException } from './exceptions/aws-service-unavailable.exception';
import { ThrottlerGuard } from '@nestjs/throttler';
const mockSmsService = {
sendSms: jest.fn(),
};
describe('SmsController', () => {
let controller: SmsController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [SmsController],
providers: [
{ provide: SmsService, useValue: mockSmsService },
{
provide: ThrottlerGuard,
useValue: { canActivate: jest.fn(() => true) },
},
],
})
.compile();
controller = module.get<SmsController>(SmsController);
jest.clearAllMocks();
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
it('should call SmsService.sendSms and return messageId on success', async () => {
const sendSmsDto: SendSmsDto = {
phoneNumber: '+15559876543',
message: 'Controller test',
};
const expectedMessageId = 'controller-mock-id-456';
const expectedStatus = 'SMS request accepted for delivery.';
mockSmsService.sendSms.mockResolvedValue(expectedMessageId);
const result = await controller.sendSms(sendSmsDto);
expect(mockSmsService.sendSms).toHaveBeenCalledWith(
sendSmsDto.phoneNumber,
sendSmsDto.message,
);
expect(result).toEqual({ messageId: expectedMessageId, status: expectedStatus });
});
it('should re-throw AwsServiceUnavailableException from service', async () => {
const sendSmsDto: SendSmsDto = {
phoneNumber: '+15559876543',
message: 'Controller error test',
};
const exception = new AwsServiceUnavailableException('Service layer error');
mockSmsService.sendSms.mockRejectedValue(exception);
await expect(controller.sendSms(sendSmsDto)).rejects.toThrow(exception);
expect(mockSmsService.sendSms).toHaveBeenCalledWith(
sendSmsDto.phoneNumber,
sendSmsDto.message,
);
});
it('should throw a generic error for unexpected service errors', async () => {
const sendSmsDto: SendSmsDto = {
phoneNumber: '+15559876543',
message: 'Controller generic error test',
};
const genericError = new Error('Some unexpected service failure');
mockSmsService.sendSms.mockRejectedValue(genericError);
await expect(controller.sendSms(sendSmsDto)).rejects.toThrow(
'An unexpected error occurred while processing the SMS request.',
);
expect(mockSmsService.sendSms).toHaveBeenCalledWith(
sendSmsDto.phoneNumber,
sendSmsDto.message,
);
});
});
- End-to-End (E2E) Testing: Use NestJS's built-in E2E testing capabilities
(
supertest) to test the entire flow from HTTP request to response, including validation, controller logic, and potentially mocking the AWS SDK at a higher level or using tools like LocalStack for local AWS emulation. E2E tests provide the highest confidence but are slower and more complex to set up.
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 β