Building an SMS Marketing App with Next.js and MessageBird - code-examples -

Frequently Asked Questions

Use the MessageBird SMS API integrated with a Next.js API route. Create a server-side function that calls the MessageBird API to send messages, triggered by a user action like submitting a form in your Next.js application. This enables dynamic SMS functionality within your app.
MessageBird provides the SMS sending and receiving capability. Its API and SDK are used to programmatically send SMS messages, receive incoming messages via webhooks, and manage subscribers, enabling the core functionality of an SMS-based application within the Next.js framework.
Prisma acts as a type-safe ORM for database access. It simplifies interactions with databases like PostgreSQL, MySQL, or SQLite, ensuring data consistency, and facilitates database operations like creating, reading, updating, and deleting subscriber information.
Set up a MessageBird webhook after obtaining a virtual mobile number and exposing your local development server. This lets MessageBird send incoming SMS messages (like SUBSCRIBE or STOP commands) to your application for processing, enabling user interactions.
Yes, localtunnel creates a public URL for your local development server. This allows MessageBird to deliver webhooks to your machine even during development when your app is not publicly deployed, essential for testing interactions with the MessageBird API.
Users text keywords (e.g., SUBSCRIBE/STOP) to a MessageBird virtual mobile number. Your Next.js webhook receives these messages, updates the database based on the keywords, and sends confirmation SMS using the MessageBird API.
The `MESSAGEBIRD_API_KEY` authenticates your application with the MessageBird service. Keep this key secure and store it in a `.env` file (never commit to version control) to protect your account and prevent unauthorized API usage.
The Next.js App Router provides a streamlined approach to creating API routes and server components, simplifying the handling of server-side logic like interacting with the MessageBird API and managing webhooks.
The admin interface allows inputting a message, which a server-side function then sends to all active subscribers via the MessageBird API, sending in batches of up to 50 recipients per API request as specified by MessageBird.
The example uses PostgreSQL, but Prisma supports other providers like MySQL and SQLite. You'll need to configure the `DATABASE_URL` in your `.env` file accordingly for Prisma to connect and manage subscriber information.
Adding authentication is crucial *before* deploying to production. The admin panel and send API route are currently unsecured; implement proper authentication to prevent unauthorized access and protect against malicious use.
Use a tool like `localtunnel` or `ngrok` to expose your local development server. Then, configure the MessageBird Flow Builder to send webhooks to your public `localtunnel` URL to test incoming messages and subscription logic during development.
Users might send SUBSCRIBE or Stop. Converting to uppercase (`toUpperCase()`) ensures the application handles variations consistently, avoiding issues with case mismatches and providing a better user experience.
The MessageBird API allows sending to up to 50 recipients per request. The provided code implements batching to handle larger subscriber lists and ensures compliance with MessageBird's API limitations.
The `@unique` constraint on the `phoneNumber` field in the Prisma schema prevents database-level duplicates. The application logic also gracefully handles re-subscribe or re-opt-out attempts, providing clear feedback to the user.