Tutorials

Developer Guide: Implementing RedwoodJS AWS SNS Delivery Status Callbacks Using Node.js

A guide on integrating AWS SNS delivery status notifications into a RedwoodJS application using Node.js for real-time SMS updates.

JH
Jacky Hossein
β€’ Principal Telecommunications Architect β€’ Published Nov 7, 2026 β€’ 19 min read

Developer Quick-Start: A guide on integrating AWS SNS delivery status notifications into a RedwoodJS application using Node.js for real-time SMS updates. 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.

Get real-time updates on your SMS messages by integrating AWS Simple Notification Service (SNS) delivery status notifications directly into your RedwoodJS application.

This guide provides a step-by-step walkthrough for setting up a robust system to send SMS messages via AWS SNS and receive delivery status callbacks (e.g., ""delivered,"" ""failed"") at a dedicated endpoint within your RedwoodJS application. This enables you to track message success rates, trigger follow-up actions, and provide better feedback to your users.

We will build a system where:

  1. Your RedwoodJS application sends an SMS using the AWS SDK.
  2. AWS SNS attempts to deliver the message.
  3. SNS sends a status update (success or failure) via HTTPS to a specified endpoint in your RedwoodJS API.
  4. Your RedwoodJS API endpoint verifies the incoming notification, parses the status, and updates your database accordingly.

Project Overview and Goals

Goal: To create a reliable mechanism within a RedwoodJS application to send SMS messages through AWS SNS and receive real-time delivery status updates via HTTPS callbacks.

Problem Solved: Standard SNS publish calls for SMS only confirm that the request was accepted by AWS, not whether the message reached the end user's device. This solution provides crucial visibility into the actual delivery outcome.

Technologies Used:

  • RedwoodJS: Full-stack JavaScript framework for building web applications. We'll use its API side for the callback endpoint and services.
  • Node.js: The runtime environment for RedwoodJS's API side.
  • AWS SNS (Simple Notification Service): Managed messaging service for sending SMS, push notifications, and more.
  • AWS IAM (Identity and Access Management): To securely grant permissions for SNS actions.
  • Prisma: Database toolkit used by RedwoodJS for schema management and database access.
  • (Optional) PostgreSQL/SQLite: Database to store message status.

Architecture:

graph LR
    A[RedwoodJS App UI/Service] -- 1. Send SMS Request --> B(RedwoodJS SMS Service);
    B -- 2. Publish SMS via SDK --> C(AWS SNS);
    C -- 3. Delivers SMS --> D(End User Device);
    C -- 4. Sends Status Update (HTTPS POST) --> E(RedwoodJS Callback Function /snsCallback);
    E -- 5. Verify SNS Signature --> E;
    E -- 6. Parse Status & Update DB --> F(Database);

    subgraph AWS Cloud
        C
    end

    subgraph RedwoodJS API
        B
        E
        F
    end

    subgraph User Interaction
        A
        D
    end

Prerequisites:

  • Node.js (>=18.x recommended) and Yarn (>=1.22.x) installed.
  • RedwoodJS CLI installed (yarn global add redwoodjs/cli).
  • An AWS account with permissions to manage SNS and IAM.
  • AWS Access Key ID and Secret Access Key configured locally (e.g., via environment variables, AWS credentials file, or IAM role).
  • A publicly accessible URL for your RedwoodJS API (for receiving callbacks). Use ngrok during development or deploy to a platform like Render, Vercel, or Fly.io.

1. Setting up the AWS Environment

Before writing code, we need to configure AWS SNS to send delivery status notifications.

Step 1: Create an IAM Policy for SNS Delivery Status

SNS needs permission to publish logs or notifications related to delivery status. While CloudWatch Logs are an option, we'll configure direct HTTPS notifications. However, setting up basic logging permissions is still good practice and sometimes required by AWS console setup wizards.

  1. Navigate to the IAM service in your AWS Console.
  2. Go to Policies and click Create policy.
  3. Switch to the JSON tab and paste the following policy. This allows SNS to write logs to CloudWatch, which is often a prerequisite step in the console wizards even if we primarily use HTTPS callbacks later.
   {
       ""Version"": ""2012-10-17"",
       ""Statement"": [
           {
               ""Effect"": ""Allow"",
               ""Action"": [
                   ""logs:CreateLogGroup"",
                   ""logs:CreateLogStream"",
                   ""logs:PutLogEvents"",
                   ""logs:PutMetricFilter"",
                   ""logs:PutRetentionPolicy""
               ],
               ""Resource"": [
                   ""*""
               ]
           }
       ]
   }
  1. Click Next: Tags, Next: Review.
  2. Give the policy a name, e.g., SNSSMSDeliveryStatusPolicy.
  3. Click Create policy.

Step 2: Create an IAM Role for SNS

