Sending SMS with Twilio in RedwoodJS: A Developer Guide - code-examples -

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.