Tutorials

Integrating RedwoodJS with WhatsApp via AWS SNS

A developer guide on setting up a RedwoodJS application to receive and send WhatsApp messages using AWS SNS and End User Messaging Social.

JH
Jacky Hossein
β€’ Principal Telecommunications Architect β€’ Published Nov 1, 2026 β€’ 16 min read

Developer Quick-Start: A developer guide on setting up a RedwoodJS application to receive and send WhatsApp messages using AWS SNS and End User Messaging Social. 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 building a production-ready integration between a RedwoodJS application and WhatsApp, leveraging AWS Simple Notification Service (SNS) and AWS End User Messaging Social for seamless message handling.

Project Overview and Goals

This guide details how to build a system where your RedwoodJS application can receive incoming WhatsApp messages sent to your business number and send replies back to users.

Problem Solved: Businesses need effective ways to communicate with customers on preferred platforms like WhatsApp. This integration enables automated responses, custom workflows, and data logging triggered by customer messages directly within your RedwoodJS application.

Technologies Used:

  • RedwoodJS: A full-stack JavaScript/TypeScript framework for the web. We'll use its API side (Node.js/Fastify) to handle incoming messages and business logic.
  • WhatsApp Business Account (WABA): Required to represent your business on WhatsApp.
  • AWS End User Messaging Social: The AWS service that links your WABA to your AWS account, simplifying integration. (Note: AWS documentation might also refer to related functionality under services like Amazon Pinpoint; verify current naming conventions if needed.)
  • AWS Simple Notification Service (SNS): A pub/sub messaging service. AWS End User Messaging Social will publish incoming WhatsApp messages to an SNS topic.
  • AWS SDK for JavaScript v3: Used within the RedwoodJS API function to interact with AWS services, particularly for sending replies.
  • Node.js: The runtime environment for the RedwoodJS API side.
  • Prisma: RedwoodJS's default ORM for database interactions (optional but recommended for storing data).

System Architecture:

graph LR
    subgraph Incoming Flow
        WA_User[WhatsApp User (Sends Message)] --> Meta_WABA[WhatsApp Business Account (via Meta Platform)];
        Meta_WABA --> AWS_EUMS[AWS End User Messaging Social];
        AWS_EUMS --> AWS_SNS[AWS SNS Topic (WhatsAppIncomingMsg)];
        AWS_SNS --> Redwood_API_In[RedwoodJS API Function (Webhook Handler)];
    end

    subgraph Processing and Reply
        Redwood_API_In --> Logic{Process Message<br/>(Optional: DB Save via Prisma)};
        Logic --> AWS_SDK[AWS SDK for JS (Send Reply)];
    end

    subgraph Outgoing Flow
        AWS_SDK --> AWS_SocialAPI[AWS Social Messaging API];
        AWS_SocialAPI --> Meta_WABA_Out[WhatsApp Business Account (via Meta Platform)];
        Meta_WABA_Out --> WA_User_Reply[WhatsApp User (Receives Reply)];
    end

    Redwood_API_In -- Triggers --> Logic;
    Logic -- Calls --> AWS_SDK;
    AWS_SDK -- Sends via --> AWS_SocialAPI;

Expected Outcome: A RedwoodJS application capable of:

  1. Receiving webhook notifications from AWS SNS containing incoming WhatsApp messages.
  2. Parsing these messages within a RedwoodJS API function.
  3. (Optional) Storing message details or related data in a database using Prisma.
  4. Sending replies back to the originating WhatsApp user via the AWS SDK.

Prerequisites:

  • An AWS account with appropriate IAM permissions to create/manage SNS, End User Messaging Social, and potentially IAM roles/Lambda (if deploying the function that way).
  • A Meta Business Account and a phone number eligible for creating a WABA. Follow Meta's instructions if you don't have one.
  • A separate phone with the WhatsApp Messenger app installed for testing (cannot be the same number as the WABA).
  • Node.js >= 18.x installed locally.
  • Yarn package manager installed (npm install -g yarn).
  • AWS CLI installed and configured (aws configure).
  • Basic familiarity with RedwoodJS concepts (API functions, environment variables).

1. Setting up the RedwoodJS Project

