Frequently Asked Questions
Use Node.js with Express, the Vonage Messages API, and node-cron to schedule SMS messages. Set up an Express server to handle API requests, use the Vonage API to send messages, and node-cron to trigger messages at scheduled times, storing job details in a database like PostgreSQL.
The Vonage Messages API is a service that enables sending SMS messages programmatically. It's integrated into the Node.js application using the official Vonage Node.js SDK, @vonage/server-sdk, simplifying the process of sending text messages.
Node-cron provides a simple way to schedule tasks in a Node.js application, similar to cron jobs on a server. It's used to trigger the sending of scheduled SMS messages at specific times defined by cron expressions.
Create a Vonage application, enable the Messages capability, download your private key, and link your virtual number. Add the Application ID, private key path, and virtual number to your .env file, which Vonage uses to send scheduled messages. Go to Messages API settings and configure SMS defaults to use the Messages API
PostgreSQL, or similar relational databases, are recommended for persistent storage. The guide provides SQL examples for PostgreSQL but suggests using an ORM like Prisma or Sequelize for more complex projects.
The pgcrypto extension is required to use the gen_random_uuid() function, which generates UUIDs for identifying scheduled SMS jobs. Enable it with CREATE EXTENSION IF NOT EXISTS 'pgcrypto';
Run npm install express @vonage/server-sdk dotenv node-cron uuid pg or yarn add express @vonage/server-sdk dotenv node-cron uuid pg to install necessary dependencies for the SMS scheduling project, such as Express, Vonage SDK, and database connector.
The dotenv package helps manage environment variables. It loads variables from a .env file into process.env, allowing you to keep sensitive credentials like API keys and database connection strings separate from code. Ensure this file is in your .gitignore
The scheduler service checks for scheduled times that have already passed when the application starts or a job is scheduled. If a past-due job is found, it's marked as 'failed' in the database, preventing attempts to send the outdated message.
The '/schedule' API endpoint expects 'recipient', 'message', and 'scheduledTime' in the request body. The recipient must be in E.164 format, and scheduledTime should be a future date and time in ISO 8601 format.
ngrok is helpful during development to expose your local server and receive inbound webhooks from Vonage, like status updates or message replies, which are not necessary for the basic sending of scheduled SMS messages, but are helpful for future enhancements.
Send a DELETE request to /api/schedule/YOUR_JOB_ID (ensure a valid API Key). This will update the job status to 'cancelled', preventing the scheduled task from sending the message, and it will stop the task in memory on the current server.
The examples use PostgreSQL syntax, but you can adapt the SQL commands and the db.js configuration to use MySQL, SQLite, or other databases if preferred. You'll need the appropriate database client for Node (mysql2, sqlite3, etc.).
A 202 Accepted response to an API request to schedule an SMS indicates that the request has been accepted for processing but hasn't been completed yet. The SMS will be sent at the specified scheduled time.
Content Loading Error
We encountered an error while processing this content.