Sending MMS with Plivo in RedwoodJS - code-examples -

Frequently Asked Questions

You can send MMS messages by integrating Plivo's MMS API into your RedwoodJS application. This involves creating a Redwood service that uses the Plivo Node.js SDK and exposing this service via a GraphQL mutation. The service handles interaction with the Plivo API, sending media URLs and optional text to recipients.
Plivo is a cloud communications platform that enables sending MMS, SMS, and voice messages. In a RedwoodJS application, the Plivo Node.js SDK is used within a service to interact with the Plivo API for sending multimedia messages, including images and GIFs.
Plivo provides a convenient API and Node.js SDK for integrating MMS functionality into a RedwoodJS backend. It allows for richer communication, handling media that standard SMS cannot, and offers robust features for managing MMS campaigns.
Use a Plivo MMS service when you need to send messages containing rich media like images, GIFs, or other files directly from your RedwoodJS application. This is ideal for user engagement, notifications, or marketing campaigns in the US and Canada.
Plivo's MMS sending, as implemented in this RedwoodJS guide, is currently limited to US and Canadian numbers. Attempts to send to other regions will fail, so your application logic should handle these limitations.
First, install the Plivo Node.js SDK using `yarn workspace api add plivo`. Then, configure environment variables (`PLIVO_AUTH_ID`, `PLIVO_AUTH_TOKEN`, `PLIVO_SENDER_ID`) in a `.env` file. These credentials are essential for accessing the Plivo API and setting the appropriate sender number.
The `PLIVO_SENDER_ID` environment variable stores your MMS-enabled Plivo phone number in E.164 format (e.g., +14155551234). This is the number that will appear as the sender of the MMS messages.
You can find your Plivo Auth ID and Auth Token on the main dashboard overview page after logging in to your Plivo Console at https://console.plivo.com/.
The provided `sendMms` service includes error handling for missing environment variables, invalid input formats, and Plivo API errors. It returns a structured response, including a success flag, message, and error details for easier debugging and client feedback.
The `mediaUrl` provided to the Plivo API must be a publicly accessible URL. Plivo's servers need to download the media from this URL to attach it to the MMS message. Private or localhost URLs will not work.
During deployment to your chosen provider, add the `PLIVO_AUTH_ID`, `PLIVO_AUTH_TOKEN`, and `PLIVO_SENDER_ID` environment variables in the hosting platform's settings. Never commit your .env file containing these secrets.
Use the Redwood CLI command `yarn rw g service plivoMms` to generate the necessary service files. Then, implement the `sendMms` function in `api/src/services/plivoMms/plivoMms.ts`, using the provided code example, to handle MMS sending logic.
Install the Plivo SDK specifically into the `api` workspace using the command `yarn workspace api add plivo`. This ensures the package is only added as a dependency for the backend where it's required.
Define the GraphQL mutation in a new file named `api/src/graphql/plivoMms.sdl.ts`. The schema should include input arguments for the recipient number (`to`), media URL (`mediaUrl`), optional text (`text`), and a response type indicating success or failure.