First, create a new RedwoodJS project if you don't have one already.

  1. Create RedwoodJS App: Open your terminal and run:
   yarn create redwood-app ./redwood-whatsapp-sns
   cd redwood-whatsapp-sns

Choose your preferred options (TypeScript recommended).

  1. Environment Variables: RedwoodJS uses a .env file for environment variables. Create one in the project root:
   touch .env

Add the following placeholders. We will populate these later. .env

   # .env

   # AWS Credentials (Ensure secure handling in production - e.g., IAM Roles)
   AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
   AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
   AWS_REGION=us-east-1 # Or your preferred AWS region

   # AWS SNS & WABA Details (Obtained in later steps)
   INCOMING_SNS_TOPIC_ARN=YOUR_SNS_TOPIC_ARN
   WABA_PHONE_NUMBER_ID=YOUR_WABA_PHONE_NUMBER_ID

   # Optional: Security token for SNS subscription confirmation
   # SNS_SUBSCRIBE_TOKEN=YOUR_SECRET_TOKEN

Important: Add .env to your .gitignore file to avoid committing secrets. For production deployments, use environment variables provided by your hosting platform or secrets management tools instead of hardcoding AWS keys. IAM roles are the recommended approach for AWS deployments.

  1. Install AWS SDK: Install the necessary AWS SDK v3 clients for SNS and Social Messaging. Note: The package name @aws-sdk/client-social-post-messaging is used here based on available information. Verify this is the correct and current package name in the official AWS SDK v3 documentation, as it might be part of Pinpoint, Chime SDK Messaging, or another service.
   yarn workspace api add @aws-sdk/client-sns @aws-sdk/client-social-post-messaging

Install the library for SNS signature verification:

   yarn workspace api add aws-sns-verify

Install Zod for validation:

   yarn workspace api add zod

Install cuid if saving outgoing messages with temporary IDs (Section 6):

   yarn workspace api add cuid

2. Implementing Core Functionality: The API Function

We'll create a RedwoodJS API function to act as the webhook endpoint for SNS notifications.

  1. Generate API Function: Use the RedwoodJS CLI to generate a new function:
   yarn rw g function whatsappWebhook --ts

