Frequently Asked Questions
You can send SMS messages with RedwoodJS by integrating the MessageBird SMS API. Create a RedwoodJS service and a GraphQL mutation to programmatically send messages, leveraging Redwood's full-stack capabilities and MessageBird's reliable platform. This is ideal for sending transactional notifications like OTPs and alerts.
The RedwoodJS MessageBird integration allows you to send SMS messages directly from your RedwoodJS application backend. It uses the MessageBird Node.js SDK and environment variables for API key management. This enables sending transactional SMS notifications like one-time passwords (OTPs) and alerts directly from your web application.
MessageBird offers a reliable and developer-friendly SMS API, and RedwoodJS provides a robust full-stack framework with GraphQL, services, and deployment options. Combining these technologies simplifies sending transactional SMS notifications from your application backend.
First, install the MessageBird Node.js SDK using `yarn workspace api add messagebird`. Then, set your MessageBird Live API Access Key in a `.env` file at your project's root, ensuring this file is in `.gitignore`. Remember to configure the same environment variable in your hosting provider's settings during deployment.
Use the RedwoodJS CLI command `yarn rw g service sms` to generate the necessary files: `sms.ts`, `sms.scenarios.ts`, and `sms.test.ts`. Implement the SMS sending logic within the generated `sms.ts` file, utilizing the MessageBird SDK and environment variables.
Use a try-catch block within your RedwoodJS service function and check for errors in the MessageBird SDK callback. Map known MessageBird error codes to RedwoodJS's UserInputError for clearer GraphQL error responses. For unexpected errors, throw a generic Error or a FatalServerError for logging and handling in the client.
GraphQL acts as the API layer in RedwoodJS. Define your sendSms mutation in the SDL (schema definition language) specifying the input parameters and return type. RedwoodJS will automatically map this mutation to your SMS service function. Be sure to protect your mutation with appropriate authorization like @requireAuth.
Use the MessageBird Test API key during initial development to verify integration without sending real SMS or incurring costs. Switch to the Live API key when testing with real phone numbers and in production to send actual messages.
Store your MessageBird API keys securely in environment variables, specifically within a `.env` file in your project's root directory during development. Add this file to your `.gitignore` to prevent accidental commits. When deploying, set the same environment variable in your hosting provider's settings.
MessageBird automatically splits long messages exceeding the standard SMS limit (160 characters for GSM, 70 for Unicode). The service includes a length warning. Always use E.164 formatting for recipient numbers to ensure international compatibility.
Yes, MessageBird supports sending to multiple recipients (up to 50) in a single API call. Modify your RedwoodJS service to accept an array of recipients for the messagebird.messages.create method. This is more efficient than sending many individual API calls.
Monitor SMS logs, delivery rates, and costs through the MessageBird Dashboard. Leverage RedwoodJS's logging capabilities for monitoring application-side behavior and integrate with error tracking services for monitoring application health.
Refer to the MessageBird API documentation and error codes. Common issues include authentication failures (check API key), invalid recipient or originator formats (check E.164 format), and insufficient balance. Consult RedwoodJS logs and MessageBird's dashboard for detailed error information.
Robust input validation, especially for phone numbers, is essential to prevent errors, ensure deliverability, and avoid unnecessary costs. Use a dedicated library like libphonenumber-js for comprehensive validation.
Use background jobs for non-time-critical SMS messages, such as bulk notifications or scheduled messages, to avoid blocking main web requests. Implement a queue system to manage these jobs and handle retries for transient errors.
This guide provides a step-by-step walkthrough for integrating the MessageBird SMS API into a RedwoodJS application to enable basic outbound SMS functionality. We'll build a simple RedwoodJS service and GraphQL mutation to send SMS messages programmatically.
This setup solves the common need to send transactional SMS notifications (like OTPs, alerts, or confirmations) directly from your web application backend. We use RedwoodJS for its full-stack capabilities and integrated tooling (GraphQL API, services, deployment options) and MessageBird for its reliable SMS delivery platform and developer-friendly API.
System Architecture:
Prerequisites:
yarn global add redwoodjs/cli
)By the end of this guide, you will have a RedwoodJS application with a GraphQL mutation capable of sending an SMS message via MessageBird.
1. Project Setup
Let's start by creating a new RedwoodJS project and installing the necessary dependencies.
Create RedwoodJS Project: Open your terminal and run:
Navigate to Project Directory:
Install MessageBird SDK: Add the official MessageBird Node.js SDK to the API side of your project:
This command specifically installs the package within the
api
workspace, where our backend logic resides.Configure Environment Variables: Sensitive information like API keys should never be hardcoded. We'll use environment variables.
Create a
.env
file in the root of your project:Add your MessageBird Live API Access Key to the
.env
file. You can find this key in your MessageBird Dashboard under Developers -> API access.Replace
YOUR_LIVE_ACCESS_KEY
with your actual key.Important: Ensure
.env
is listed in your project's root.gitignore
file (RedwoodJS includes this by default) to prevent accidentally committing your secret key.Test vs. Live Keys: MessageBird provides both Live and Test API keys.
Deployment: When deploying (Section 12), you must configure this same environment variable (
MESSAGEBIRD_ACCESS_KEY
) in your hosting provider's settings.This completes the basic project setup and configuration.
2. Implementing Core SMS Sending Functionality
RedwoodJS uses a service layer (
api/src/services
) for business logic. We'll create ansms
service to handle interactions with the MessageBird API.Generate the SMS Service: Use the RedwoodJS CLI generator:
This creates:
api/src/services/sms/sms.ts
: The service file where our logic will live.api/src/services/sms/sms.scenarios.ts
: For defining seed data for tests.api/src/services/sms/sms.test.ts
: The unit test file.Implement the Sending Logic: Open
api/src/services/sms/sms.ts
and replace its contents with the following:Explanation:
initClient
frommessagebird
and necessary types.logger
for structured logging andUserInputError
for standard GraphQL errors.SendSmsInput
,SmsSendSuccessResponse
) for better type safety and code clarity.process.env.MESSAGEBIRD_ACCESS_KEY
and initialize the client. We now check for the key inside thesendSms
function to provide a better error if it's missing during an actual attempt.sendSms
function accepts an object withoriginator
,recipient
, andbody
.libphonenumber-js
is recommended for production.params
object required bymessagebird.messages.create
. Note thatrecipients
must be an array.messagebird.messages.create
method is asynchronous and uses a callback pattern. We wrap it in aPromise
for cleanerasync/await
syntax.err
exists, we log the detailed error and reject the promise with aUserInputError
, providing a more user-friendly message where possible by inspectingerr.errors
. MessageBird error codes (like2
for auth or21
for bad request) can be used for more specific feedback. Unexpected API response structures are handled with a genericError
.try...catch
handles any unexpected errors during the promise execution or validation.3. Building the API Layer (GraphQL Mutation)
RedwoodJS automatically maps service functions to GraphQL resolvers. We need to define the GraphQL schema (SDL) for our
sendSms
mutation.Generate the SDL: Use the RedwoodJS CLI generator:
This creates
api/src/graphql/sms.sdl.ts
.Define the Mutation Schema: Open
api/src/graphql/sms.sdl.ts
and replace its contents with the following:Explanation:
input SendSmsInput
type mirroring the structure expected by our service function.type SmsSendResponse
for the data returned by the mutation upon successful initiation, including success status, the MessageBird message ID, and the initial status.error: String
field has been removed. GraphQL standard practice is to handle errors through the top-levelerrors
array in the response. RedwoodJS automatically populates this when a service function throws anError
(especiallyUserInputError
). The client should check for the presence of thiserrors
array.sendSms
mutation within theMutation
type. It accepts theSendSmsInput
and returnsSmsSendResponse
on success.@skipAuth
: For simplicity in this guide, we're skipping authentication. In a real application, you MUST protect this mutation using@requireAuth
or similar directives to ensure only authorized users/systems can send SMS.RedwoodJS automatically connects this SDL definition to the
sendSms
function inapi/src/services/sms/sms.ts
based on naming conventions.4. Integrating with MessageBird (API Keys Recap)
While covered in setup, this section recaps the key aspects of MessageBird API key usage:
Obtain API Key:
Secure Storage & Purpose:
MESSAGEBIRD_ACCESS_KEY
environment variable (in.env
locally) is used by thesms
service (process.env.MESSAGEBIRD_ACCESS_KEY
) to authenticate requests.Environment Handling:
.env
file is used during development (yarn rw dev
).MESSAGEBIRD_ACCESS_KEY
) in your hosting provider's settings (e.g., Vercel, Netlify Environment Variables). Do not commit your.env
file.5. Error Handling, Logging, and Retries
Our service implementation includes basic error handling and logging.
UserInputError
for clearer feedback via GraphQL (returned in theerrors
array).try...catch
and typically throw a genericError
or potentially Redwood'sFatalServerError
.UserInputError
for invalid input or predictable API issues (bad number, auth fail).logger
(import { logger } from 'src/lib/logger'
).logger.info
: Log successful attempts and outcomes, including the MessageBird message ID and status. Include key parameters like recipient/originator for context (avoid logging sensitive body content unless necessary and compliant).logger.error
: Log detailed error objects (err
from MessageBird,error
from catch blocks) to aid debugging. Include context like input parameters where safe.6. Database Schema and Data Layer (Optional)
For this basic guide focused purely on sending an SMS, a database schema isn't strictly required. However, in a production application, you would likely want to log SMS sending attempts and their outcomes.
Potential Schema (Example using Prisma):
Implementation:
schema.prisma
.yarn rw prisma migrate dev
to apply changes.sendSms
service function:SmsLog
entry with status 'attempted'.messageBirdId
.errorMessage
.This database logging is omitted from the main code for simplicity but is a recommended practice.
7. Security Features
Protecting your SMS sending functionality is crucial.
@skipAuth
with@requireAuth
(or role-based directives) on thesendSms
mutation inapi/src/graphql/sms.sdl.ts
. This ensures only logged-in/authorized users or systems can trigger the SMS send. Refer to RedwoodJS Authentication documentation.libphonenumber-js
for robust phone number validation and formatting.body
.originator
against allowed values (your purchased numbers or registered alphanumeric IDs)..env
and never committing the file or exposing the key in frontend code.sendSms
mutation. Options:rate-limiter-flexible
with Redis or an in-memory store.sendSms
service (e.g., querySmsLog
timestamps per user/recipient).8. Handling Special Cases
+14155552671
) as the originator.+
followed by country code and number, no spaces/dashes). Our basic validation hints at this. Use robust validation (e.g.,libphonenumber-js
).9. Performance Optimizations
For single transactional SMS, application performance is usually not the bottleneck.
recipients
array. Modify the service if batch sending is needed. Avoid many sequential individual API calls.10. Monitoring, Observability, and Analytics
/graphql/health
endpoint or create custom checks.11. Troubleshooting and Caveats
Authentication failed
(Code 2): CheckMESSAGEBIRD_ACCESS_KEY
.recipient is invalid
/originator is invalid
(Code 21): Check E.164 format for recipient; check originator validity (VMN format or registered Alphanumeric ID per country rules).No balance
(Code 9): Add MessageBird credits.Message body is empty
: Ensurebody
is provided.401 Unauthorized
: API key issue.400 Bad Request
: Invalid parameters; check MessageBird error details in logs.sent
) doesn't guarantee final delivery. Use MessageBird webhooks for DLRs (delivered
,failed
).12. Deployment and CI/CD
MESSAGEBIRD_ACCESS_KEY
with your Live key in your hosting provider's environment variable settings. Ensure it's available to the API service at runtime (and potentially build time).yarn rw deploy vercel
).MESSAGEBIRD_ACCESS_KEY
secret. Include tests (yarn rw test api
).13. Verification and Testing
Local Development Testing:
Run
yarn rw dev
.Use the GraphQL playground (
http://localhost:8911/graphql
) to execute thesendSms
mutation:Replace placeholders. Check the GraphQL response (expect data, not errors). Check API logs. Check the phone (if using Live key). Check MessageBird Dashboard Log.
Automated Testing (Unit/Integration):
api/src/services/sms/sms.test.ts
.