Frequently Asked Questions
You can send SMS messages within a RedwoodJS application by creating a serverless function that integrates with the Twilio Programmable Messaging API. This function will handle the logic for sending messages using your Twilio credentials and the recipient's phone number.
Twilio Programmable Messaging is a cloud communications platform that allows developers to send and receive SMS, MMS, and WhatsApp messages programmatically. It provides a simple REST API and client libraries for various programming languages, including Node.js, which is used in RedwoodJS.
RedwoodJS uses serverless functions for SMS because they provide a scalable and cost-effective way to handle backend logic without managing servers. This is ideal for tasks like sending SMS messages, which are often triggered by user actions or scheduled events.
A message queue like BullMQ is recommended for high-volume SMS sending or when implementing retry mechanisms to handle temporary failures. It helps prevent API gateway timeouts by offloading the sending process to a background worker.
You should store your Twilio Account SID, Auth Token, and Twilio phone number as environment variables in a .env file in the root of your RedwoodJS project. This file is automatically excluded from version control via .gitignore for security.
The API endpoint for sending SMS is /api/sendSms. It accepts POST requests with a JSON body containing the recipient's phone number (to) in E.164 format and the message content (body).
The provided code example includes a try...catch block within the sendSms function's handler. This catches errors during the Twilio API call, logs them using Redwood's logger, and returns a JSON error response with an appropriate HTTP status code and details about the error, if available from Twilio.
The recipient's phone number (to) must be in E.164 format. This is an international standard and includes a plus sign (+) followed by the country code and phone number, without any spaces or dashes (e.g., +15551234567).
Yes, you can test the sendSms function locally by running yarn rw dev to start the development server and then using curl or a tool like Postman to send POST requests to the /api/sendSms endpoint with test data.
Common Twilio errors include 20003 (invalid credentials), 21608 (unverified recipient number for trial accounts), 21212 (invalid "from" number), 21211 (invalid "to" number), and 20429/HTTP 429 (rate limits exceeded). Always consult the Twilio error documentation for detailed explanations and troubleshooting.
Logs are available in your RedwoodJS development console when running yarn rw dev, within your hosting provider’s logging service in production, and in the Twilio Console under "Monitor" -> "Logs" -> "Messaging". Twilio logs provide detailed information on message status and delivery.
Critically, the example sendSms function is insecure without authentication. In production, you must secure it using RedwoodJS's authentication features (yarn rw setup auth) and the @requireAuth directive or by checking isAuthenticated / currentUser in your function handler.
For high-volume SMS, use a message queue (like BullMQ with Redis) to handle sending asynchronously and implement retry mechanisms. Consider using Twilio Messaging Services to improve deliverability and manage opt-outs, and distribute sending across multiple Twilio numbers if necessary.
How to Send SMS with Twilio in RedwoodJS: Complete Integration Guide
Build a RedwoodJS API function to send SMS messages with Twilio's Programmable Messaging API in this comprehensive tutorial. Learn how to integrate Twilio SMS into your RedwoodJS application using Node.js, covering project setup, authentication, error handling, testing, and production deployment.
This tutorial shows you how to create a production-ready
/api/sendSmsendpoint in under 30 minutes. Send notifications, alerts, two-factor authentication codes, and transactional messages through Twilio's reliable SMS infrastructure. Perfect for developers building RedwoodJS applications that need SMS capabilities.Project Overview and Goals
Goal: Build a RedwoodJS API function that sends SMS messages via the Twilio API.
Problem Solved: Enables programmatic SMS communication directly from your RedwoodJS backend, facilitating features like user notifications, appointment reminders, or two-factor authentication prompts.
Technologies:
System Architecture:
sendSmsin this guide) receives the request containing recipient number and message body.Prerequisites:
npm install --global yarn.Final Outcome: A RedwoodJS project with a functional
/api/sendSmsendpoint that securely sends SMS messages using Twilio.1. Setting Up Your RedwoodJS Project with Twilio
Start by creating a new RedwoodJS project and installing the necessary dependencies.
Create a New RedwoodJS App: Open your terminal and run:
Follow the prompts (you can choose JavaScript or TypeScript – this guide uses JavaScript examples, but the concepts are identical).
Navigate to Project Directory:
Install Twilio Node.js Helper Library: RedwoodJS uses Yarn workspaces. Install the Twilio library specifically in the
apiworkspace.This command adds the
twiliopackage to theapi/package.jsonfile and installs it.Note: This guide is compatible with Twilio Node.js SDK v5.x (current as of 2024), which includes TypeScript definitions and improved performance. The SDK uses Twilio's Programmable Messaging API base URL
https://api.twilio.com/2010-04-01for message resources. For SDK documentation, see Twilio Node Helper Library.Configure Environment Variables: Never hardcode sensitive credentials like API keys. RedwoodJS uses
.envfiles for environment variables..envfile in the root of your project (redwood-twilio-sms/.env)..envfile:Obtaining Credentials:
TWILIO_ACCOUNT_SIDandTWILIO_AUTH_TOKEN: Find these on your main Twilio Console dashboard (https://www.twilio.com/console).TWILIO_PHONE_NUMBER: This is the SMS-enabled Twilio phone number you purchased. Find it under "Phone Numbers" > "Manage" > "Active numbers" in your Twilio Console. Ensure it's in E.164 format (e.g.,+15551234567).Security: Ensure your
.envfile is listed in your.gitignorefile (RedwoodJS includes this by default) to prevent accidentally committing secrets.2. Creating the RedwoodJS API Function to Send SMS
Create a RedwoodJS API function to handle the SMS sending logic.
Generate the API Function: Use the RedwoodJS CLI to scaffold a new function:
This creates the file
api/src/functions/sendSms.js(or.ts) with some boilerplate code.Implement the Sending Logic: Replace the contents of
api/src/functions/sendSms.jswith the following code:Code Explanation:
loggerfor logging and thetwiliolibrary.process.envand initialize the Twilio client. Check if the environment variables exist before initializing.POSTrequests.eventand validate the presence and basic format of thetophone number (must start with+) and thebody.client.messages.createwith thebody,from(your Twilio number), andtoparameters. This is an asynchronous operation, so we useawait.200 OKstatus with the message SID and status.try...catchblock handles potential errors during the API call (e.g., invalid credentials, network issues, Twilio errors). It logs the error and returns an appropriate error status code (using Twilio's status if available, otherwise 500) and a JSON error message, potentially including Twilio-specific error codes (error.code) and documentation links (error.moreInfo) for easier debugging.3. Testing Your RedwoodJS SMS API Endpoint
The RedwoodJS function is our API layer for this specific task.
Endpoint:
/api/sendSmsMethod:
POSTRequest Body (JSON):
Success Response (JSON – 200 OK):
Error Response (JSON – 4xx/5xx):
Testing with
curl: Make sure your RedwoodJS development server runs (yarn rw dev). Open a new terminal window and run:+1YOUR_VERIFIED_PHONE_NUMBERwith your actual mobile number (which must be verified in Twilio if using a trial account).yarn rw devfor logs.4. Configuring Twilio Credentials and Phone Numbers
Steps 1 (installation, environment variables) and 2 (client initialization, API call) covered this integration.
Key Configuration Points:
TWILIO_ACCOUNT_SID: Your unique account identifier. Find this on the Twilio Console dashboard. Purpose: Identifies your account to Twilio. Format:ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.TWILIO_AUTH_TOKEN: Your secret API key. Find this on the Twilio Console dashboard. Treat this like a password. Purpose: Authenticates your API requests. Format: Alphanumeric string.TWILIO_PHONE_NUMBER: The Twilio number messages send from. Find this in the Phone Numbers section of the Twilio Console. Purpose: Sets thefromnumber for outgoing SMS. Format: E.164 (+1...).Dashboard Navigation:
5. Implementing Error Handling and Logging for SMS
try...catchblock inapi/src/functions/sendSms.js. It catches errors from theclient.messages.createcall, logs them using Redwood's logger, and returns structured JSON error responses with appropriate HTTP status codes. It includes Twilio-specific error details when available (error.code,error.moreInfo).logger(api/src/lib/logger.js) provides logging capabilities.logger.info: Logs successful operations and key steps.logger.error: Logs exceptions and failure conditions.logger.warn: Logs non-critical issues like invalid input.yarn rw devruns. In production deployments, logs typically route to your hosting provider's logging service (e.g., Vercel Logs, Netlify Functions Logs).bullmqand Redis) to handle sending and retries more robustly, preventing HTTP request timeouts for long-running processes.Testing Error Scenarios:
TWILIO_AUTH_TOKENin.envto an incorrect value, restartyarn rw dev, and send acurlrequest. Expect a 401 or similar error.toNumber: Send acurlrequest with an improperly formattedtonumber (e.g.,5551234,+1). Expect a 400 error from our validation. Send a validly formatted but non-existent number – expect an error from Twilio (potentially logged, response might vary).curlrequest with"body": "". Expect a 400 error from our validation.6. Database Schema and Data Layer
Not Applicable: This specific function does not require database interaction. It receives data via the API request and interacts directly with the external Twilio service. If you need to store message history, associate messages with users, or manage opt-outs, define Prisma schemas in
api/db/schema.prisma, generate migrations (yarn rw prisma migrate dev), and use Redwood's services (yarn rw g service ...) to interact with the database before or after calling the Twilio API.7. Security Features
api/src/functions/sendSms.jsfunction includes basic validation for thetoandbodyparameters to prevent malformed requests or trivial injection attempts (though Twilio's API handles its own sanitization). Use more robust validation (e.g., usingyuporzod) for complex inputs.TWILIO_ACCOUNT_SID,TWILIO_AUTH_TOKEN) securely in environment variables (.env) and never check them into version control (.gitignore). In production, configure these via the hosting provider's environment variable settings./api/sendSms) can potentially call it.yarn rw setup auth ...) and add the@requireAuthdirective or checkisAuthenticated/currentUserwithin thehandlerfunction to ensure only authorized users or services can trigger SMS sending. The function as presented without authentication is insecure and unsuitable for production environments. See RedwoodJS Auth Docs.429 Too Many Requestserrors (Twilio error code 20429).8. SMS Best Practices: E.164 Format and Character Limits
tonumber is always in E.164 format (+followed by country code and number, no spaces or dashes). The validation enforces the leading+, but more comprehensive E.164 validation libraries exist if needed.bodysends as-is. If you need to send messages in different languages, your application logic (potentially in a RedwoodJS service calling this function) determines the correct language and provides the translatedbodytext.mediaUrlparameter to theclient.messages.createcall, pointing to a publicly accessible URL of the media file. MMS is typically only supported in the US and Canada.9. Optimizing SMS Performance and Handling High Volume
client.messages.createcall is asynchronous (await), preventing the Node.js event loop from blocking during the API request.fromnumber. Pass themessagingServiceSidinstead offromtoclient.messages.create.10. Monitoring, Observability, and Analytics
queued,sent,delivered,undelivered,failed), error codes, message segments, and cost. This is invaluable for debugging delivery issues./api/health) that returns200 OKto ensure the API side is generally responsive.sendSmsfunction.11. Troubleshooting Common Twilio SMS Issues
yarn rw devruns. This often shows immediate errors from your function code (like parsing errors, invalid input, or exceptions before the Twilio call is even made).TWILIO_ACCOUNT_SIDandTWILIO_AUTH_TOKENin your.envfile and ensure they match the Twilio Console. Restartyarn rw devafter changes.FromNumber (Twilio Error 21212): EnsureTWILIO_PHONE_NUMBERin.envis a valid Twilio number associated with your account and enabled for SMS.ToNumber (Twilio Error 21211): Ensure thetonumber provided in the request body is a valid phone number in E.164 format. Check Twilio logs for details if the format is correct but delivery fails..envNot Loaded: Ensure the.envfile is in the project root andyarn rw devwas restarted after creating/modifying it. Check that the environment variables are correctly named.@requireAuth), this endpoint is open. Secure it before deploying to production.curlcommand or frontend request sends a valid JSON body with the correctContent-Type: application/jsonheader.12. Deploying Your RedwoodJS SMS Application
TWILIO_ACCOUNT_SID,TWILIO_AUTH_TOKEN, andTWILIO_PHONE_NUMBERenvironment variables within your hosting provider's settings dashboard (e.g., Vercel Environment Variables, Netlify Build environment variables). Do not commit your.envfile.yarn install), potentially run tests (yarn rw test), build the app (yarn rw build), and deploy (vercel deploy --prod,netlify deploy --prod, etc.).13. Verification and Testing
Manual Verification:
yarn rw dev.curl(as shown in Section 3) or a tool like Postman/Insomnia to send aPOSTrequest tohttp://localhost:8910/api/sendSmswith a valid JSON body.curl/tool response for a200 OKstatus and JSON body with"success": true.yarn rw devconsole output for logs.Automated Testing (Jest): RedwoodJS uses Jest for testing. Write a basic test for our function.
api/src/functions/sendSms.test.js:jest.mock('twilio', ...)to replace the actual Twilio library with a mock.client.messages.createfunction, allowing us to control its return value (mockResolvedValue) or make it throw an error (mockRejectedValue).src/lib/loggerto avoid log output during tests.process.envvariables needed by the function within the test suite usingbeforeAlland restore them withafterAll.it(...)block tests a specific scenario: wrong HTTP method, invalid input, successful call, and failed API call.mockHttpEventfrom@redwoodjs/testing/apito simulate the event object passed to the handler.statusCodeandbodyof the response.twilio().messages.createfunction was called with the correct arguments.Frequently Asked Questions
How do I send SMS messages from RedwoodJS?
Send SMS from RedwoodJS by creating an API function that uses the Twilio Node.js SDK. Install the SDK with
yarn workspace api add twilio, configure your Twilio credentials in a.envfile, and useclient.messages.create()to send messages. This guide provides complete implementation details including error handling and testing.What Node.js version does RedwoodJS require for Twilio integration?
RedwoodJS requires Node.js version 20.x or later as of RedwoodJS 8.x releases. Install Node.js >=20 before starting your Twilio SMS integration project.
How much does it cost to send SMS with Twilio?
Twilio SMS costs vary by destination country. US SMS typically costs $0.0079 per message segment. Messages exceeding 160 characters (GSM-7) or 70 characters (UCS-2) split into multiple segments, with each segment billed separately. Check Twilio's pricing page for current rates in your target countries.
What is E.164 phone number format for Twilio?
E.164 is the international phone number format Twilio requires:
+[country code][number]with no spaces, dashes, or parentheses. Example:+15551234567for a US number. Always validate phone numbers to E.164 format before sending to avoid errors.How do I handle Twilio error codes in RedwoodJS?
Catch Twilio errors in your try-catch block and access
error.code(Twilio's error code),error.status(HTTP status), anderror.moreInfo(documentation link). Common errors include 21608 (unverified trial number), 21211 (invalid 'to' number), and 20429 (rate limit exceeded). Log these details for easier debugging.Can I send SMS from a RedwoodJS trial Twilio account?
Yes, but Twilio trial accounts have restrictions: (1) verify recipient phone numbers in the Twilio Console before sending, (2) messages include a "Sent from a Twilio trial account" prefix, and (3) you receive $15 in trial credit. Upgrade to a paid account to remove these limitations.
How do I secure my RedwoodJS SMS endpoint?
Secure your
/api/sendSmsendpoint by implementing RedwoodJS authentication. Useyarn rw setup authto configure auth, then add the@requireAuthdirective or checkisAuthenticatedin your handler function. Without authentication, your endpoint is publicly accessible and vulnerable to abuse.What are SMS character limits with Twilio?
Single SMS messages support 160 characters (GSM-7 encoding) or 70 characters (UCS-2 encoding for emojis/non-Latin scripts). Multi-segment messages reduce to 153 characters per segment (GSM-7) or 67 characters (UCS-2) due to the User Data Header. Twilio automatically handles segmentation but bills per segment.
How do I test Twilio SMS in RedwoodJS locally?
Test locally by running
yarn rw devand using curl or Postman to POST tohttp://localhost:8910/api/sendSmswith JSON body containingtoandbodyfields. For automated testing, use Jest with mocked Twilio clients as demonstrated in Section 13 of this guide.Can I send MMS images with Twilio in RedwoodJS?
Yes, add a
mediaUrlparameter toclient.messages.create()pointing to a publicly accessible image URL. MMS is primarily supported in the US and Canada. Note that MMS costs more than SMS – check Twilio's MMS pricing for your target countries.