This creates api/src/functions/whatsappWebhook.ts and a test file.

  1. Implement Function Logic (Initial Version - Receiving & Parsing): Replace the contents of api/src/functions/whatsappWebhook.ts with the following code. This version includes SNS verification and parsing, preparing for reply logic.
   import type { APIGatewayEvent, Context } from 'aws-lambda'
   import { logger } from 'src/lib/logger'
   import { Verifier } from 'aws-sns-verify'
   import { SNSClient, ConfirmSubscriptionCommand } from '@aws-sdk/client-sns'
   import { z } from 'zod'

   const awsRegion = process.env.AWS_REGION || 'us-east-1'
   const snsClient = new SNSClient({ region: awsRegion })

   const verifier = new Verifier()

   const WhatsappMessageSchema = z.object({
     object: z.string(),
     entry: z.array(z.object({
       changes: z.array(z.object({
         value: z.object({
           messaging_product: z.literal('whatsapp'),
           metadata: z.object({
             display_phone_number: z.string(),
             phone_number_id: z.string(),
           }),
           contacts: z.array(z.object({ profile: z.object({ name: z.string() }), wa_id: z.string() })).optional(),
           messages: z.array(z.object({
             from: z.string(),
             id: z.string(),
             timestamp: z.string(),
             type: z.string(),
             text: z.object({ body: z.string() }).optional(),

           })),
         }),
         field: z.literal('messages'),
       })),
     })),
   });

   export const handler = async (event: APIGatewayEvent, _context: Context) => {
     logger.info('Received request on whatsappWebhook function')


     let snsMessage: any
     try {

       const rawPayload = JSON.parse(event.body || '{}')
       await verifier.verify(rawPayload)
       snsMessage = rawPayload
       logger.info({ type: snsMessage.Type }, 'SNS Signature Verified Successfully')
     } catch (error) {
       logger.error({ error, body: event.body }, 'SNS Signature Verification Failed')
       return { statusCode: 403, body: 'Forbidden: Invalid SNS Signature' }
     }


     if (snsMessage.Type === 'SubscriptionConfirmation') {


       if (snsMessage.TopicArn !== process.env.INCOMING_SNS_TOPIC_ARN) {
          logger.error('Attempted subscription confirmation for wrong topic ARN:', snsMessage.TopicArn);
          return { statusCode: 400, body: 'Bad Request: Invalid Topic ARN' };
       }

       logger.info(`Attempting to confirm SNS subscription programmatically for token: ${snsMessage.Token}`);
       try {
         const command = new ConfirmSubscriptionCommand({
           TopicArn: snsMessage.TopicArn,
           Token: snsMessage.Token,
         });
         await snsClient.send(command);
         logger.info('SNS Subscription Confirmed programmatically');
         return { statusCode: 200, body: 'Subscription Confirmed' };
       } catch (confirmError) {
         logger.error({ confirmError }, 'Failed to confirm SNS subscription');

         return { statusCode: 500, body: 'Subscription confirmation failed' };
       }
     }


     if (snsMessage.Type === 'Notification') {
       try {

         const rawWhatsappPayload = JSON.parse(snsMessage.Message)


         const validationResult = WhatsappMessageSchema.safeParse(rawWhatsappPayload);
         if (!validationResult.success) {
           logger.error({ errors: validationResult.error.issues, payload: rawWhatsappPayload }, 'WhatsApp payload validation failed');

           return { statusCode: 400, body: 'Bad Request: Invalid WhatsApp payload structure' };
         }
         const whatsappPayload = validationResult.data;

         logger.info(
           { messageId: whatsappPayload?.entry?.[0]?.changes?.[0]?.value?.messages?.[0]?.id },
           'Processing validated incoming WhatsApp message notification'
         )


         const messageEntry = whatsappPayload.entry[0]?.changes[0]?.value;
         const messageDetails = messageEntry?.messages?.[0];
         const senderWaId = messageDetails?.from;
         const messageText = messageDetails?.text?.body;
         const senderProfileName = messageEntry?.contacts?.[0]?.profile?.name;
         const messageTimestamp = messageDetails?.timestamp;
         const messageId = messageDetails?.id;

         if (!senderWaId || !messageId) {
            logger.warn({ payload: whatsappPayload }, 'Essential message details missing from payload')

            return { statusCode: 400, body: 'Bad Request: Incomplete message data' }
         }

         logger.info(
           `Message ID ${messageId} from ${senderProfileName || 'Unknown'} (${senderWaId}): ""${messageText || '[Non-text message]'}""`
         )














         return { statusCode: 200, body: 'Notification received successfully' }

       } catch (error) {

         logger.error({ error, snsMessage }, 'Error processing SNS Notification message')


         return { statusCode: 500, body: 'Internal Server Error processing notification' }
       }
     }


     logger.warn({ snsMessageType: snsMessage.Type }, 'Received unhandled SNS message type')
     return { statusCode: 200, body: 'Message received but not processed' }
   }

Explanation:

  • The handler receives the HTTP request from SNS.
  • SNS Signature Verification: Uses aws-sns-verify to ensure the request is authentic before processing. This is crucial for security.
  • SubscriptionConfirmation: Handles the initial handshake with SNS programmatically, validating the Topic ARN first.
  • Notification: This is the main path.
  • It parses the inner Message field (containing the WhatsApp payload).
  • It validates the structure of this payload using the WhatsappMessageSchema (Zod).
  • It extracts key details (sender ID, text, etc.). Note: The exact payload structure can change; always refer to the latest Meta documentation.
  • TODO: Integrate your business logic and prepare for sending replies (Section 4).
  • Returns 200 OK for successful processing or appropriate error codes (403 for bad signature, 400 for invalid payload, 500 for processing errors or failed subscription confirmation to allow retries).

3. Building the API Layer

