Build Two-Way SMS: RedwoodJS & Sinch Integration Guide - code-examples -

Frequently Asked Questions

You can send SMS messages by creating a RedwoodJS service that uses the Sinch SMS API. This service will handle the logic for sending messages and interacting with your database to store message history. A GraphQL mutation will expose this functionality to your RedwoodJS frontend.
Ngrok creates a secure tunnel from a public URL to your local development environment. This is essential for testing webhooks locally because Sinch needs to deliver messages to a publicly accessible URL. Without ngrok, Sinch cannot reach your local server during development.
The .env file stores sensitive configuration details such as API keys and database URLs, keeping them separate from your codebase. This approach enhances security and prevents accidental exposure of credentials in version control systems like Git. The file should *not* be committed to your repository.
Verification is the first step after the inboundSms function is invoked. Always verify the webhook's signature to confirm its origin and integrity. This process prevents malicious actors from spoofing requests, protecting your data and application.
Yes, this guide describes setting up a Prisma schema and migrations to store message data like body, sender/receiver numbers, timestamps, and Sinch's external ID for each message. This provides a robust record of all sent and received SMS activity for your application.
Set up a Redwood Function as a webhook handler. Configure the webhook in the Sinch dashboard and point its target URL to your Redwood function's publicly accessible endpoint (using ngrok during development). The webhook will then deliver incoming messages to your application as they arrive.
Redwood Functions handle serverless logic, perfect for webhook endpoints. The `inboundSms` function receives data from Sinch when an SMS is sent to your Sinch virtual number. The function then stores the message information in the database and can initiate logic based on the incoming message (like automated replies).
Sinch's Conversation API provides a unified approach for handling messages across multiple channels (SMS, chat, etc.). While the dedicated SMS API can handle outbound messages, the Conversation API's webhooks are the standard way to receive inbound SMS through Sinch.
For simple outbound-only SMS, the SMS API (used via `@sinch/sdk-core` here) might be simpler. For inbound messages or scenarios requiring more advanced Conversation features (e.g., multiple channels), use the Conversation API's webhooks and associated functionalities.
Yes, ngrok creates a tunnel to your local development environment, enabling Sinch to send webhook requests to your `inboundSms` function locally. Ensure both your Redwood server and ngrok are running during local tests.
Sinch automatically retries webhook deliveries based on the status code your function returns. Return 5xx status codes (e.g., 500 Internal Server Error) for errors you want Sinch to retry. Return a 2xx code (e.g., 200 OK) when successful.
The provided `sendSms` service includes a `try...catch` block. Enhance it for production by implementing retries for transient errors, catching Sinch-specific errors, and logging error details appropriately.
It's crucial for securing your webhook. It's used to verify the signature of incoming requests, ensuring they originate from Sinch. This prevents unauthorized actors from sending fake webhook requests.
Add a `Message` model to your `schema.prisma` file. This model should include fields like `body`, `fromNumber`, `toNumber`, `direction`, `status`, `externalId`, and timestamps. Run `yarn rw prisma migrate dev` to apply the changes to your database.