Send WhatsApp Messages from Next.js using AWS Lambda and Meta API - code-examples -

Frequently Asked Questions

You can send WhatsApp messages from a Next.js application by using AWS Lambda as the backend and the Meta Business API. Create a Next.js API route that triggers a Lambda function, which securely retrieves your WhatsApp API credentials from AWS Secrets Manager and sends the message via the Meta Graph API.
AWS Lambda provides serverless compute for executing the message-sending logic. It retrieves credentials from AWS Secrets Manager and interacts with the Meta Graph API without requiring you to manage server infrastructure.
AWS Secrets Manager securely stores sensitive information like your WhatsApp API access token, protecting it from exposure in your code or environment variables. The Lambda function retrieves the token from Secrets Manager at runtime.
Message templates are mandatory for production applications, particularly for business-initiated conversations outside the 24-hour customer service window, or for sending notifications. You must use pre-approved templates by Meta to comply with their policies.
While Meta provides a temporary test phone number for development, it has limitations and is unsuitable for production. You must register your own business phone number through the Meta platform for production messaging.
Create a Meta App, enable the WhatsApp product, note the Phone Number ID, add your test phone number as a recipient, and generate a permanent access token for a system user. This access token is essential for interacting with the WhatsApp Business API.
The core AWS services are Lambda for serverless execution, Secrets Manager for storing API credentials, and IAM for managing permissions. You may also optionally utilize the AWS CLI/SDK for local deployment.
In the AWS Lambda console, create a new function using Node.js as the runtime. Configure the function's execution role with access to Secrets Manager. Finally, write your function code to fetch secrets and use Axios or another HTTP client to send requests to the Meta API.
Implement robust phone number validation using libraries like `libphonenumber-js` for ensuring E.164 formatting. Verify numbers within the Next.js API route before passing them to the Lambda function.
Protect your Next.js API route with authentication/authorization mechanisms, validate phone numbers thoroughly, use IAM least privilege for AWS resources, and implement rate limiting to prevent abuse of your API.
Implement detailed logging, define standard error responses, and consider retry mechanisms only for transient network errors or AWS service issues. For persistent Lambda failures, use Lambda Destinations (SQS or another Lambda function) for further processing.
Axios is used within the AWS Lambda function to make HTTP POST requests to the Meta Graph API. These requests send the actual WhatsApp messages, containing the recipient's phone number and the message content within the body of the request.
You can manually deploy your Lambda code as a zip file containing the function's logic and dependencies. For production, utilize infrastructure-as-code tools such as AWS CDK, Serverless Framework, SAM, or Terraform for a more reproducible and efficient deployment process.
Store opt-out information (e.g., using a database) and ensure your Lambda function checks this status before attempting to send any message. Respecting user choices regarding communication is vital for compliance and user trust.
Leverage message templates with language packs to handle different languages and locales appropriately. Ensure your message content is correctly translated and localized for the intended recipient.