In RedwoodJS, the API function whatsappWebhook is the API layer for this integration point.

  • Endpoint: RedwoodJS exposes this function at /api/whatsappWebhook. This is the URL you provide to AWS SNS.
  • Authentication/Authorization:
  • From SNS: The primary security is SNS Signature Verification (implemented in Section 2). This cryptographically verifies the request origin.
  • Internal Calls: Standard authentication (API keys, JWTs) applies if this function calls other internal/external APIs.
  • Request Validation:
  • SNS Signature verification handles authenticity.
  • The Zod schema (WhatsappMessageSchema) validates the structure of the inner WhatsApp payload (snsMessage.Message). Important: This schema is an example based on common structures. You must verify and potentially adjust it based on the actual payloads you receive from Meta/AWS via SNS, as these can evolve. Test with real messages.
  • API Documentation (SNS Input): The function expects POST requests with a JSON body matching the AWS SNS notification format. The critical field is Message, which contains a stringified JSON payload from AWS End User Messaging Social, mirroring Meta's Webhook format.
  • SNS Wrapper Example (Input to the function):
    {
      "Type": "Notification",
      "MessageId": "abcd-1234-...",
      "TopicArn": "arn:aws:sns:us-east-1:111122223333:YourTopicName",
      "Subject": null,
      "Message": "{\"object\":\"whatsapp_business_account\",\"entry\":[{\"id\":\"WABA_ID\",\"changes\":[{\"value\":{\"messaging_product\":\"whatsapp\",\"metadata\":{\"display_phone_number\":\"+1555...\",\"phone_number_id\":\"YOUR_WABA_PHONE_NUMBER_ID\"},\"contacts\":[{\"profile\":{\"name\":\"Test User\"},\"wa_id\":\"16505551234\"}],\"messages\":[{\"from\":\"16505551234\",\"id\":\"wamid.HB...\",\"timestamp\":\"1679...\",\"text\":{\"body\":\"Hello Redwood\"},\"type\":\"text\"}]},\"field\":\"messages\"}]}]}",
      "Timestamp": "2024-04-20T10:30:00.000Z",
      "SignatureVersion": "1",
      "Signature": "...",
      "SigningCertURL": "...",
      "UnsubscribeURL": "..."
    }
  • Inner Message Payload (Parsed WhatsApp Event - for clarity): This is what the JSON.parse(snsMessage.Message) call produces (structure needs verification):
    {
      "object": "whatsapp_business_account",
      "entry": [
        {
          "id": "WABA_ID",
          "changes": [
            {
              "value": {
                "messaging_product": "whatsapp",
                "metadata": {
                  "display_phone_number": "+1555...",
                  "phone_number_id": "YOUR_WABA_PHONE_NUMBER_ID"
                },
                "contacts": [
                  {
                    "profile": {
                      "name": "Test User"
                    },
                    "wa_id": "16505551234"
                  }
                ],
                "messages": [
                  {
                    "from": "16505551234",
                    "id": "wamid.HB...",
                    "timestamp": "1679...",
                    "text": {
                      "body": "Hello Redwood"
                    },
                    "type": "text"
                  }
                ]
              },
              "field": "messages"
            }
          ]
        }
      ]
    }

Refer to the official Meta for Developers documentation for the most current payload structure.

  • Testing (Simulating SNS): Use curl or Postman to send a POST request to your local development server's webhook endpoint (http://localhost:8911/api/whatsappWebhook by default). You'll need to generate a valid signature or temporarily disable verification for local testing. Tools exist to help craft signed SNS messages if needed. Basic curl without a valid signature will be rejected if verification is active.

4. Integrating with AWS Services (SNS, End User Messaging, Sending Replies)