SNS will assume this role to gain the permissions defined in the policy.

  1. In IAM, go to Roles and click Create role.
  2. Select AWS service as the trusted entity type.
  3. Choose SNS from the ""Use cases for other AWS services"" dropdown.
  4. Click Next.
  5. Search for and select the SNSSMSDeliveryStatusPolicy you just created.
  6. Click Next.
  7. Give the role a name, e.g., SNSSMSDeliveryStatusRole.
  8. Click Create role. Note the ARN (Amazon Resource Name) of this role; you'll need it shortly.

Step 3: Configure SNS Text Messaging (SMS) Preferences

This is where you tell SNS how to report delivery statuses. The primary method for this guide is via an SNS Topic triggering an HTTPS endpoint, but configuring CloudWatch logging is also shown.

  1. Navigate to the Simple Notification Service (SNS) in your AWS Console.
  2. In the left navigation pane, click Text messaging (SMS).
  3. Click the Edit button in the ""Text messaging preferences"" section.
  4. Delivery status logging (Optional - for CloudWatch Logs):
  • If you want delivery status sent to CloudWatch Logs in addition to HTTPS callbacks, check the box Enable delivery status logging.
  • Enter the IAM role ARN for Success sample rate (%) from the role created in Step 2 (SNSSMSDeliveryStatusRole).
  • Enter the IAM role ARN for Failure sample rate (%) (same role).
  • Set the sample rates (e.g., 100% for both during development/testing, potentially lower in production to manage CloudWatch costs). Note: This section configures logging to CloudWatch. The crucial part for HTTPS callbacks follows.
  1. Crucial: Create an SNS Topic for Delivery Status Updates
  • This Topic will receive the delivery status events from SNS, which will then trigger our HTTPS endpoint.
  • Go to Topics in the SNS console (you might need to open this in a new tab).
  • Click Create topic.
  • Choose Standard.
  • Give it a name, e.g., sms-delivery-status-topic.
  • Leave other settings as default and click Create topic.
  • Important: Copy the ARN of this newly created topic. You'll need it below and in your application's environment variables.
  1. Configure Delivery status notifications (Recommended for HTTPS Callbacks):
  • Go back to the Text messaging (SMS) preferences tab/page and ensure you are still in Edit mode.
  • Scroll down to the Delivery status notifications section.
  • For Amazon SNS topic for successes, paste or select the ARN of the topic you just created (e.g., arn:aws:sns:us-east-1:123456789012:sms-delivery-status-topic).
  • For Amazon SNS topic for failures, paste or select the same topic ARN. This ensures both success and failure events are sent to your callback handler via this topic.
  1. Click Save changes at the bottom of the Text messaging preferences page.

Step 4: Obtain AWS Credentials

Ensure your AWS Access Key ID and Secret Access Key are available to your application. The recommended way is via environment variables:

  • AWS\_ACCESS\_KEY\_ID
  • AWS\_SECRET\_ACCESS\_KEY
  • AWS\_REGION (e.g., us-east-1)

You can set these in your RedwoodJS project's .env file.


2. Setting up the RedwoodJS Project

Step 1: Create a New RedwoodJS App (if needed)

yarn create redwood-app ./redwood-sns-callbacks
cd redwood-sns-callbacks

Step 2: Install Dependencies

We need the AWS SDK v3 for SNS and a validator for incoming SNS messages.

yarn workspace api add @aws-sdk/client-sns aws-sns-message-validator

Step 3: Configure Environment Variables

