Building Two-Way SMS with NestJS and MessageBird - code-examples -

Frequently Asked Questions

Start by installing the NestJS CLI, creating a new project, and adding the MessageBird SDK, NestJS ConfigModule, and dotenv. Configure the ConfigModule to load environment variables, enable the ValidationPipe globally, and create a ".env" file to store your MessageBird API key and originator (virtual number).
MessageBird is a communication platform that provides APIs for various communication channels, including SMS. In this NestJS setup, MessageBird's SMS API and virtual numbers are used to receive incoming SMS messages and send replies, enabling two-way SMS communication.
A tunneling service like ngrok or localtunnel is necessary during development to expose your locally running NestJS application to the public internet. This allows MessageBird webhooks, which are triggered by incoming SMS messages, to reach your local server.
While not essential for simple SMS applications, a database becomes crucial when you need to persist message history, especially for features like customer support chats, interactive SMS campaigns, or scenarios requiring tracking message status and user responses over time.
Use the NestJS CLI to generate a module, controller, and service specifically for webhooks. Define an IncomingMessageDto with validation rules, implement the MessageBird service to initialize the SDK and send replies, and configure the controller to handle incoming SMS webhooks at the desired endpoint.
The IncomingMessageDto is a Data Transfer Object in NestJS that represents the structure of the incoming webhook payload from MessageBird. It is used with the ValidationPipe to automatically validate and sanitize incoming data, ensuring only expected fields with correct types are processed.
Implement try...catch blocks around MessageBird API calls within your service to handle potential errors during sending. Log the errors using NestJS's Logger and throw appropriate exceptions. Consider retry mechanisms for better resilience.
After starting your local server and a tunneling service, go to the MessageBird Flow Builder. Create a new flow triggered by SMS, select your virtual number, and add a "Call HTTP endpoint with SMS" action. Set the method to POST and the URL to your tunnel URL + webhook endpoint path.
class-validator and class-transformer enhance security and streamline data handling. class-validator ensures that only expected fields with correct types are processed, preventing vulnerabilities. class-transformer simplifies data transformations between different formats.
To handle duplicate messages, use a database to store message history. Upon receiving a webhook, query the database for a message with a similar timestamp, originator, and payload. If found, log it as a duplicate and avoid reprocessing.
Implement input validation, rate limiting using @nestjs/throttler, secure API key management, and regular dependency updates to enhance your application's security.
Use NestJS's built-in Logger for development. For production, integrate a more robust logging library like Pino or Winston to output structured logs, facilitating analysis and debugging.
Initialize the MessageBird Node.js SDK (v10+) using your API key. Use the messages.create method with appropriate MessageParameters, including originator, recipients, and body, to send SMS messages. Handle responses and errors appropriately.
Key dependencies include messagebird (Node.js SDK), @nestjs/config, dotenv, class-validator, class-transformer, and optionally @nestjs/typeorm, typeorm, and a database driver like pg for PostgreSQL.