This section covers setting up AWS infrastructure and enabling replies.

  1. Create SNS Topic (AWS Console or CLI):
  • Console: Navigate to SNS -> Topics -> Create topic. Type: Standard, Name: WhatsAppIncomingMessages (or similar). Note the Topic ARN. Add it to your .env (INCOMING\_SNS\_TOPIC\_ARN).
  • CLI: aws sns create-topic --name WhatsAppIncomingMessages --region YOUR\_REGION (Copy the TopicArn).
  1. Link WABA to AWS End User Messaging Social:
  • Navigate to the WABA page in the AWS Console (search for ""WhatsApp Business Accounts"" or similar under End User Computing / Messaging).
  • Click ""Add WhatsApp phone number"" (or similar wording).
  • Click ""Launch connection wizard"" (or similar, e.g., ""Launch Facebook portal"").
  • Follow the Meta pop-up instructions carefully: Log in, select the correct Meta Business Account, WhatsApp Business Account (WABA), and the specific phone number profile you want to link. Grant necessary permissions.
  • After the pop-up closes and you return to AWS, confirm the link is established.
  • In the ""Message and event destination"" (or similar) section, select SNS as the destination type and paste the ARN of the SNS topic you created (INCOMING\_SNS\_TOPIC\_ARN).
  • Save or ""Add phone number"".
  • Verify the account status becomes ""Active"" on the WABA page in AWS.
  • Click the Business account ID/Name you added. Under ""Phone Numbers"" (or similar tab), find the number you linked and copy its Phone number ID. Add this to your .env file (WABA\_PHONE\_NUMBER\_ID).
  1. Subscribe RedwoodJS Function to SNS Topic:
  • Deploy First: Your RedwoodJS API function needs to be deployed to a publicly accessible HTTPS URL (See Section 12 - Note: Section 12 is not present in the provided text, assume deployment is covered elsewhere). Assume URL is https://your-app.com/api/whatsappWebhook.
  • Subscribe (AWS Console or CLI):
  • Console: Go to your SNS Topic -> Subscriptions -> Create subscription. Protocol: HTTPS, Endpoint: https://your-app.com/api/whatsappWebhook, Disable ""Enable raw message delivery"". Create subscription.
  • CLI: aws sns subscribe --topic-arn YOUR\_SNS\_TOPIC\_ARN --protocol https --endpoint https://your-app.com/api/whatsappWebhook --region YOUR\_REGION
  • Confirm Subscription: SNS sends a SubscriptionConfirmation message. Your deployed function's logic (from Section 2) should handle this programmatically (validating the ARN and calling ConfirmSubscriptionCommand). Monitor logs to ensure confirmation succeeds. The subscription status in AWS changes from ""Pending confirmation"" to ""Confirmed"".
  1. Implement Sending Replies: Update the whatsappWebhook.ts function (or create a helper file like whatsappWebhook.lib.ts) to include the reply logic.
   import { logger } from 'src/lib/logger'
   import {
     SocialPostMessagingClient,
     SendWhatsappMessageCommand,
     SendWhatsappMessageCommandInput,
   } from '@aws-sdk/client-social-post-messaging'
   import { z } from 'zod'

   const awsRegion = process.env.AWS_REGION || 'us-east-1'

   const socialMessagingClient = new SocialPostMessagingClient({ region: awsRegion })

   export async function sendWhatsAppReply(recipientWaId: string, messageBody: string): Promise<void> {
     const wabaPhoneNumberId = process.env.WABA_PHONE_NUMBER_ID
     if (!wabaPhoneNumberId) {
       logger.error('WABA_PHONE_NUMBER_ID environment variable is not set.')

       throw new Error('Missing WABA Phone Number ID configuration.')
     }









     const params: SendWhatsappMessageCommandInput = {

        OriginationIdentity: wabaPhoneNumberId,
        DestinationIdentity: recipientWaId,
        Content: {
          Text: messageBody,


        },










     }


     try {
       logger.info({ recipientWaId, wabaPhoneNumberId, bodyLength: messageBody.length }, 'Attempting to send WhatsApp reply via AWS SDK')

       const command = new SendWhatsappMessageCommand(params)
       const result = await socialMessagingClient.send(command)

       logger.info({ awsMessageId: result.MessageId  }, 'Successfully sent WhatsApp reply via AWS SDK')
     } catch (error) {
       logger.error({ error, recipientWaId, wabaPhoneNumberId }, 'Failed to send WhatsApp reply via AWS SDK')


       throw error
     }
   }

Key Changes & Considerations:

  • Imported the necessary SDK client and command (verify names!).
  • Initialized the SDK client.
  • Implemented sendWhatsAppReply using the client.
  • CRITICAL: Added strong warnings that the SendWhatsappMessageCommandInput structure (params) must be verified against official AWS SDK documentation. The provided code is a placeholder guess.
  • Emphasized the 24-hour rule for free-form replies within the function documentation and near the call site.
  • Included error handling for the send operation, re-throwing errors to allow the main handler to manage the response to SNS (e.g., return 500 for retries on transient SDK errors).
  • Integrated the call to sendWhatsAppReply into the main handler's Notification block (shown commented out for integration).

