Real-Time SMS Delivery Status Notifications in NestJS with AWS SNS - code-examples -

Frequently Asked Questions

Use the AWS SDK v3 for SNS within a NestJS service. Create a PublishCommand with the recipient's phone number and message content, then use the SNSClient to send the command. Ensure your AWS credentials are configured correctly via environment variables, profiles, or IAM roles.
Real-time callbacks provide immediate feedback on whether an SMS message was successfully delivered, failed, or encountered an issue. This enables reliable workflows, improves user experience, and aids troubleshooting by providing insights into delivery issues.
AWS SNS needs a public HTTPS URL to push delivery status notifications to your application. This is how SNS communicates delivery updates asynchronously. During development, tools like Ngrok can create a tunnel to expose a local endpoint, but a stable public domain is essential for production.
Use 'Transactional' for mission-critical messages requiring high reliability, like one-time passwords or delivery confirmations. 'Promotional' is suitable for marketing or informational messages where slightly lower delivery rates are acceptable, often at a lower cost.
Ngrok's free tier provides temporary URLs and has limitations, making it suitable only for development and testing SMS delivery callbacks. For production, deploy your application with a stable public HTTPS domain for reliable callback handling.
Use the SetSMSAttributesCommand to configure your SNS SMS preferences. Set the `DeliveryStatusSuccessSamplingRate` to 100 and provide the ARN of your dedicated SNS topic for both `SuccessfulFeedbackRoleArn` and `FailureFeedbackRoleArn` parameters. This routes status updates to your topic.
The `sns-validator` library is crucial for verifying the authenticity and integrity of incoming SNS messages. It confirms that the message originated from AWS SNS, preventing security risks from spoofed or manipulated callbacks. Always validate the signature before processing the message content.
Create a secure NestJS controller endpoint publicly accessible via HTTPS. AWS SNS will POST delivery status notifications to this URL. Use `@Req() req: RawBodyRequest` along with the `rawBody: true` option in `main.ts` to get the raw body needed for signature verification with `sns-validator`.
The 'Message' field contains a JSON string with the actual delivery status details. It includes the original message ID, recipient number, status (SUCCESS, FAILURE, or other status strings), timestamp, and provider-specific information.
SNS sends various message types: 'SubscriptionConfirmation', 'Notification', and 'UnsubscribeConfirmation'. Use a switch statement to differentiate them. Confirm subscriptions via the SubscribeURL, process delivery updates from 'Notification' messages, and manage unsubscriptions as needed.
A 'SubscriptionConfirmation' signifies that AWS SNS is attempting to verify your callback endpoint. You *must* visit the provided SubscribeURL to activate the subscription. This confirms that you control the endpoint and authorizes SNS to send notifications there.
Validating the Topic ARN adds an extra layer of security. It ensures that the received notification originates from the expected SNS topic, reducing the risk of processing messages from unauthorized sources.
Do *not* process the message if validation fails. Return a 200 OK response to prevent SNS from retrying the invalid message repeatedly. Log the validation error internally for investigation.
Install necessary packages like `@aws-sdk/client-sns`, `@nestjs/config`, `class-validator`, `class-transformer`, and `sns-validator`. Set up environment variables for AWS credentials, region, and the callback topic ARN. Create a NestJS module and controller to handle sending messages and the callback logic.