Add your AWS credentials and the SNS Topic ARN to your .env file (create it if it doesn't exist). Never commit .env files to version control.

.env

# .env
AWS_ACCESS_KEY_ID="YOUR_ACCESS_KEY_ID"
AWS_SECRET_ACCESS_KEY="YOUR_SECRET_ACCESS_KEY"
AWS_REGION="us-east-1" # Or your preferred region
SNS_DELIVERY_STATUS_TOPIC_ARN="arn:aws:sns:us-east-1:123456789012:sms-delivery-status-topic" # Replace with your ACTUAL topic ARN
  • Important:
  • You must replace "YOUR\_ACCESS\_KEY\_ID" and "YOUR\_SECRET\_ACCESS\_KEY" with your actual AWS credentials.
  • You must replace the entire example "arn:aws:sns:us-east-1:123456789012:sms-delivery-status-topic" with the actual ARN of the SNS topic you created in Step 1.3. Ensure the region (us-east-1 in the example) and account ID (123456789012 in the example) match your setup.
  • AWS_REGION: The AWS region where you configured SNS and the topic.

3. Implementing the Database Schema

We need a way to store the messages we send and track their status.

Step 1: Define the Prisma Schema

Open api/db/schema.prisma and add a model to store SMS details:

Prisma

// api/db/schema.prisma

datasource db {
  provider = ""postgresql"" // Or ""sqlite""
  url      = env(""DATABASE_URL"")
}

generator client {
  provider      = ""prisma-client-js""
  binaryTargets = ""native""
}

// Add this model
model SmsMessage {
  id            String   @id @default(cuid()) // Using CUID for IDs
  createdAt     DateTime @default(now())
  updatedAt     DateTime @updatedAt

  toPhoneNumber String
  messageBody   String
  awsMessageId  String?  @unique // The MessageId returned by SNS publish
  deliveryStatus String? // e.g., 'PENDING', 'DELIVERED', 'FAILED'
  statusPayload Json?    // Store the raw status payload from SNS
  sentSuccessfully Boolean @default(false) // Track if the initial publish call succeeded
}
  • awsMessageId: Stores the unique ID returned by SNS when the message is initially published. This is crucial for correlating callbacks.
  • deliveryStatus: Stores the final status received from the callback.
  • statusPayload: Stores the full JSON payload from the SNS notification for debugging or additional info.
  • sentSuccessfully: Indicates if the sns.publish call itself was successful.

Step 2: Apply Migrations

Generate and apply the database migration:

yarn rw prisma migrate dev --name add_sms_message

4. Implementing the SMS Sending Service

Encapsulate the logic for sending SMS messages within a RedwoodJS service.

Step 1: Generate the Service

yarn rw g service sms

Step 2: Implement the Sending Logic

Replace the contents of api/src/services/sms/sms.ts with the following:

import { SNSClient, PublishCommand } from '@aws-sdk/client-sns';
import type { PublishCommandInput } from '@aws-sdk/client-sns';
import { db } from 'src/lib/db';
import { logger } from 'src/lib/logger';

const region = process.env.AWS_REGION;
const snsClient = new SNSClient({ region });

interface SendSmsInput {
  toPhoneNumber: string;
  messageBody: string;
}

export const sendSms = async ({ toPhoneNumber, messageBody }: SendSmsInput) => {
  logger.info(
    `Attempting to send SMS to ${toPhoneNumber} via AWS SNS`
  );


  let smsRecord;
  try {
    smsRecord = await db.smsMessage.create({
      data: {
        toPhoneNumber,
        messageBody,
        deliveryStatus: 'PENDING',
      },
    });
    logger.debug({ custom: { smsRecordId: smsRecord.id } }, 'SMS record created');
  } catch (dbError) {
    logger.error({ custom: { dbError } }, 'Failed to create SMS record in DB');


    throw new Error('Database error creating SMS record.');
  }


  const params: PublishCommandInput = {
    Message: messageBody,
    PhoneNumber: toPhoneNumber,
    MessageAttributes: {





      'AWS.SNS.SMS.SMSType': {
        DataType: 'String',
        StringValue: 'Transactional',
      },
    },
  };


  try {
    const command = new PublishCommand(params);
    const data = await snsClient.send(command);

    logger.info(
      { custom: { awsMessageId: data.MessageId, smsRecordId: smsRecord.id } },
      'SMS successfully published to SNS'
    );


    const updatedRecord = await db.smsMessage.update({
      where: { id: smsRecord.id },
      data: {
        awsMessageId: data.MessageId,
        sentSuccessfully: true,
      },
    });

    return {
      success: true,
      message: 'SMS submitted to SNS successfully.',
      databaseId: updatedRecord.id,
      awsMessageId: data.MessageId,
    };
  } catch (error) {
    logger.error(
      { custom: { error, smsRecordId: smsRecord.id } },
      'Failed to publish SMS via SNS'
    );

    await db.smsMessage.update({
      where: { id: smsRecord.id },
      data: {
        deliveryStatus: 'PUBLISH_FAILED',
        sentSuccessfully: false,
      },
    }).catch(updateError => {
      logger.error({ custom: { updateError, smsRecordId: smsRecord.id }}, 'Failed to update SMS record after SNS publish failure');
    });

    return {
      success: false,
      message: 'Failed to submit SMS to SNS.',
      error: error.message,
      databaseId: smsRecord.id,
    };
  }
};

export const schema = gql`
  type SmsSendResponse {
    success: Boolean!
    message: String!
    databaseId: String
    awsMessageId: String
    error: String
  }

  type Mutation {
    sendSms(toPhoneNumber: String!, messageBody: String!): SmsSendResponse! @requireAuth
  }
`;
  • Database Interaction: Creates a record before sending and updates it with the awsMessageId on success or failure.
  • Error Handling: Logs errors and updates the database status if the SNS publish fails.
  • AWS SDK v3: Uses the modular SDK (@aws-sdk/client-sns).
  • SMSType: Set to 'Transactional' (for non-marketing) or 'Promotional'.

5. Implementing the Callback Handler Function

This is the core endpoint that AWS SNS will call with delivery status updates.

Step 1: Generate the Redwood Function

yarn rw g function snsCallback

This creates api/src/functions/snsCallback.ts.

Step 2: Implement the Handler Logic

Replace the contents of api/src/functions/snsCallback.ts with the following:

import type { APIGatewayEvent, Context } from 'aws-lambda';
import { MessageValidator } from 'aws-sns-message-validator';
import { db } from 'src/lib/db';
import { logger } from 'src/lib/logger';

const validator = new MessageValidator();

interface SnsNotificationPayload {
  Type: 'SubscriptionConfirmation' | 'Notification' | 'UnsubscribeConfirmation';
  MessageId: string;
  Token?: string;
  TopicArn: string;
  Subject?: string;
  Message: string;
  SubscribeURL?: string;
  Timestamp: string;
  SignatureVersion: string;
  Signature: string;
  SigningCertURL: string;
}

interface DeliveryStatusMessage {
  notification: {
    messageId: string;
    timestamp: string;
  };
  delivery: {
    phoneCarrier: string;
    mnc: number;
    destination: string;
    priceInUSD: number;
    smsType: string;
    mcc: number;
    providerResponse: string;
    dwellTimeMs: number;
    dwellTimeMsUntilDeviceAck?: number;
  };
  status: 'SUCCESS' | 'FAILURE';
}

export const handler = async (event: APIGatewayEvent, _context: Context) => {
  logger.debug({ custom: event.headers }, 'Received request on /snsCallback');

  if (!event.body) {
    logger.error('Request body is missing');
    return { statusCode: 400, body: 'Bad Request: Missing body' };
  }

  let snsPayload: SnsNotificationPayload;
  try {

    snsPayload = JSON.parse(event.body);
    logger.debug({ custom: snsPayload }, 'Parsed SNS Payload');
  } catch (error) {
    logger.error({ custom: { body: event.body, error } }, 'Failed to parse request body as JSON');
    return { statusCode: 400, body: 'Bad Request: Invalid JSON' };
  }


  try {
    await new Promise<void>((resolve, reject) => {
      validator.validate(snsPayload, (err, message) => {
        if (err) {
          logger.error({ custom: { error: err, snsPayload } }, 'SNS Message validation failed');
          return reject(err);
        }
        logger.info({ custom: { messageId: message.MessageId } }, 'SNS Message signature validated successfully');
        resolve();
      });
    });
  } catch (validationError) {
    return { statusCode: 403, body: 'Forbidden: Invalid SNS message signature' };
  }


  if (snsPayload.Type === 'SubscriptionConfirmation') {
    logger.info(
      { custom: { subscribeUrl: snsPayload.SubscribeURL } },
      'Received SNS Subscription Confirmation'
    );
    try {



      const response = await fetch(snsPayload.SubscribeURL);
      if (!response.ok) {
         throw new Error(`Failed to confirm subscription: ${response.statusText}`);
      }
      logger.info('Successfully confirmed SNS subscription via SubscribeURL');
      return { statusCode: 200, body: 'Subscription confirmed' };
    } catch (error) {
      logger.error({ custom: { error } }, 'Failed to automatically confirm SNS subscription');


      return { statusCode: 200, body: 'Subscription confirmation received, but auto-confirm failed. Check logs.' };
    }
  }


  if (snsPayload.Type === 'Notification') {
    logger.info({ custom: { messageId: snsPayload.MessageId } }, 'Received SNS Notification');

    let deliveryStatus: DeliveryStatusMessage;
    try {
      deliveryStatus = JSON.parse(snsPayload.Message);
      logger.debug({ custom: deliveryStatus }, 'Parsed Delivery Status Message');
    } catch (error) {
      logger.error(
        { custom: { message: snsPayload.Message, error } },
        'Failed to parse SNS Message content as JSON (delivery status)'
      );
      return { statusCode: 400, body: 'Bad Request: Invalid Message JSON' };
    }

    const awsMessageId = deliveryStatus.notification.messageId;
    const status = deliveryStatus.status === 'SUCCESS' ? 'DELIVERED' : 'FAILED';


    try {
      const updatedRecord = await db.smsMessage.update({
        where: { awsMessageId: awsMessageId },
        data: {
          deliveryStatus: status,
          statusPayload: deliveryStatus as any,
          updatedAt: new Date(),
        },
      });
      logger.info(
        { custom: { awsMessageId, newStatus: status, dbId: updatedRecord.id } },
        'Successfully updated SMS message status in database'
      );
      return { statusCode: 200, body: 'Notification processed successfully' };
    } catch (error) {

      if (error.code === 'P2025') {
         logger.warn({ custom: { awsMessageId } }, 'Could not find matching SMS record for update. Maybe already processed or invalid ID.');

         return { statusCode: 200, body: 'Notification acknowledged, but no matching record found.' };
      }
      logger.error(
        { custom: { awsMessageId, error } },
        'Failed to update SMS message status in database'
      );

      return { statusCode: 500, body: 'Internal Server Error: Database update failed' };
    }
  }


  logger.warn({ custom: { type: snsPayload.Type } }, 'Received unhandled SNS message type');
  return { statusCode: 200, body: 'Unhandled message type received' };
};
  • Message Validation: Uses aws-sns-message-validator to verify the Signature field. This is essential to ensure the request genuinely came from AWS SNS.
  • Subscription Confirmation: Handles the initial handshake from SNS. When you first create the subscription, SNS sends a SubscriptionConfirmation message. You must confirm this, either by visiting the SubscribeURL (logged by the function) or by making an HTTP GET request to it from the handler.
  • Notification Handling: Parses the Message field (which contains the actual delivery status JSON) when the Type is Notification.
  • Database Update: Finds the SmsMessage record using the awsMessageId from the notification and updates its deliveryStatus and statusPayload.
  • Error Handling: Includes checks for missing body, JSON parsing errors, validation failures, and database update errors (including handling cases where the record isn't found). Returns appropriate HTTP status codes (200 for success/non-retryable errors, 4xx for bad requests/auth issues, 500 for potential retryable server errors).

6. Connecting SNS to the Callback Endpoint

Now, link the SNS Topic (which receives delivery status) to your RedwoodJS callback function.

Step 1: Get Your Public Callback URL

  • Development: Use a tool like ngrok to expose your local development server:
  ngrok http 8911

Copy the https forwarding URL provided by ngrok (e.g., https://<unique-id>.ngrok.io). Your full callback URL will be https://<unique-id>.ngrok.io/snsCallback.

  • Production: Deploy your RedwoodJS application to a hosting provider (Render, Vercel, Fly.io, Netlify, etc.). Get the public URL of your deployed API. Your callback URL will be https://<your-deployed-api-url>/snsCallback.

Step 2: Create an SNS Subscription

  1. Navigate to SNS in the AWS Console.
  2. Go to Topics and select the topic you created for delivery status (e.g., sms-delivery-status-topic).
  3. Click the Create subscription button.
  4. Protocol: Select HTTPS.
  5. Endpoint: Paste your public callback URL (from Step 1).
  6. Enable raw message delivery: Keep this unchecked. We want the full SNS JSON structure, including metadata needed for validation.
  7. Click Create subscription.

Step 3: Confirm the Subscription

  • Check the logs of your running RedwoodJS API (or your ngrok console). You should see log entries indicating a SubscriptionConfirmation message was received, including the SubscribeURL.
  • Copy the SubscribeURL from the logs and paste it into your web browser. You should see an XML confirmation from AWS.
  • Alternatively, if you implemented the automatic fetch in the handler, it might confirm automatically (check logs for success/failure).
  • Go back to the SNS subscription list in the AWS console. The status should change from "Pending confirmation" to "Confirmed".

7. Security Considerations

  • Signature Validation: This is paramount. Always validate the signature of incoming SNS messages using aws-sns-message-validator or a similar library. This prevents attackers from spoofing delivery status updates.
  • HTTPS: Always use HTTPS for your callback endpoint URL to encrypt data in transit. ngrok provides HTTPS URLs, and production deployment platforms should be configured for HTTPS.
  • Environment Variables: Keep AWS credentials and other secrets out of your codebase. Use .env locally and configure environment variables securely in your deployment environment. Do not commit .env.
  • Least Privilege: The IAM role (SNSSMSDeliveryStatusRole) created earlier has broad CloudWatch permissions (\*). In a production scenario, scope this down to the specific log groups SNS needs to write to, if you rely heavily on CloudWatch logging alongside callbacks, as warned in Step 1.1. For the callback mechanism itself (sending notifications to the SNS Topic), the critical permissions are implicitly handled by SNS when you configure the delivery status topic in the SNS service settings.
  • Input Validation: While SNS provides the data, robust applications should still treat the parsed deliveryStatus payload cautiously, although the primary security concern is the signature validation.

8. Testing the Implementation

Step 1: Unit Testing the Service

Mock the AWS SDK to test your sendSms service logic without actually sending messages or hitting AWS.

import { mock } from 'jest-mock-extended';
import { SNSClient, PublishCommand } from '@aws-sdk/client-sns';
import { sendSms } from './sms';
import { db } from 'src/lib/db';

jest.mock('@aws-sdk/client-sns', () => {
  const originalModule = jest.requireActual('@aws-sdk/client-sns');
  return {
    ...originalModule,
    SNSClient: jest.fn(() => ({
      send: jest.fn(),
    })),
    PublishCommand: jest.fn((input) => ({
      input,
    })),
  };
});

describe('sms service', () => {
  const mockSnsClient = new SNSClient({});
  const mockSend = mockSnsClient.send as jest.Mock;

  beforeEach(() => {
    jest.clearAllMocks();




  });

  it('sendSms successfully publishes to SNS and updates DB', async () => {
    const toPhoneNumber = '+15550001111';
    const messageBody = 'Test message';
    const mockAwsMessageId = 'mock-aws-id-xyz';


    mockSend.mockResolvedValueOnce({ MessageId: mockAwsMessageId });


    const mockDbCreate = jest.spyOn(db.smsMessage, 'create').mockResolvedValueOnce({
      id: 'sms-cuid-123', createdAt: new Date(), updatedAt: new Date(),
      toPhoneNumber, messageBody, awsMessageId: null, deliveryStatus: 'PENDING',
      statusPayload: null, sentSuccessfully: false
    });
    const mockDbUpdate = jest.spyOn(db.smsMessage, 'update').mockResolvedValueOnce({
      id: 'sms-cuid-123', createdAt: new Date(), updatedAt: new Date(),
      toPhoneNumber, messageBody, awsMessageId: mockAwsMessageId, deliveryStatus: 'PENDING',
      statusPayload: null, sentSuccessfully: true
    });

    const result = await sendSms({ toPhoneNumber, messageBody });

    expect(result.success).toBe(true);
    expect(result.awsMessageId).toBe(mockAwsMessageId);
    expect(result.databaseId).toBe('sms-cuid-123');
    expect(mockSend).toHaveBeenCalledTimes(1);


    expect(mockDbCreate).toHaveBeenCalledWith({ data: { toPhoneNumber, messageBody, deliveryStatus: 'PENDING' } });
    expect(mockDbUpdate).toHaveBeenCalledWith({ where: { id: 'sms-cuid-123' }, data: { awsMessageId: mockAwsMessageId, sentSuccessfully: true } });
  });

  it('sendSms handles SNS publish failure', async () => {
     const toPhoneNumber = '+15552223333';
     const messageBody = 'Failure test';
     const errorMessage = 'SNS publish failed miserably';


     mockSend.mockRejectedValueOnce(new Error(errorMessage));


     const mockDbCreate = jest.spyOn(db.smsMessage, 'create').mockResolvedValueOnce({
       id: 'sms-fail-456', createdAt: new Date(), updatedAt: new Date(),
       toPhoneNumber, messageBody, awsMessageId: null, deliveryStatus: 'PENDING',
       statusPayload: null, sentSuccessfully: false
     });

     const mockDbUpdateOnFail = jest.spyOn(db.smsMessage, 'update').mockResolvedValueOnce({
       id: 'sms-fail-456', createdAt: new Date(), updatedAt: new Date(),
       toPhoneNumber, messageBody, awsMessageId: null, deliveryStatus: 'PUBLISH_FAILED',
       statusPayload: null, sentSuccessfully: false
     });

     const result = await sendSms({ toPhoneNumber, messageBody });

     expect(result.success).toBe(false);
     expect(result.error).toBe(errorMessage);
     expect(result.databaseId).toBe('sms-fail-456');
     expect(mockSend).toHaveBeenCalledTimes(1);
     expect(mockDbCreate).toHaveBeenCalledTimes(1);
     expect(mockDbUpdateOnFail).toHaveBeenCalledWith({
        where: { id: 'sms-fail-456' },
        data: { deliveryStatus: 'PUBLISH_FAILED', sentSuccessfully: false }
     });
  });


});

Step 2: Unit Testing the Callback Handler

Mock incoming HTTP events representing SNS messages.

import { mockHttpEvent } from '@redwoodjs/testing/api';
import { handler } from './snsCallback';
import { MessageValidator } from 'aws-sns-message-validator';
import { db } from 'src/lib/db';
import { logger } from 'src/lib/logger';

jest.mock('aws-sns-message-validator', () => ({
  MessageValidator: jest.fn().mockImplementation(() => ({
    validate: jest.fn((payload, callback) => {


      if (payload?.Signature === 'INVALID_SIGNATURE_FOR_TEST') {

        callback(new Error('Invalid signature'));
      } else if (payload?.Type === 'SubscriptionConfirmation' && payload?.SubscribeURL) {

        callback(null, payload);
      } else if (payload?.Type === 'Notification' && payload?.MessageId) {

        callback(null, payload);
      } else {

        callback(null, payload);
      }
    }),
  })),
}));

jest.mock('src/lib/db', () => ({
  db: {
    smsMessage: {
      update: jest.fn(),
    },
  },
}));

global.fetch = jest.fn();

jest.mock('src/lib/logger', () => ({
  logger: {
    info: jest.fn(),
    error: jest.fn(),
    warn: jest.fn(),
    debug: jest.fn(),
  },
}));

describe('snsCallback handler', () => {
  const mockDbUpdate = db.smsMessage.update as jest.Mock;
  const mockFetch = global.fetch as jest.Mock;
  const mockValidatorInstance = new MessageValidator({});
  const mockValidate = mockValidatorInstance.validate as jest.Mock;

  beforeEach(() => {
    jest.clearAllMocks();

    mockFetch.mockResolvedValue({ ok: true, statusText: 'OK' });
  });

  it('returns 400 if body is missing', async () => {
    const event = mockHttpEvent({});
    const response = await handler(event, null);
    expect(response.statusCode).toBe(400);
    expect(response.body).toContain('Missing body');
  });

  it('returns 400 if body is invalid JSON', async () => {
    const event = mockHttpEvent({ body: '{invalid json' });
    const response = await handler(event, null);
    expect(response.statusCode).toBe(400);
    expect(response.body).toContain('Invalid JSON');
  });

  it('returns 403 if SNS signature validation fails', async () => {
    const invalidPayload = {
      Type: 'Notification',
      MessageId: 'test-msg-id',
      TopicArn: 'arn:aws:sns:us-east-1:123456789012:test-topic',
      Message: '{}',
      Timestamp: new Date().toISOString(),
      SignatureVersion: '1',
      Signature: 'INVALID_SIGNATURE_FOR_TEST',
      SigningCertURL: 'https://example.com/cert.pem',
    };
    const event = mockHttpEvent({ body: JSON.stringify(invalidPayload) });

    const response = await handler(event, null);

    expect(mockValidate).toHaveBeenCalled();
    expect(response.statusCode).toBe(403);
    expect(response.body).toContain('Invalid SNS message signature');
  });

  it('handles SubscriptionConfirmation successfully', async () => {
    const subscribeUrl = 'https://example.com/subscribe?token=123';
    const subscriptionPayload = {
      Type: 'SubscriptionConfirmation',
      MessageId: 'sub-conf-id',
      Token: '123',
      TopicArn: 'arn:aws:sns:us-east-1:123456789012:test-topic',
      Message: 'You have chosen to subscribe to the topic...',
      SubscribeURL: subscribeUrl,
      Timestamp: new Date().toISOString(),
      SignatureVersion: '1',
      Signature: 'VALID_SIGNATURE',
      SigningCertURL: 'https://example.com/cert.pem',
    };
    const event = mockHttpEvent({ body: JSON.stringify(subscriptionPayload) });

    const response = await handler(event, null);

    expect(mockValidate).toHaveBeenCalledWith(subscriptionPayload, expect.any(Function));
    expect(mockFetch).toHaveBeenCalledWith(subscribeUrl);
    expect(response.statusCode).toBe(200);
    expect(response.body).toContain('Subscription confirmed');
    expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('Successfully confirmed SNS subscription'));
  });

   it('handles SubscriptionConfirmation failure during fetch', async () => {
    const subscribeUrl = 'https://example.com/subscribe?token=456';
    const subscriptionPayload = {
      Type: 'SubscriptionConfirmation', MessageId: 'sub-conf-id-fail', Token: '456',
      TopicArn: 'arn:aws:sns:us-east-1:123456789012:test-topic', Message: 'Sub message',
      SubscribeURL: subscribeUrl, Timestamp: new Date().toISOString(), SignatureVersion: '1',
      Signature: 'VALID_SIGNATURE', SigningCertURL: 'https://example.com/cert.pem',
    };
    const event = mockHttpEvent({ body: JSON.stringify(subscriptionPayload) });


    mockFetch.mockResolvedValueOnce({ ok: false, statusText: 'Not Found' });

    const response = await handler(event, null);

    expect(mockValidate).toHaveBeenCalled();
    expect(mockFetch).toHaveBeenCalledWith(subscribeUrl);
    expect(response.statusCode).toBe(200);
    expect(response.body).toContain('auto-confirm failed');
    expect(logger.error).toHaveBeenCalledWith(expect.objectContaining({ custom: { error: expect.any(Error) } }), expect.stringContaining('Failed to automatically confirm'));
  });

  it('handles Notification for SUCCESS status and updates DB', async () => {
    const awsMessageId = 'sns-publish-id-123';
    const deliveryStatusMessage: DeliveryStatusMessage = {
      notification: { messageId: awsMessageId, timestamp: new Date().toISOString() },
      delivery: { destination: '+15559998888', smsType: 'Transactional',  } as any,
      status: 'SUCCESS',
    };
    const notificationPayload = {
      Type: 'Notification',
      MessageId: 'sns-callback-id-abc',
      TopicArn: 'arn:aws:sns:us-east-1:123456789012:sms-delivery-status-topic',
      Message: JSON.stringify(deliveryStatusMessage),
      Timestamp: new Date().toISOString(),
      SignatureVersion: '1',
      Signature: 'VALID_SIGNATURE',
      SigningCertURL: 'https://example.com/cert.pem',
    };
    const event = mockHttpEvent({ body: JSON.stringify(notificationPayload) });


    mockDbUpdate.mockResolvedValueOnce({ id: 'db-record-id-xyz', awsMessageId });

    const response = await handler(event, null);

    expect(mockValidate).toHaveBeenCalled();
    expect(mockDbUpdate).toHaveBeenCalledWith({
      where: { awsMessageId: awsMessageId },
      data: {
        deliveryStatus: 'DELIVERED',
        statusPayload: deliveryStatusMessage,
        updatedAt: expect.any(Date),
      },
    });
    expect(response.statusCode).toBe(200);
    expect(response.body).toContain('Notification processed successfully');
    expect(logger.info).toHaveBeenCalledWith(expect.objectContaining({ custom: { awsMessageId, newStatus: 'DELIVERED' } }), expect.stringContaining('Successfully updated SMS message status'));
  });

  it('handles Notification for FAILURE status and updates DB', async () => {
    const awsMessageId = 'sns-publish-id-456';
    const deliveryStatusMessage: DeliveryStatusMessage = {
      notification: { messageId: awsMessageId, timestamp: new Date().toISOString() },
      delivery: { destination: '+15557776666', smsType: 'Transactional',  } as any,
      status: 'FAILURE',
    };
    const notificationPayload = {
       Type: 'Notification', MessageId: 'sns-callback-id-def', TopicArn: '...',
       Message: JSON.stringify(deliveryStatusMessage), Timestamp: '...', SignatureVersion: '1',
       Signature: 'VALID_SIGNATURE', SigningCertURL: '...',
    };
    const event = mockHttpEvent({ body: JSON.stringify(notificationPayload) });

    mockDbUpdate.mockResolvedValueOnce({ id: 'db-record-id-abc', awsMessageId });

    const response = await handler(event, null);

    expect(mockValidate).toHaveBeenCalled();
    expect(mockDbUpdate).toHaveBeenCalledWith(expect.objectContaining({
      data: expect.objectContaining({ deliveryStatus: 'FAILED' })
    }));
    expect(response.statusCode).toBe(200);
    expect(response.body).toContain('Notification processed successfully');
     expect(logger.info).toHaveBeenCalledWith(expect.objectContaining({ custom: { awsMessageId, newStatus: 'FAILED' } }), expect.stringContaining('Successfully updated SMS message status'));
  });

  it('returns 400 if Notification Message is invalid JSON', async () => {
     const notificationPayload = {
      Type: 'Notification', MessageId: 'sns-callback-id-ghi', TopicArn: '...',
      Message: '{invalid json inside message',
      Timestamp: '...', SignatureVersion: '1', Signature: 'VALID_SIGNATURE', SigningCertURL: '...',
    };
    const event = mockHttpEvent({ body: JSON.stringify(notificationPayload) });

    const response = await handler(event, null);

    expect(mockValidate).toHaveBeenCalled();
    expect(response.statusCode).toBe(400);
    expect(response.body).toContain('Invalid Message JSON');
    expect(logger.error).toHaveBeenCalledWith(expect.anything(), expect.stringContaining('Failed to parse SNS Message content'));
  });

  it('returns 200 if DB update fails with record not found (P2025)', async () => {
    const awsMessageId = 'sns-publish-id-789-notfound';
    const deliveryStatusMessage: DeliveryStatusMessage = {  } as any;
     deliveryStatusMessage.notification = { messageId: awsMessageId, timestamp: '...' };
     deliveryStatusMessage.status = 'SUCCESS';
    const notificationPayload = {  } as any;
    notificationPayload.Type = 'Notification';
    notificationPayload.Message = JSON.stringify(deliveryStatusMessage);
    notificationPayload.Signature = 'VALID_SIGNATURE';
    const event = mockHttpEvent({ body: JSON.stringify(notificationPayload) });


    const notFoundError = new Error('Record to update not found.');
    notFoundError.code = 'P2025';
    mockDbUpdate.mockRejectedValueOnce(notFoundError);

    const response = await handler(event, null);

    expect(mockValidate).toHaveBeenCalled();
    expect(mockDbUpdate).toHaveBeenCalled();
    expect(response.statusCode).toBe(200);
    expect(response.body).toContain('no matching record found');
    expect(logger.warn).toHaveBeenCalledWith(expect.objectContaining({ custom: { awsMessageId } }), expect.stringContaining('Could not find matching SMS record'));
  });

  it('returns 500 if DB update fails with other error', async () => {
    const awsMessageId = 'sns-publish-id-101-dberror';
     const deliveryStatusMessage: DeliveryStatusMessage = {  } as any;
     deliveryStatusMessage.notification = { messageId: awsMessageId, timestamp: '...' };
     deliveryStatusMessage.status = 'FAILURE';
    const notificationPayload = {  } as any;
    notificationPayload.Type = 'Notification';
    notificationPayload.Message = JSON.stringify(deliveryStatusMessage);
    notificationPayload.Signature = 'VALID_SIGNATURE';
    const event = mockHttpEvent({ body: JSON.stringify(notificationPayload) });


    const dbError = new Error('Database connection lost');
    mockDbUpdate.mockRejectedValueOnce(dbError);

    const response = await handler(event, null);

    expect(mockValidate).toHaveBeenCalled();
    expect(mockDbUpdate).toHaveBeenCalled();
    expect(response.statusCode).toBe(500);
    expect(response.body).toContain('Database update failed');
    expect(logger.error).toHaveBeenCalledWith(expect.objectContaining({ custom: { awsMessageId, error: dbError } }), expect.stringContaining('Failed to update SMS message status'));
  });

  it('handles unhandled SNS message types gracefully', async () => {
     const unhandledPayload = {
      Type: 'UnsubscribeConfirmation',
      MessageId: 'unsub-id', TopicArn: '...', Message: '...', Timestamp: '...',
      SignatureVersion: '1', Signature: 'VALID_SIGNATURE', SigningCertURL: '...',
    };
    const event = mockHttpEvent({ body: JSON.stringify(unhandledPayload) });

    const response = await handler(event, null);

    expect(mockValidate).toHaveBeenCalled();
    expect(response.statusCode).toBe(200);
    expect(response.body).toContain('Unhandled message type received');
    expect(logger.warn).toHaveBeenCalledWith(expect.objectContaining({ custom: { type: 'UnsubscribeConfirmation' } }), expect.stringContaining('Received unhandled SNS message type'));
  });

});

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 β†’