5. Implementing Error Handling, Logging, and Retry Mechanisms

  • Error Handling:
  • Use try...catch blocks extensively.
  • SNS Signature Failures: Return 403 Forbidden. No retry needed.
  • Payload Validation Errors (Zod): Return 400 Bad Request. The message structure is wrong; retrying won't help.
  • Missing Configuration (e.g., Env Vars): Log error, return 500 Internal Server Error. Fix config and potentially use DLQ for failed messages.
  • SDK Call Failures (sendWhatsAppReply): Catch errors. Log details. Return 500 Internal Server Error to signal to SNS that it should retry (could be throttling, temporary network issue).
  • Database Errors: Catch errors. Log details. Return 500 Internal Server Error for potential retries.
  • Logging:
  • Use RedwoodJS logger (import { logger } from 'src/lib/logger').
  • Log key events: function start, signature verification result, message type, parsed details (sanitize PII!), reply attempts, success/failure of operations, errors with context.
  • Use structured logging: logger.info({ key: value }, 'Message').
  • Configure LOG\_LEVEL per environment.
  • PII: Avoid logging full message bodies or user identifiers unless necessary and compliant with privacy policies.
  • Retry Mechanisms:
  • SNS Retries: SNS automatically retries HTTPS endpoints on 5xx errors or timeouts. Customize the retry policy (attempts, backoff) in the SNS subscription settings if needed.
  • Dead-Letter Queue (DLQ): Highly recommended. Create an SQS queue to act as a DLQ for the SNS subscription. If SNS fails delivery after all retries, the message goes to the DLQ for inspection and potential manual reprocessing. Configure this in the SNS subscription settings. Requires granting SNS permission to write to the SQS queue.
  • Testing Error Scenarios: Test invalid signatures, malformed JSON, SDK failures (by mocking rejection), DB errors (mocking rejection), and timeouts (setTimeout) to ensure correct error code responses and DLQ behavior.

6. Creating a Database Schema and Data Layer (Optional)

Use Prisma to store message history or related data.

  1. Define Schema: Edit api/db/schema.prisma.

Prisma

   // api/db/schema.prisma
   datasource db {
     provider = ""postgresql"" // Or your chosen database
     url      = env(""DATABASE_URL"")
   }

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

   model WhatsappMessage {
     id            String    @id @default(cuid()) // Internal DB ID
     whatsappId    String    @unique // wamid from Meta
     senderWaId    String    // User's WA ID
     recipientWaId String    // Your WABA number ID
     body          String?   // Message text content
     type          String    // Message type ('text', 'image', etc.)
     direction     String    // 'incoming' or 'outgoing'
     timestamp     DateTime  // When the message was sent/received (from Meta timestamp)
     status        String?   // Optional: 'sent', 'delivered', 'read' (for outgoing)
     createdAt     DateTime  @default(now()) // DB record creation time
     updatedAt     DateTime  @updatedAt

     // Add relations if needed, e.g., to a User or Conversation model
     // conversationId String?
     // conversation   Conversation? @relation(fields: [conversationId], references: [id])
   }

   // Example Conversation model
   // model Conversation {
   //   id            String    @id @default(cuid())
   //   userWaId      String    @unique // Link conversation to a specific user WA ID
   //   messages      WhatsappMessage[]
   //   createdAt     DateTime  @default(now())
   //   updatedAt     DateTime  @updatedAt
   // }
  1. Apply Schema Changes: Run Prisma Migrate to update your database schema and generate the Prisma Client.
   yarn rw prisma migrate dev --name add-whatsapp-message-model
  1. Use Prisma Client in API Function: Modify api/src/functions/whatsappWebhook.ts to save incoming messages.
   import { db } from 'src/lib/db'
   import cuid from 'cuid'

   if (snsMessage.Type === 'Notification') {
     try {


       if (!senderWaId || !messageId) {

          return { statusCode: 400, body: 'Bad Request: Incomplete message data' }
       }

       logger.info(
         `Message ID ${messageId} from ${senderProfileName || 'Unknown'} (${senderWaId}): ""${messageText || '[Non-text message]'}""`
       )


       try {
         await db.whatsappMessage.create({
           data: {
             whatsappId: messageId,
             senderWaId: senderWaId,
             recipientWaId: messageEntry.metadata.phone_number_id,
             body: messageText,
             type: messageDetails.type,
             direction: 'incoming',
             timestamp: new Date(parseInt(messageTimestamp) * 1000),

           },
         })
         logger.info({ whatsappId: messageId }, 'Incoming message saved to database')
       } catch (dbError) {


         logger.error({ error: dbError, whatsappId: messageId }, 'Failed to save incoming message to database')


       }


       let replyText = `Hi ${senderProfileName || 'there'}, we received: ""${messageText}"".`;



       try {

         await sendWhatsAppReply(senderWaId, replyText);
         logger.info(`Reply sent for message ID ${messageId}`);

























       } catch (replyError) {
         logger.error({ error: replyError, whatsappId: messageId }, 'Failed to send WhatsApp reply');

         return { statusCode: 500, body: 'Internal Server Error sending reply' };
       }



       return { statusCode: 200, body: 'Notification processed successfully' }

     } catch (error) {

       logger.error({ error, snsMessage }, 'Error processing SNS Notification message')
       if (error instanceof z.ZodError) {
          return { statusCode: 400, body: 'Bad Request: Invalid WhatsApp payload structure' };
       }

       return { statusCode: 500, body: 'Internal Server Error processing notification' }
     }
   }

