Send SMS from RedwoodJS with Node.js and Plivo - code-examples -

Frequently Asked Questions

Integrate the Plivo SMS API into your RedwoodJS application. This involves setting up a backend service, creating a GraphQL mutation, and handling Plivo credentials securely using environment variables, as detailed in the guide.
RedwoodJS is a full-stack JavaScript framework chosen for its developer-friendly structure and tooling. It simplifies building both frontend (React) and backend (Node.js, GraphQL, Prisma) components.
Plivo is a cloud communications platform offering robust APIs for SMS, voice, and more. Its reliable infrastructure and Node.js SDK make it ideal for sending programmatic text messages.
Install the Plivo Node.js SDK with `yarn workspace api add plivo`. Then, securely store your Plivo Auth ID, Auth Token, and sender number in a `.env` file in your project's root directory.
Log in to your Plivo Console. Your Auth ID and Auth Token are displayed on the main dashboard. Your Plivo sender number can be found under "Phone Numbers" in the console.
The Plivo integration logic mainly resides in the `api` side of your RedwoodJS project. This includes the `sms` service in `api/src/services/sms/sms.ts` and the GraphQL schema in `api/src/graphql/sms.sdl.ts`.
Use the command `yarn rw g service sms --ts`. This generates the service file (`sms.ts`) where you'll implement the Plivo integration logic and a test file (`sms.test.ts`) for unit tests.
E.164 format is essential for sending SMS messages. It starts with a '+' followed by the country code and then the phone number, for example, +14155551234.
Implement `try...catch` blocks in your `sms` service. Return a structured `SmsResponse` object with `success: false` and a descriptive error message. Use Redwood's `logger` to log detailed errors internally for debugging.
Check the Plivo Console under Messaging > Logs for detailed message statuses (Sent, Delivered, Failed) and specific error codes. Use this information along with your internal logs to troubleshoot issues.
Use the GraphQL Playground at `http://localhost:8911/graphql` after running `yarn rw dev`. Replace `+1XXXXXXXXXX` with your test number in the provided mutation query example and execute it.
Input validation, like checking for E.164 format and message body length, prevents potential security vulnerabilities and ensures messages are sent correctly. Sanitize all user inputs to prevent misuse elsewhere in your application.
Store your Plivo Auth ID, Auth Token, and sender number in a `.env` file. Ensure this file is added to `.gitignore` to prevent accidental commits to version control. For production, use environment variables provided by your hosting platform.
Use Plivo's bulk SMS feature when sending the *same* message to multiple recipients (up to Plivo's limit). This is more efficient than making individual API calls for each recipient.
Use a library like `async-retry` to handle transient network or Plivo API errors. Implement exponential backoff and be sure to only retry retryable errors, such as network issues or 5xx server errors, but not permanent errors like authentication failures (4xx).
Yes, but you need to check the specific regulations for each country regarding Sender ID rules, opt-in requirements, and content restrictions. Ensure your Plivo account is configured for international sending.