Build an SMS Scheduler with Node.js, Express, and Vonage - code-examples -

Frequently Asked Questions

Use Node.js with Express, the Vonage SMS API, and node-cron to schedule messages. An Express server handles API requests, stores schedules, and a cron job triggers the Vonage API to send SMS at specified times. Remember, the example uses in-memory storage, unsuitable for production; a database is required for persistent storage.
Node-cron is a task scheduler that allows you to define when specific actions should occur. In this SMS scheduler, node-cron triggers a function at set intervals (e.g., every minute) to check the scheduled messages and send any that are due. It's crucial to configure the timezone correctly (Etc/UTC) to ensure accurate scheduling.
In-memory storage simplifies initial development but is not suitable for production. If the server restarts, all scheduled messages will be lost. Section 6 of the article details how to implement a database (e.g., PostgreSQL, MongoDB) for persistent storage in a production environment.
Replace in-memory storage with a persistent database *before* deploying your SMS scheduler to production. Using in-memory storage in production will result in data loss if the server crashes or restarts. The article emphasizes this repeatedly and provides database schema examples.
While the Vonage SDK supports the Messages API, this guide uses the simpler vonage.sms.send() method for the SMS API. Adapting to the Messages API is possible but requires modifying the sending logic within the cron job's function.
The provided code includes basic error handling with logging. For production, add retry logic (with exponential backoff) using libraries like 'async-retry', implement a dead-letter queue, and monitor for high failure rates. Log Vonage's status codes and error text for effective debugging.
PostgreSQL is a good choice due to its robust support for TIMESTAMPTZ for handling timezones correctly. However, other databases like MySQL or MongoDB (with appropriate handling of UTC time) can also be used. Choose an ORM/ODM for efficient data access.
Create a .env file and store your VONAGE_API_KEY, VONAGE_API_SECRET, VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH, and VONAGE_NUMBER. Never commit this file to version control. In production, use your platform's mechanisms (e.g., Heroku config vars, Docker secrets) to manage environment variables securely.
Express.js creates the API endpoint (/schedule) to receive scheduling requests from clients. It parses incoming JSON, validates requests, and adds the message information to the in-memory store (or database in production). It also handles basic server setup and routing.
Log into your Vonage API Dashboard, go to 'Applications', create a new application, generate public and private keys (saving the private key securely), enable the 'Messages' capability, and link your Vonage virtual number to the application.
The private.key file authenticates your application with Vonage. It should be kept secure, never committed to version control, and its path should be specified in the VONAGE_PRIVATE_KEY_PATH environment variable.
After setting up your environment and running the server, use tools like curl or Postman to send POST requests to the /schedule endpoint with the recipient number, message, and sendAt time in ISO 8601 format. Ensure the sendAt time is in the future and in UTC.
The client (e.g., Postman) sends a POST request to the Node.js/Express server. The server stores the request in memory (for testing) or a database (production), and the cron job regularly checks for due messages. If a message is due, the server uses the Vonage SDK to send it via the Vonage SMS API.
Setting the cron job timezone to UTC is crucial for accurate scheduling, regardless of the server's location. This ensures messages are sent at the intended time, avoiding issues due to different time zones.