Key Changes:

  • Imported db from src/lib/db.
  • Added a try...catch block around db.whatsappMessage.create to save the incoming message details.
  • Converted the Meta timestamp (string Unix seconds) to a JavaScript Date object.
  • Included commented-out example logic for saving an outgoing message placeholder, acknowledging that getting the final status/ID might require separate mechanisms (like status webhooks from Meta/AWS).
  • Refined error handling to distinguish between fatal errors (like validation) and potentially retryable errors (like DB or SDK failures).

Remember to configure your DATABASE_URL environment variable in .env for Prisma to connect to your database.

Integrate WhatsApp with RedwoodJS using AWS SNS and AWS End User Messaging Social. This involves setting up a RedwoodJS API function as a webhook endpoint to receive incoming WhatsApp messages and send replies via the AWS SDK.

AWS End User Messaging Social is an AWS service that connects your WhatsApp Business Account (WABA) to your AWS account, enabling seamless integration with other AWS services like SNS for message handling.

AWS SNS acts as a pub/sub messaging service, allowing AWS End User Messaging Social to publish incoming WhatsApp messages to an SNS topic, which your RedwoodJS application can subscribe to for real-time message processing.

Use pre-approved WhatsApp Message Templates when replying to users outside the 24-hour free-form message window. Within 24 hours of a user's last message, you can send free-form text replies.

Yes, you can store WhatsApp message details in a database using Prisma, RedwoodJS's default ORM. Define a schema in schema.prisma and use the Prisma client in your API function to save incoming and outgoing message data.

Create a RedwoodJS API function (e.g., whatsappWebhook) and deploy it to a public HTTPS URL. Subscribe this URL to your AWS SNS topic, which receives messages from AWS End User Messaging Social linked to your WhatsApp Business Account.

WhatsApp allows free-form text replies only within 24 hours of a user's last message. Outside this window, use pre-approved Message Templates, which have a different structure within the AWS Social Post Messaging API.

Within your RedwoodJS webhook function, validate that the TopicArn in the SubscriptionConfirmation message matches your expected ARN, then use the AWS SDK's ConfirmSubscriptionCommand to programmatically confirm the subscription.

Zod is used for validating the incoming WhatsApp payload structure within the RedwoodJS function. It ensures data integrity by checking against a predefined schema, protecting your app from unexpected data formats.

Use the AWS SDK for JavaScript v3, specifically the SocialPostMessagingClient (or similar) and SendWhatsappMessageCommand with the correct parameters to send replies from your RedwoodJS API function to users' WhatsApp numbers.

Your RedwoodJS function needs AWS credentials to interact with AWS services (SNS, End User Messaging Social, etc.). Securely manage these using environment variables, IAM roles (recommended for production), or secrets management tools.

SNS signature verification validates that incoming webhook requests originate from AWS SNS, ensuring security. Use the aws-sns-verify library in your RedwoodJS function to verify the signature before processing any message data.

A DLQ (SQS queue) captures messages that SNS fails to deliver to your RedwoodJS webhook after multiple retries. This enables inspecting and potentially reprocessing failed messages, preventing data loss.

Implement try...catch blocks and logging for robust error handling. Return appropriate HTTP status codes (e.g., 403 for invalid signature, 400 for bad payload, 500 for processing errors or failed subscription confirmation to allow retries) to manage SNS retries and DLQ routing.

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