Integrate Sinch Batch SMS API with RedwoodJS for Marketing Campaigns - code-examples -

Frequently Asked Questions

Integrate the Sinch Batch SMS API into your RedwoodJS application. This involves setting up environment variables for your Sinch credentials, creating database models for campaigns and contacts, and implementing RedwoodJS services to manage these entities and interact with the Sinch API. This guide provides a step-by-step tutorial for this integration process.
The Sinch Batch SMS API allows you to send bulk SMS messages programmatically within your RedwoodJS app, making it suitable for marketing campaigns. This guide focuses on integrating with the `/xms/v1/{SERVICE_PLAN_ID}/batches` endpoint, enabling automated campaign creation and targeted sending. Note that Sinch may offer dedicated Marketing Campaign APIs with additional features.
You can create a dedicated Sinch API client within your `api/src/lib` directory to handle the API interaction logic. Then import and call the client functions from your RedwoodJS services, specifically within the service responsible for managing and sending campaigns.
Prisma acts as an ORM (Object-Relational Mapper), simplifying database interactions within your RedwoodJS application. It allows you to define data models (like Contacts and Campaigns) and easily perform CRUD operations without writing raw SQL queries.
You primarily need services for managing campaigns and contacts. The campaign service handles creating, updating, and sending campaigns, while the contact service manages your contact list. These services interact with Prisma for database access and the Sinch client for sending SMS messages.
Create a `.env` file in the root of your RedwoodJS project. Add your `SINCH_SERVICE_PLAN_ID`, `SINCH_API_TOKEN`, `SINCH_SENDER_ID`, and `SINCH_API_BASE_URL` obtained from your Sinch dashboard to this file. Your `.env` file should already be in `.gitignore` for security.
GraphQL serves as the communication layer between the RedwoodJS frontend (web side) and backend (api side). You define GraphQL schemas (SDL) to specify data types and queries/mutations. RedwoodJS automatically generates resolvers that map these to your service functions.
Use the command `yarn create redwood-app --typescript` in your terminal. The `--typescript` flag is recommended for better type safety. Then, navigate into the project directory using `cd `.
While Node.js has a built-in `fetch`, using `node-fetch@^2` explicitly ensures better CommonJS compatibility, particularly with the RedwoodJS api side. Install it with `yarn workspace api add node-fetch@^2` and its types with `yarn workspace api add -D @types/node-fetch@^2`.
The example uses SQLite for simplicity, but you can configure PostgreSQL or MySQL in your `schema.prisma` file. The `DATABASE_URL` in your `.env` file controls the database connection. For local SQLite, use `DATABASE_URL="file:./dev.db"`.
The provided `sinchRequest` helper function in the Sinch client includes basic error handling and logging. It attempts to parse error responses from Sinch to provide more detailed messages for debugging. Always avoid exposing raw Sinch errors to the frontend if possible.
Yes, you can use a registered phone number, short code, or alphanumeric sender ID approved by Sinch. Set the `SINCH_SENDER_ID` environment variable in your `.env` file. This value is then used in the `from` field of the API request.
The Sinch Service Plan ID is a unique identifier that specifies the service plan you're using within the Sinch platform. It's required to make authenticated requests to the Sinch API and is included in the API endpoint URL.