Developer Quick-Start: A step-by-step guide on integrating AWS Simple Notification Service (SNS) into a RedwoodJS application to send SMS messages using the AWS SDK v3. 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 your RedwoodJS application to send SMS messages. We'll build a simple backend service and GraphQL mutation to trigger SMS sending via the AWS SDK for JavaScript v3.
By the end of this tutorial, you will have a RedwoodJS API capable of sending transactional or promotional SMS messages to valid phone numbers using your AWS account, complete with basic error handling and secure credential management.
Project Overview and Goals
What We're Building
We will add functionality to a standard RedwoodJS application enabling it to send SMS messages via AWS SNS. This involves:
- Setting up AWS credentials and permissions.
- Creating a RedwoodJS service to interact with the AWS SNS API.
- Defining a GraphQL mutation to expose this functionality.
- Implementing basic validation and error handling.
Problem Solved
This guide addresses the common need for applications to send SMS notifications, one-time passcodes (OTPs), alerts, or other text messages directly to users' phones reliably and scalably using cloud infrastructure.
Technologies Used
- RedwoodJS: A full-stack JavaScript/TypeScript framework for the web. We'll use its API structure (GraphQL, services) and conventions.
- Node.js: The runtime environment for the RedwoodJS API side.
- AWS SDK for JavaScript v3 (
@aws-sdk/client-sns): The official AWS library for interacting with SNS from Node.js. - AWS SNS (Simple Notification Service): The AWS managed service used to send messages, including SMS.
- GraphQL: The query language used by RedwoodJS for API interactions.
- Yarn: Package manager for the project.
System Architecture
graph LR
A[User/Client] -- GraphQL Mutation --> B(RedwoodJS GraphQL API);
B -- Calls Service --> C(RedwoodJS SMS Service);
C -- Uses AWS SDK --> D(AWS SNS API);
D -- Sends SMS --> E(User's Phone);
Prerequisites
- Node.js (v18 or later recommended)
- Yarn (v1 or v3+)
- RedwoodJS CLI installed (yarn global add redwoodjs/cli)
- An active AWS Account
- Basic familiarity with RedwoodJS, TypeScript, GraphQL, and the AWS Management Console.
1. Setting Up the RedwoodJS Project
First, let's create a new RedwoodJS project if you don't already have one.
yarn create redwood-app redwood-sns-sms --typescript
cd redwood-sns-sms
Next, we need to install the AWS SDK v3 package for SNS within the API workspace.
yarn workspace api add @aws-sdk/client-sns
Finally, prepare the environment file to store AWS credentials securely. RedwoodJS automatically loads
variables from .env files.
Create a file named ` .env ` in the root directory of your project:
.env
# .env
# AWS Credentials - Obtain from IAM User setup (Section 2)
AWS_ACCESS_KEY_ID=""YOUR_AWS_ACCESS_KEY_ID""
AWS_SECRET_ACCESS_KEY=""YOUR_AWS_SECRET_ACCESS_KEY""
# AWS Region where SNS SMS is supported and you want to operate
# e.g., us-east-1, us-west-2, ap-southeast-1, eu-west-1
AWS_REGION=""us-east-1""
Important: Add
` .env to your .gitignore ` file if it's
not already there to prevent accidentally committing your credentials.
2. Integrating with AWS SNS: IAM Setup
Before writing code to interact with AWS, we need an IAM (Identity and Access Management) user with the necessary permissions to send SMS messages via SNS. This guide uses IAM User access keys, suitable for local development or applications running outside AWS.
(Note: If your application will be hosted within AWS, such as on EC2, ECS, or Lambda, consider using IAM Roles instead of IAM Users. IAM Roles provide temporary credentials automatically, avoiding the need to manage long-lived access keys.)
Steps to Create an IAM User
- Navigate to IAM: Log in to your AWS Management Console and navigate to the IAM service.
- Create User:
- In the left navigation pane, click on Users.
- Click the Create user button.
- User name: Enter a descriptive name, e.g., redwood-sns-sender.
- Provide user access to the AWS Management Console: Leave this unchecked unless you specifically need console access for this user (not required for API access). Click Next.
- Set Permissions:
- Select Attach policies directly.
- In the search box, type AmazonSNSFullAccess.
- Check the box next to the AmazonSNSFullAccess policy.
- Security Best Practice: While AmazonSNSFullAccess is used here for simplicity, it grants broad permissions. For production environments, it is highly recommended to create a custom IAM policy granting only the sns:Publish permission. This adheres to the principle of least privilege. A minimal policy might look like this:
{
""Version"": ""2012-10-17"",
""Statement"": [
{
""Effect"": ""Allow"",
""Action"": ""sns:Publish"",
""Resource"": ""*""
}
]
}
(You can further restrict the Resource if needed, e.g., to specific regions or topics, although
sns:Publish to phone numbers often requires Resource: ""*"").
- Click Next.
- Review and Create:
- Review the user details and permissions.
- Click Create user.
- Retrieve Credentials:
- On the success screen, click on the username you just created (e.g., redwood-sns-sender).
- Go to the Security credentials tab.
- Scroll down to the Access keys section and click Create access key.
- Select Application running outside AWS as the use case (or Command Line Interface (CLI) if appropriate).
- Click Next.
- (Optional) Add a description tag, e.g., RedwoodJS SNS Sender Key.
- Click Create access key.
- Crucial: This is your only chance to view the Secret access key. Copy both the Access key ID and the
Secret access key and paste them into your \
.env\file created in Step 1. Download the .csv file as a backup if needed, but store it securely.
Environment Variable Explanation
- AWS\_ACCESS\_KEY\_ID: The access key ID for the IAM user you created. Identifies the user making the API request.
- AWS\_SECRET\_ACCESS\_KEY: The secret key associated with the Access Key ID. Used to sign API requests, proving identity. Keep this secret!
- AWS\_REGION: The AWS region where your SNS resources will be managed and from which SMS messages will originate. SNS SMS support varies by region. Check the AWS Region Table under ""SNS"" for SMS support. us-east-1 is a common choice. The AWS SDK will automatically use these environment variables if not configured explicitly in the code.
3. Implementing Core Functionality: RedwoodJS Service
Now, let's create the RedwoodJS service that will contain the logic for sending SMS messages.
yarn rw g service sms --tests
This command creates two files:
- \
api/src/services/sms/sms.ts\: Where our core logic will reside. - \
api/src/services/sms/sms.test.ts\: For writing unit tests.
Open ` api/src/services/sms/sms.ts ` and replace its contents with the following:
import { SNSClient, PublishCommand } from '@aws-sdk/client-sns'
import type { PublishCommandInput, PublishCommandOutput } from '@aws-sdk/client-sns'
import { UserInputError, RedwoodError } from '@redwoodjs/graphql-server'
import { logger } from 'src/lib/logger'
const snsClient = new SNSClient({})
const isValidE164 = (phoneNumber: string): boolean => {
const pattern = /^\+[1-9]\d{9,14}$/
return typeof phoneNumber === 'string' && pattern.test(phoneNumber)
}
interface SendSmsParams {
to: string
message: string
messageType?: 'Promotional' | 'Transactional'
}
interface SendSmsResult {
success: boolean
messageId?: string
}
export const sendSms = async ({
to,
message,
messageType,
}: SendSmsParams): Promise<Omit<SendSmsResult, 'error'>> => {
logger.info({ custom: { to, messageType } }, 'Attempting to send SMS via SNS')
if (!isValidE164(to)) {
logger.error({ custom: { to } }, 'Invalid E.164 phone number format provided.')
throw new UserInputError('Invalid phone number format. Must be E.164 (e.g., +15551234567).')
}
if (!message || typeof message !== 'string' || message.trim().length === 0) {
logger.error('SMS message body cannot be empty.')
throw new UserInputError('SMS message body cannot be empty.')
}
const params: PublishCommandInput = {
PhoneNumber: to,
Message: message,
}
if (messageType) {
params.MessageAttributes = {
'AWS.SNS.SMS.SMSType': {
DataType: 'String',
StringValue: messageType,
},
}
}
try {
const command = new PublishCommand(params)
const data: PublishCommandOutput = await snsClient.send(command)
logger.info(
{ custom: { messageId: data.MessageId, to } },
'SMS sent successfully via SNS'
)
return { success: true, messageId: data.MessageId }
} catch (error) {
logger.error({ custom: { error, to } }, 'Failed to send SMS via SNS')
throw new RedwoodError('Failed to send SMS. Please try again later.')
}
}
Code Explanation
- Imports: We import necessary components from \
@aws-sdk/client-sns\, RedwoodJS error classes (\UserInputError\, \RedwoodError\), and the RedwoodJS logger. - SNS Client: \
new SNSClient({})\creates an SNS client instance. Because we set the standard AWS environment variables (\AWS\_ACCESS\_KEY\_ID\, \AWS\_SECRET\_ACCESS\_KEY\, \AWS\_REGION\), the SDK automatically configures itself. isValidE164: A helper function performs a regular expression check to validate the phone number format required by SNS (starts with +, followed by 10-15 digits). A comment notes its basic nature.sendSmsFunction:
- Accepts an object with to, message, and optional messageType.
- Input Validation: Checks if the to number appears valid E.164 format and if the message is not empty.
Throws \
UserInputError\for invalid inputs, which translates well to GraphQL errors. - Prepare Params: Creates the \
PublishCommandInput\object required by the SDK, including PhoneNumber and Message. - Message Type (Optional): If messageType is provided, it adds MessageAttributes to the parameters to specify 'Transactional' or 'Promotional'. Transactional messages generally have higher delivery rates but may cost slightly more. See AWS SNS documentation for details.
- Send via SNS: It wraps the \
snsClient.send(command)\call in a try...catch block. - Success: If successful, logs the success with the returned MessageId and returns a success object (without an error field).
- Error: If an error occurs, it logs the detailed error and throws a generic \
RedwoodError\to the client, signaling failure via the GraphQL error mechanism.
4. Building the API Layer: RedwoodJS GraphQL
Now, let's expose our ` sendSms ` service function through the RedwoodJS GraphQL
API.
yarn rw g sdl sms
This creates ` api/src/graphql/sms.sdl.ts `. Open this file and define the mutation
and its response type:
export const schema = gql`
type SendSmsResponse {
success: Boolean!
messageId: String
}
type Mutation {
""""""
Sends an SMS message to the specified E.164 phone number via AWS SNS.
""""""
sendSms(
to: String!
message: String!
messageType: String
): SendSmsResponse! @requireAuth
}
`
SDL Explanation
SendSmsResponseType: Defines the structure of the data returned by the mutation on success. It includes a success flag and the optional messageId from SNS. The error field has been removed to align with the service implementation which throws errors.MutationType:
- Defines the \
sendSms\mutation. - Specifies input arguments: to (required String), message (required String), and messageType (optional String).
- Specifies the return type as SendSmsResponse! (non-nullable on success).
@requireAuthDirective: This RedwoodJS directive ensures only authenticated users can call this mutation. Remove or modify this based on your application's authentication needs.
RedwoodJS Magic: Redwood automatically connects the
` sendSms mutation defined in the SDL to the sendSms function exported from the corresponding service file ( api/src/services/sms/sms.ts `).
5. Error Handling, Logging, and Retries
Error Handling
- Input Validation: \
UserInputError\in the service provides clear GraphQL client feedback for invalid inputs. - SNS/SDK Errors: Generic \
RedwoodError\is thrown for backend errors, preventing internal detail exposure. Specific AWS SDK error codes can be inspected in the catch block for more granular server-side handling if needed. - GraphQL Errors: Redwood translates thrown errors (\
UserInputError\, \RedwoodError\) into standard GraphQL error responses.
Logging
- RedwoodJS's built-in \
logger\(\import { logger } from 'src/lib/logger'\) is used. - \
logger.info\logs standard operations; \logger.error\logs detailed failure information. - Structured logging (custom property) aids analysis in log management systems. Configure logger options in
\
api/src/lib/logger.ts\.
Retry Mechanisms
- AWS SDK: The SDK v3 has built-in retries for transient errors (e.g., throttling). This is often sufficient.
- Custom Retries: For critical messages or specific errors, implement custom retry logic (e.g., using
\
async-retry\) or queue messages for background processing (advanced).
6. Database Schema and Data Layer (Optional Extension)
To log SMS attempts or statuses, you could add a model to ` api/db/schema.prisma `:
Prisma
// api/db/schema.prisma
model SmsLog {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
to String
message String
messageType String? // 'Promotional' or 'Transactional'
status String // e.g., 'SENT', 'FAILED', 'PENDING'
snsMessageId String? @unique // Store the ID from SNS
errorCode String? // Store error code on failure
errorMessage String? // Store error message on failure
}
Migrations
yarn rw prisma migrate dev --name add_sms_log
Modify the
` sendSms service to create/update SmsLog `
records.
7. Adding Security Features
- Authentication: Use RedwoodJS auth (\
@requireAuth\) to protect the mutation. - Input Validation: Basic E.164 and message checks are implemented. Consider stricter validation or sanitization if needed.
- Credential Security: Use \
.env\locally and secure environment variable management in deployment. Never commit credentials. Ensure \.env\is in \.gitignore\. - Rate Limiting: Implement rate limiting (e.g., via API Gateway, GraphQL Shield, or custom logic) if the endpoint is public or prone to abuse.
- IAM Policy: Use a custom IAM policy with least privilege (\
sns:Publish\only) instead of AmazonSNSFullAccess in production (see Section 2).
8. Handling Special Cases
- E.164 Format: Strictly enforce the \
+<country\_code><number>\format. - Promotional vs. Transactional: Use 'Transactional' for critical messages (e.g., OTPs) for better
deliverability; use 'Promotional' for marketing to optimize costs. Set via \
MessageAttributes\. - Message Length & Encoding: Be mindful of SMS character limits (160 GSM-7, 70 UCS-2). SNS handles segmentation but costs may increase.
- Opt-Outs: SNS handles standard opt-outs (STOP). Check status with other SDK commands if needed.
- Sender ID: Custom alphanumeric Sender IDs (e.g., ""MyApp"") can be configured in SNS but may require registration depending on the country.
9. Implementing Performance Optimizations
- SNS Client Reuse: The code correctly reuses a single \
SNSClient\instance. - Batching: SNS Publish doesn't batch unique messages to unique phone numbers. For high volume, use parallel async calls (respecting limits) or publish to an SNS Topic if sending the same message to multiple subscribers.
- Asynchronous Operations: async/await prevents blocking the Node.js event loop during API calls.
10. Monitoring, Observability, and Analytics
- RedwoodJS Logging: Forward structured logs to a centralized service (CloudWatch Logs, Datadog, etc.).
- AWS CloudWatch Metrics: Monitor SNS metrics (\
NumberOfMessagesPublished\, \NumberOfNotificationsFailed\, etc.) and set alarms. - SNS Delivery Status Logging: Configure SNS to log detailed delivery status to CloudWatch Logs for deep troubleshooting.
- APM Tools: Use tools like Datadog, New Relic, Sentry for tracing application performance.
11. Troubleshooting and Caveats
InvalidParameterValueException: Often due to bad E.164 format or invalid message content.CredentialsError/AuthorizationErrorException: Incorrect/missing AWS keys/region in \.env\or insufficient IAM permissions.- Region Mismatch: Ensure \
.env\AWS\_REGION matches your intended operational region with SMS support. - SNS Sandbox Limitations: New accounts are sandboxed:
- Spending Limit: Low monthly limit (e.g., $1.00 USD).
- Verified Destinations: May only allow sending to numbers verified in your AWS account.
- Exit Sandbox: You must request removal from the sandbox via an AWS Support case to send to unverified numbers globally and lift the default spending limit. Check the SNS SMS Sandbox documentation.
- DND / Opt-Outs: Delivery may fail to opted-out numbers. 'Transactional' type can help but respect preferences.
- Carrier Filtering: Carriers might filter messages perceived as spam. Use clear content and consider registered Sender IDs.
12. Deployment and CI/CD
- Environment Variables: Securely manage AWS credentials (\
AWS\_ACCESS\_KEY\_ID\, \AWS\_SECRET\_ACCESS\_KEY\, \AWS\_REGION\) using your hosting provider's secrets management. Do not commit `.env`. - Deployment Commands: Use standard RedwoodJS commands (\
yarn rw build\, \yarn rw deploy <provider>\). Consult RedwoodJS Deployment Docs. - CI/CD: Integrate build, test (\
yarn rw test api\), and deploy steps. Securely inject environment variables. - Rollback: Understand your provider's rollback procedures.
13. Verification and Testing
Manual Verification
- Set correct AWS credentials and region in `
.env`. - Start dev server: `
yarn rw dev`. - Open GraphQL Playground (`
http://localhost:8911/graphql`). - Execute the
`
sendSmsmutation. **Important:** Replace+15551234567` with your own valid E.164 phone number. If you are still in the SNS Sandbox, this number might need to be verified in your AWS account first (see Section 11).
mutation SendTestSms {
sendSms(to: ""+15551234567"", message: ""Hello from RedwoodJS SNS Test!"") {
success
messageId
}
}
- Check Playground response for
success: trueand amessageId. - Verify SMS arrival on the target phone.
- Check `
yarn rw dev` console logs. - Test Failure Cases: Try invalid number formats, empty messages, or temporarily invalidate credentials to observe GraphQL error responses.
Automated Testing (Unit Tests)
Use the generated test file (` api/src/services/sms/sms.test.ts `) with SDK
mocking.
import { UserInputError, RedwoodError } from '@redwoodjs/graphql-server'
import { sendSms } from './sms'
const mockSend = vi.fn()
vi.mock('@aws-sdk/client-sns', async (importOriginal) => {
const originalModule = await importOriginal()
return {
...originalModule,
SNSClient: vi.fn(() => ({
send: mockSend,
})),
PublishCommand: vi.fn((input) => ({
input: input,
})),
}
})
describe('sms service', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('sends an SMS successfully with valid inputs', async () => {
const mockMessageId = 'mock-sns-message-id-123'
mockSend.mockResolvedValue({ MessageId: mockMessageId })
const result = await sendSms({
to: '+15551234567',
message: 'Test message',
})
expect(result.success).toBe(true)
expect(result.messageId).toBe(mockMessageId)
expect(mockSend).toHaveBeenCalledTimes(1)
const commandInstance = mockSend.mock.calls[0][0];
expect(commandInstance.input).toEqual({ PhoneNumber: '+15551234567', Message: 'Test message' })
})
it('sends an SMS with Transactional message type', async () => {
mockSend.mockResolvedValue({ MessageId: 'mock-id-trans' })
await sendSms({
to: '+15559876543',
message: 'Transactional Test',
messageType: 'Transactional',
})
expect(mockSend).toHaveBeenCalledTimes(1)
const commandInstance = mockSend.mock.calls[0][0];
expect(commandInstance.input.MessageAttributes).toEqual({
'AWS.SNS.SMS.SMSType': {
DataType: 'String',
StringValue: 'Transactional',
},
})
})
it('throws UserInputError for invalid E.164 phone number', async () => {
await expect(sendSms({ to: '12345', message: 'Test' })).rejects.toThrow(UserInputError)
await expect(sendSms({ to: '+123', message: 'Test' })).rejects.toThrow(UserInputError)
await expect(sendSms({ to: '+12345678901234567', message: 'Test'})).rejects.toThrow(UserInputError)
await expect(sendSms({ to: '15551234567', message: 'Test'})).rejects.toThrow(UserInputError)
await expect(sendSms({ to: '12345', message: 'Test' })).rejects.toThrow('Invalid phone number format')
expect(mockSend).not.toHaveBeenCalled()
})
it('throws UserInputError for empty message', async () => {
await expect(sendSms({ to: '+15551112222', message: ' ' })).rejects.toThrow(UserInputError)
await expect(sendSms({ to: '+15551112222', message: '' })).rejects.toThrow(UserInputError)
await expect(sendSms({ to: '+15551112222', message: ' ' })).rejects.toThrow('SMS message body cannot be empty')
expect(mockSend).not.toHaveBeenCalled()
})
it('throws RedwoodError when SNS client fails', async () => {
const snsError = new Error('SNS Failure')
snsError.name = 'SomeSNSError'
mockSend.mockRejectedValue(snsError)
await expect(
sendSms({ to: '+15553334444', message: 'Will fail' })
).rejects.toThrow(RedwoodError)
mockSend.mockRejectedValue(snsError);
await expect(
sendSms({ to: '+15553334444', message: 'Will fail' })
).rejects.toThrow('Failed to send SMS. Please try again later.')
expect(mockSend).toHaveBeenCalledTimes(2)
})
})
Run tests:
yarn rw test api
Verification Checklist
- AWS IAM User created (or Role considered).
- Access Keys/Region stored securely in \
.env\(or Role configured). - Custom IAM policy with \
sns:Publish\recommended/used for production. - \
@aws-sdk/client-sns\installed in api. - \
sms.ts\service created with \sendSms\logic. - \
sms.sdl.ts\created with \sendSms\mutation (no error field in response type). - Auth (\
@requireAuth\) configured appropriately. - E.164 validation implemented.
- Error handling (try...catch, logging, throwing \
UserInputError\/\RedwoodError\) implemented. - Able to send SMS via GraphQL Playground (respecting Sandbox limits/verification).
- Error cases handled gracefully in GraphQL.
- Unit tests pass (\
yarn rw test api\). - Environment variables configured in deployment.
Final Thoughts
You now have a functional integration for sending SMS messages from your RedwoodJS application using AWS SNS. This provides a scalable and reliable foundation. Remember to refine IAM permissions, monitor usage via CloudWatch, exit the SNS Sandbox for production sending, and enhance features as needed.
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 β