Send SMS with Node.js, Fastify, and AWS SNS - code-examples -

Frequently Asked Questions

You can send SMS messages by creating a Fastify API endpoint that uses the AWS SDK for JavaScript v3 to interact with Amazon SNS. This involves setting up a POST route that takes the phone number and message as input and then uses the SNS service to send the SMS.
AWS SNS (Simple Notification Service) is used as the messaging service to handle sending the SMS messages. The Node.js application interacts with SNS using the AWS SDK, sending the message and target phone number to the SNS service, which then delivers the SMS.
Fastify is a high-performance, low-overhead web framework for Node.js, making it ideal for building efficient API endpoints. Its speed and ease of use make it a good choice for handling HTTP requests related to sending SMS messages through AWS SNS.
Create an IAM user in the AWS console with the `AmazonSNSFullAccess` policy (or a custom policy with `sns:Publish`) and generate access keys for this user. Store these keys securely in a `.env` file, which will be loaded by your Node.js application. Never commit this file to version control.
The phone number must be in E.164 format, which includes a plus sign (+) followed by the country code and subscriber number. For example, a US number would be +12065551212. Incorrect formatting will result in an `InvalidParameterException`.
The provided code example uses a try-catch block to handle errors from the AWS SDK. It specifically checks for common errors like `InvalidParameterException`, `AuthorizationErrorException`, `ThrottlingException`, and `PhoneNumberOptedOutException` to return appropriate status codes and messages.
The `sns.service.js` file contains the logic for interacting with AWS SNS. It encapsulates the AWS SDK calls, handles constructing the `PublishCommand`, and manages the sending of the SMS message. It also handles and re-throws any errors encountered during the process.
You can test the `/sms` endpoint using `curl` or a similar API testing tool like Postman. Send a POST request to `http://localhost:3000/sms` with a JSON payload containing the `phoneNumber` and `message` fields.
If a user replies "STOP" to an SMS message, AWS SNS marks the number as opted out, and you'll receive a `PhoneNumberOptedOutException` when trying to send to that number. You can use the `ListPhoneNumbersOptedOutCommand` in the AWS SDK to manage these opt-outs.
Use a least-privilege IAM policy, manage credentials securely using environment variables and a .gitignore file, validate input data, and implement rate limiting to prevent abuse and manage costs. Avoid hardcoding credentials in your source code.
While SNS SMS is available in multiple regions, `us-east-1` (N. Virginia) is generally a reliable choice for international SMS. You should always confirm the best region for your needs via official AWS documentation.
Yes, you can set a custom Sender ID using the `AWS.SNS.SMS.SenderID` message attribute. Be mindful of country-specific regulations, as some require pre-registration of sender IDs.
Transactional messages are for time-sensitive information like one-time passwords (OTPs) and have higher deliverability but potentially higher cost. Promotional messages are for marketing and other non-critical communications.
The application can be deployed to various platforms like AWS EC2, Elastic Beanstalk, ECS, Lambda, or other services such as Heroku or Vercel. Configure the necessary environment variables securely within the chosen platform's settings, rather than including your .env file.
You'll need Node.js (v18 or later recommended), npm or yarn, an AWS account, and basic understanding of Node.js, APIs, and using the command line.