Tutorials

How to Build SMS Appointment Reminders with Vonage and Next.js (2026 Guide)

Build automated SMS appointment reminders and scheduling with Vonage Messages API and Next.js. Production-ready tutorial with Prisma, PostgreSQL, timezone handling, and Vercel Cron Jobs.

JH
Jacky Hossein
β€’ Principal Telecommunications Architect β€’ Published Nov 1, 2026 β€’ 15 min read

Developer Quick-Start: Build automated SMS appointment reminders and scheduling with Vonage Messages API and Next.js. Production-ready tutorial with Prisma, PostgreSQL, timezone handling, and Vercel Cron Jobs. Want to skip 10DLC vetting queues, carrier surcharges, and multi-step SDK boilerplate? You can send the same SMS directly via SMSRoute's REST API with instant no-KYC provisioning.

Build automated SMS appointment reminders using Next.js and Vonage Messages API that reliably deliver scheduled notifications to users at their chosen frequency. This comprehensive guide covers project setup, database design with Prisma and PostgreSQL, timezone handling, security best practices, and production deployment with Vercel Cron Jobs.

Learn how to create a system where users subscribe via a web interface, provide their phone number and reminder frequency, and receive automated SMS reminders via the Vonage Messages API according to their schedule. This tutorial addresses real-world notification needs including appointment reminders, subscription renewals, and habit tracking applications.

Project Overview and Goals

What You'll Build:

  • A Next.js application with a simple frontend for users to subscribe
  • A Next.js API route (using Pages Router) to handle subscription requests
  • A PostgreSQL database managed with Prisma ORM to store subscription details
  • A scheduled task (logic within an API route, triggered externally) to check for due reminders
  • Integration with the Vonage Messages API to send SMS reminders
  • Robust error handling, logging, and security considerations
  • Deployment instructions for Vercel

Technologies Used:

  • Next.js: A React framework providing server-side rendering, static site generation, and API routes – ideal for building full-stack applications. Use the Pages Router for API routes in this guide. (Compatible with Next.js 14-15; while the App Router with Route Handlers is now recommended for new projects as of Next.js 14+, Pages Router remains fully supported.)
  • Node.js: The runtime environment for your Next.js backend and scheduled tasks. (v18 or later recommended, aligning with current LTS releases.)
  • Vonage Messages API: A powerful API for sending and receiving messages across various channels, including SMS. Use the Node SDK (@vonage/server-sdk v3.24.1 or @vonage/messages v1.20.3, current as of late 2024/early 2025).
  • PostgreSQL: A reliable open-source relational database.
  • Prisma: A modern Node.js and TypeScript ORM that simplifies database access, migrations, and type safety. (Prisma v5.x+ includes driver adapter support and improved TypeScript type-safety.)
  • date-fns & date-fns-tz: Libraries for robust date and time zone manipulation. (date-fns-tz v3.x provides timezone support using the Intl API, which is natively supported in modern Node.js and browsers.)
  • Vercel: A platform for deploying frontend and serverless applications, offering seamless integration with Next.js and features like Cron Jobs. (Vercel Cron Jobs require vercel.json configuration and are available on Hobby plans with 2 job limit, Pro plans with 40 job limit.)

Deployment Alternatives:

Platform Best For Cron Support Database Options Cost
Vercel Next.js apps, quick deployment Built-in Cron Jobs Vercel Postgres, external Free tier available
Railway Full-stack apps, persistent services External schedulers needed Managed PostgreSQL included Pay-as-you-go
AWS Lambda + EventBridge High scale, enterprise Native EventBridge scheduling RDS, Aurora Pay per execution
DigitalOcean App Platform Traditional apps, predictable pricing App Platform Cron Managed databases Fixed monthly

System Architecture:

+-----------------+      +---------------------+      +-----------------+
| User (Browser)  | ---> | Next.js Frontend    | ---> | Next.js API     |
|                 |      | (pages/index.tsx)   |      | (/api/subscribe)|
+-----------------+      +---------------------+      +--------+--------+
                                                             |
                                                             | (Write)
                                                             v
+-----------------+      +---------------------+      +--------+--------+
| External Cron   | ---> | Next.js API         | <--- | PostgreSQL DB   |
| Trigger (Vercel)|      | (/api/cron)         |      | (Prisma Client) |
|                 |      | (Scheduled Job Logic)| ---> +--------+--------+
+-----------------+      +---------+-----------+            |
                                   |                        | (Read)
                                   v                        ^
                          +--------+--------+               |
                          | Vonage Messages |---------------+
                          | API             |
                          +-----------------+

Prerequisites:

  • Node.js (v18 or later recommended, matching current LTS) and npm/yarn
  • A Vonage API account (Sign up here). You'll need your API Key, API Secret, and a Vonage Application ID with the "Messages" capability enabled and a generated private key. (Vonage uses JWT authentication with private keys for production environments as a security best practice.)
  • A Vonage phone number capable of sending SMS messages, which must be linked to your specific Vonage Application within the Vonage dashboard
  • Access to a PostgreSQL database (local or cloud-hosted, e.g., Vercel Postgres, Supabase, Neon)
  • A tool like ngrok only if you intend to test incoming Vonage webhooks (like delivery receipts or inbound SMS), which this guide doesn't focus on. It's not required for the core outbound scheduling functionality described here.
  • Basic understanding of React, Node.js, APIs, and databases

1. Setting up the Next.js Project

Initialize your Next.js project and install the necessary dependencies for SMS scheduling.

  1. Create Next.js App: Open your terminal and run the following command. Use the Pages Router (--no-app) for API routes in this guide for clarity in the examples.
   npx create-next-app@latest vonage-scheduler --typescript --eslint --tailwind --src-dir --no-app --import-alias "@/*"
   cd vonage-scheduler

(Adjust Tailwind/ESLint preferences if needed. This guide assumes a src/ directory).

  1. Install Dependencies:
   npm install @vonage/server-sdk @prisma/client date-fns date-fns-tz dotenv zod
   npm install --save-dev prisma typescript @types/node @types/react @types/react-dom
  • @vonage/server-sdk: The official Vonage SDK for Node.js (v3.24.1 as of late 2024). Alternatively, use @vonage/messages (v1.20.3) for standalone Messages API access.
  • @prisma/client: The Prisma client library to interact with your database.
  • prisma: The Prisma CLI for migrations and generation (dev dependency).
  • date-fns, date-fns-tz: For reliable date/time and time zone handling. date-fns-tz v3.x uses the Intl API for timezone support.
  • dotenv: To load environment variables from a .env file during development.
  • zod: For robust input validation.
  • typescript and @types/\*: For TypeScript support and type definitions.
  1. Initialize Prisma:
   npx prisma init --datasource-provider postgresql

This creates:

  • A prisma directory with a schema.prisma file.
  • A .env file (add this to .gitignore if not already present!).
  1. Configure .env: Open the .env file created by Prisma and add your database connection URL and Vonage credentials. Never commit this file to version control. Create a .env.example file to track needed variables. Quote all string values, especially multi-line ones or those containing special characters.

.env

   # .env

   # Database Connection (Replace with your actual connection string)
   DATABASE_URL="postgresql://user:password@host:port/database?sslmode=require"

   # Vonage Credentials
   VONAGE_API_KEY="YOUR_VONAGE_API_KEY"
   VONAGE_API_SECRET="YOUR_VONAGE_API_SECRET"
   VONAGE_APPLICATION_ID="YOUR_VONAGE_APPLICATION_ID"

   # Store the content of your private.key file here, enclosed in double quotes.
   # Ensure correct formatting with literal \n for newlines if copying directly.
   # Alternatively, provide a path (VONAGE_PRIVATE_KEY_PATH) and read the file in code.
   # IMPORTANT: Vonage uses JWT authentication with private keys for production security.
   # The private key should not contain return characters; newlines must be escaped as \n.
   VONAGE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nYOUR_PRIVATE_KEY_CONTENT_LINE_1\nYOUR_PRIVATE_KEY_CONTENT_LINE_2\n-----END PRIVATE KEY-----"
   # Or uncomment and use path:
   # VONAGE_PRIVATE_KEY_PATH="./private.key"

   VONAGE_SMS_FROM_NUMBER="YOUR_VONAGE_PHONE_NUMBER" # e.g., 14155550100

   # Used by the cron job trigger (can be anything, just needs to match)
   # SECURITY: Vercel automatically includes this secret as a bearer token in cron job requests.
   # Generate using: openssl rand -base64 32
   CRON_SECRET="YOUR_SUPER_SECRET_CRON_TRIGGER_KEY"
  • DATABASE_URL: Get this from your PostgreSQL provider.
  • Vonage Credentials: Find these on your Vonage API Dashboard:
  • API Key & Secret: Top of the dashboard.
  • Application ID & Private Key: Go to "Applications" β†’ "Create a new application". Give it a name (e.g., "NextJS Scheduler"), enable the "Messages" capability. Click "Generate public and private key" – the private.key file will download. Save this file securely. Each time a new key is generated, the old key is no longer valid. Copy its content exactly into VONAGE\_PRIVATE\_KEY (ensuring newlines are represented as \\n within the quotes) or provide the path to the file in VONAGE\_PRIVATE\_KEY\_PATH. Note the Application ID shown.
  • Link your Vonage Number: In the Application settings within the Vonage dashboard, link the Vonage phone number you intend to send SMS from (VONAGE\_SMS\_FROM\_NUMBER) to this specific application.
  • VONAGE_SMS_FROM_NUMBER: The Vonage number linked above.
  • CRON_SECRET: Generate a strong random string using openssl rand -base64 32 or a password generator. Vercel Cron Jobs automatically include this as a bearer token in the Authorization header for security.
  1. Project Structure: Your src directory will contain:
  • pages/: Frontend pages and API routes.
  • index.tsx: Your main subscription form UI.
  • api/: Backend API endpoints.
  • subscribe.ts: Handles new subscriptions.
  • cron.ts: Contains the logic for sending scheduled reminders.
  • health.ts: (Optional) Basic health check endpoint.
  • lib/: Utility functions/modules.
  • prisma.ts: Prisma client instance.
  • vonageClient.ts: Vonage SDK client instance.
  • utils/: Helper functions.
  • timezones.ts: Timezone validation helpers.
  • scheduler.ts: Logic for calculating reminder times.
  • styles/: Global styles.

2. Creating a Database Schema and Data Layer

Define your database schema using Prisma and create the necessary database table for storing SMS reminder subscriptions.

  1. Define Schema: Open prisma/schema.prisma and define the Subscription model:

Prisma

   // prisma/schema.prisma

   generator client {
     provider = "prisma-client-js"
   }

   datasource db {
     provider = "postgresql"
     url      = env("DATABASE_URL")
   }

   model Subscription {
     id        String   @id @default(cuid())
     phoneNumber String   @unique // Ensure phone numbers are unique
     frequencyMinutes Int      // How often to remind (in minutes)
     timezone  String   // User's IANA timezone (e.g., "America/New_York")
     nextReminderAt DateTime // When the next reminder should be sent (UTC)
     isActive  Boolean  @default(true) // To easily disable subscriptions

     createdAt DateTime @default(now())
     updatedAt DateTime @updatedAt

     @@index([nextReminderAt, isActive]) // Index for efficient cron job querying
   }
  • Store phoneNumber (uniquely), frequencyMinutes, and the user's timezone.
  • nextReminderAt is crucial: store the exact UTC timestamp for the next reminder.
  • isActive allows disabling reminders without deleting data.
  • An index on nextReminderAt and isActive makes querying for due reminders efficient.
  • Note: Prisma v5.4.0+ supports driver adapters as the primary database communication method, improving performance and flexibility.

Scaling Considerations for Indexes:

For applications with millions of subscriptions, consider these additional indexes:

Index Purpose When to Add
@@index([phoneNumber, isActive]) Quick user lookups > 100K subscriptions
@@index([createdAt]) Analytics queries Any scale
@@index([timezone, isActive]) Timezone-based reporting > 1M subscriptions
  1. Run Database Migration: Apply the schema changes to your database:
   npx prisma migrate dev --name init

This creates the Subscription table in your PostgreSQL database.

  1. Generate Prisma Client: Ensure the Prisma client is up-to-date with your schema:
   npx prisma generate
  1. Create Prisma Client Instance: Create a reusable Prisma client instance.
   import { PrismaClient } from '@prisma/client';

   declare global {

     var prisma: PrismaClient | undefined;
   }

   export const prisma =
     global.prisma ||
     new PrismaClient({
       log: process.env.NODE_ENV === 'development' ? ['query', 'info', 'warn', 'error'] : ['error'],
     });

   if (process.env.NODE_ENV !== 'production') {
     global.prisma = prisma;
   }

   export default prisma;

This pattern prevents creating multiple PrismaClient instances during development hot-reloading.

3. Integrating with Necessary Third-Party Services (Vonage)

Set up the Vonage SDK client for sending automated SMS reminders.

  1. Create Vonage Client Instance: Create a file to initialize and export the Vonage client.
   import { Vonage } from '@vonage/server-sdk';
   import { Auth } from '@vonage/auth';
   import { SMS } from '@vonage/messages';
   import fs from 'fs';
   import path from 'path';

   import 'dotenv/config';

   const getPrivateKey = (): string => {
     if (process.env.VONAGE_PRIVATE_KEY) {


       return process.env.VONAGE_PRIVATE_KEY.replace(/\\n/g, '\n');
     } else if (process.env.VONAGE_PRIVATE_KEY_PATH) {
       try {
         const keyPath = path.resolve(process.cwd(), process.env.VONAGE_PRIVATE_KEY_PATH);
         return fs.readFileSync(keyPath, 'utf-8');
       } catch (error) {
         console.error('Error reading Vonage private key file:', error);
         throw new Error(`Could not read Vonage private key file at path: ${process.env.VONAGE_PRIVATE_KEY_PATH}`);
       }
     } else {
       throw new Error('VONAGE_PRIVATE_KEY or VONAGE_PRIVATE_KEY_PATH must be set in environment variables.');
     }
   };

   if (!process.env.VONAGE_API_KEY || !process.env.VONAGE_API_SECRET || !process.env.VONAGE_APPLICATION_ID) {
     console.error('Missing Vonage API Key, Secret, or Application ID in environment variables.');
     throw new Error('Missing required Vonage API credentials.');
   }

   const credentials = new Auth({
     apiKey: process.env.VONAGE_API_KEY,
     apiSecret: process.env.VONAGE_API_SECRET,
     applicationId: process.env.VONAGE_APPLICATION_ID,
     privateKey: getPrivateKey(),
   });

   const vonage = new Vonage(credentials);

   export { vonage, SMS };

   export const sendSms = async (to: string, text: string): Promise<any> => {
     if (!process.env.VONAGE_SMS_FROM_NUMBER) {
       console.error('VONAGE_SMS_FROM_NUMBER is not set in environment variables.');
       throw new Error('Vonage SMS sender number is not configured.');
     }

     console.log(`Attempting to send SMS via Vonage to ${to}`);
     try {

       const response = await vonage.messages.send(
         new SMS({
           to: to,
           from: process.env.VONAGE_SMS_FROM_NUMBER,
           text: text,

         })
       );
       console.log(`Vonage message sent successfully to ${to}. Message UUID: ${response.messageUuid}`);
       return response;
     } catch (error: any) {

       const errorDetails = error?.response?.data || error.message || error;
       console.error(`Error sending Vonage SMS to ${to}:`, JSON.stringify(errorDetails, null, 2));

       throw new Error(`Failed to send SMS: ${error.message || 'Unknown Vonage API error'}`);
     }
   };
  • This initializes the Vonage client using credentials from environment variables.
  • It includes logic to read the private key either directly from VONAGE\_PRIVATE\_KEY (handling escaped newlines) or from a file path specified by VONAGE\_PRIVATE\_KEY\_PATH.
  • The sendSms helper function simplifies sending SMS messages, including improved logging.
  • Vonage SDK v3.x (specifically v3.24.1 as of late 2024) uses TypeScript and Promises for asynchronous operations.

Vonage Error Handling:

Common Vonage API errors and how to handle them:

Error Code Meaning Recommended Action
1300 Invalid credentials Verify API key and secret
1320 Insufficient balance Alert admin, top up account
1380 Invalid phone number format Validate E.164 format before sending
1440 Rate limit exceeded Implement exponential backoff
1520 Message rejected by carrier Log and notify user via alternative channel

4. Building the API Layer (Subscription Endpoint)

Create the API endpoint for users to subscribe to automated SMS reminders.

  1. Create Subscription API Route:
   import type { NextApiRequest, NextApiResponse } from 'next';
   import prisma from '@/lib/prisma';
   import { sendSms } from '@/lib/vonageClient';
   import { z } from 'zod';
   import { isSupportedTimeZone } from '@/utils/timezones';
   import { calculateNextReminderTime } from '@/utils/scheduler';

   const SubscriptionSchema = z.object({
     phoneNumber: z.string().trim().regex(/^\+?[1-9]\d{1,14}$/, { message: "Invalid phone number format (E.164 expected, e.g., +15551234567)" }),
     frequencyMinutes: z.coerce.number().int().min(1, { message: "Frequency must be at least 1 minute" }).max(10080, { message: "Frequency cannot exceed 1 week (10080 minutes)" }),
     timezone: z.string().refine(isSupportedTimeZone, { message: "Invalid or unsupported timezone" }),
   });

   type ResponseData = {
       message: string;
       subscriptionId?: string;
       error?: string;
       details?: z.ZodFormattedError<z.infer<typeof SubscriptionSchema>>['fieldErrors'];
   };

   export default async function handler(
     req: NextApiRequest,
     res: NextApiResponse<ResponseData>
   ) {
     if (req.method !== 'POST') {
       res.setHeader('Allow', ['POST']);
       return res.status(405).json({ message: `Method ${req.method} Not Allowed` });
     }


     const parseResult = SubscriptionSchema.safeParse(req.body);
     if (!parseResult.success) {
       console.warn('Subscription validation failed:', parseResult.error.flatten());
       return res.status(400).json({
         message: 'Invalid input data.',
         error: 'Validation Error',
         details: parseResult.error.format().fieldErrors,
       });
     }

     const { phoneNumber, frequencyMinutes, timezone } = parseResult.data;

     try {

       const nextReminderAt = calculateNextReminderTime(new Date(), frequencyMinutes, timezone);
       if (!nextReminderAt) {

           console.error(`Could not calculate next reminder time for tz: ${timezone}, freq: ${frequencyMinutes}`);
           throw new Error("Internal error: Could not calculate the initial reminder time.");
       }





       const subscription = await prisma.subscription.upsert({
         where: { phoneNumber: phoneNumber },
         update: {
           frequencyMinutes: frequencyMinutes,
           timezone: timezone,
           nextReminderAt: nextReminderAt,
           isActive: true,
           updatedAt: new Date(),
         },
         create: {
           phoneNumber: phoneNumber,
           frequencyMinutes: frequencyMinutes,
           timezone: timezone,
           nextReminderAt: nextReminderAt,
           isActive: true,
         },
       });

       console.log(`Subscription created/updated for ${phoneNumber}: ${subscription.id}`);


       try {
         await sendSms(
           phoneNumber,
           `Welcome! You're subscribed to reminders every ${frequencyMinutes} minutes. Your timezone is set to ${timezone}.`
         );
       } catch (smsError) {

         console.error(`Failed to send welcome SMS to ${phoneNumber} (Sub ID: ${subscription.id}), but subscription was saved:`, smsError);

       }


       return res.status(201).json({
         message: 'Subscription successful!',
         subscriptionId: subscription.id,
       });

     } catch (error: any) {
       console.error('Error processing subscription request:', error);


       return res.status(500).json({
         message: 'Failed to process subscription due to an internal error.',
         error: error.message || 'Internal Server Error',
       });
     }
   }
  1. Create Utility Functions: Create helpers for time zone validation and calculating the next reminder time.
   import { listTimeZones } from 'date-fns-tz';

   let supportedTimezones: Set<string> | null = null;

   function getTimezoneSet(): Set<string> {
       if (!supportedTimezones) {
           try {
               supportedTimezones = new Set(listTimeZones());
           } catch (e) {
               console.error("Failed to list timezones using date-fns-tz. Intl API might be required.", e);

               supportedTimezones = new Set(['UTC', 'America/New_York', 'Europe/London', 'Asia/Tokyo']);
           }
       }
       return supportedTimezones;
   }

   export const isSupportedTimeZone = (tz: string): boolean => {
       if (!tz) return false;
       return getTimezoneSet().has(tz);
   };

   export const getTimeZoneList = (): string[] => {

       return Array.from(getTimezoneSet()).sort();
   }
   import { addMinutes } from 'date-fns';
   import { utcToZonedTime, zonedTimeToUtc } from 'date-fns-tz';

   export const calculateNextReminderTime = (
       baseTime: Date,
       frequencyMinutes: number,
       timezone: string
   ): Date | null => {
       try {





           const baseTimeInUserTz = utcToZonedTime(baseTime, timezone);


           const nextTimeInUserTz = addMinutes(baseTimeInUserTz, frequencyMinutes);


           const nextTimeUtc = zonedTimeToUtc(nextTimeInUserTz, timezone);




           return nextTimeUtc;
       } catch (error) {
           console.error(`Error calculating next reminder time for base ${baseTime}, freq ${frequencyMinutes}, tz ${timezone}:`, error);
           return null;
       }
   };

Timezone Conversion Flow:

User Time (Local) β†’ UTC (Storage) β†’ User Time (Display/Calculation)
    ↓                    ↓                      ↓
Input/Display      Database Store         Next Calculation
America/NY 2pm β†’ 2025-01-15T19:00Z β†’ America/NY 3pm (next hour)
                                    ↓
                           2025-01-15T20:00Z (stored)

DST Handling: The date-fns-tz library automatically handles Daylight Saving Time transitions. When DST occurs:

  • Spring forward: A 1-hour reminder might skip if scheduled during the missing hour (2am β†’ 3am)
  • Fall back: A reminder scheduled during the repeated hour runs once at the first occurrence
  1. Testing the API Endpoint: Test this endpoint using curl or Postman once your development server is running (npm run dev):
   curl -X POST http://localhost:3000/api/subscribe \
     -H 'Content-Type: application/json' \
     -d '{
           "phoneNumber": "+12015550123",
           "frequencyMinutes": 60,
           "timezone": "America/New_York"
         }'

Expected Success Response (201 Created):

   {
     "message": "Subscription successful!",
     "subscriptionId": "clsomecuidstring123"
   }

Expected Validation Error Response (400 Bad Request):

   {
       "message": "Invalid input data.",
       "error": "Validation Error",
       "details": {
           "phoneNumber": [
               "Invalid phone number format (E.164 expected, e.g., +15551234567)"
           ]
       }
   }

5. Implementing Core Functionality (Scheduled Job)

This section handles sending the reminders. Create an API route containing the job logic, designed to be triggered externally by a scheduler like Vercel Cron Jobs.

  1. Create Cron Job API Route:
   import type { NextApiRequest, NextApiResponse } from 'next';
   import prisma from '@/lib/prisma';
   import { sendSms } from '@/lib/vonageClient';
   import { calculateNextReminderTime } from '@/utils/scheduler';

   type JobStats = {
       processed: number;
       sent: number;
       failed: number;
       errors: string[];
   };

   type ResponseData = {
     message: string;
     stats?: JobStats;
     error?: string;
   };

   async function runReminderJob(): Promise<JobStats> {
       const stats: JobStats = { processed: 0, sent: 0, failed: 0, errors: [] };
       console.log(`[${new Date().toISOString()}] Reminder Job triggered.`);

       const now = new Date();





       const batchSize = 100;
       let subscriptionsToSend;
       try {
            subscriptionsToSend = await prisma.subscription.findMany({
               where: {
                   isActive: true,
                   nextReminderAt: {
                       lte: now,
                   },
               },
               take: batchSize,
               orderBy: {
                   nextReminderAt: 'asc',
               },
           });
       } catch (dbError: any) {
            console.error("Cron job failed to query database:", dbError);
            stats.errors.push(`Database query failed: ${dbError.message}`);
            return stats;
       }

       stats.processed = subscriptionsToSend.length;
       console.log(`Found ${stats.processed} subscriptions due for reminders.`);

       if (stats.processed === 0) {
           console.log("No reminders due at this time.");
           return stats;
       }


       const processingPromises = subscriptionsToSend.map(async (sub) => {
           try {

               await sendSms(
                   sub.phoneNumber,
                   `Friendly reminder! Time for your scheduled check-in.`
               );
               stats.sent++;



               const nextReminderAt = calculateNextReminderTime(
                   sub.nextReminderAt,
                   sub.frequencyMinutes,
                   sub.timezone
               );

               if (!nextReminderAt) {

                   console.error(`Could not recalculate next reminder time for sub ${sub.id}. Leaving current nextReminderAt.`);

                   throw new Error(`Could not recalculate next reminder time for sub ${sub.id}`);
                }


               await prisma.subscription.update({
                   where: { id: sub.id },
                   data: {
                       nextReminderAt: nextReminderAt,

                   },
               });

               console.log(`Reminder sent and next time updated for sub ${sub.id} (${sub.phoneNumber}). Next at: ${nextReminderAt.toISOString()}`);

           } catch (error: any) {
               stats.failed++;
               const errorMessage = `Failed processing sub ${sub.id} (${sub.phoneNumber}): ${error.message}`;
               console.error(errorMessage);
               stats.errors.push(errorMessage);










                const retryTime = new Date(Date.now() + 5 * 60 * 1000);
                try {
                   await prisma.subscription.update({
                       where: { id: sub.id },
                       data: {
                           nextReminderAt: retryTime,

                       },
                   });
                   console.warn(`Sub ${sub.id} failed. Scheduled retry attempt around ${retryTime.toISOString()}`);
                } catch (updateError: any) {
                    console.error(`Failed to update sub ${sub.id} after error: ${updateError.message}`);

                }


           }
       });


       await Promise.allSettled(processingPromises);

       console.log(`Reminder Job finished. Processed: ${stats.processed}, Sent: ${stats.sent}, Failed: ${stats.failed}`);
       if (stats.errors.length > 0) {
           console.error("Errors encountered during job run:", stats.errors);
       }
       return stats;
   }

   export default async function handler(
     req: NextApiRequest,
     res: NextApiResponse<ResponseData>
   ) {



     const authHeader = req.headers.authorization;
     const triggerSecret = authHeader?.startsWith('Bearer ') ? authHeader.split(' ')[1] : null;


     if (triggerSecret === process.env.CRON_SECRET) {

         console.log("Cron job authorized via Bearer token.");
         const stats = await runReminderJob();
         return res.status(200).json({ message: 'Cron job executed.', stats });
     }


     if (req.query.secret === process.env.CRON_SECRET) {

         console.log("Cron job authorized via query parameter.");
         const stats = await runReminderJob();
         return res.status(200).json({ message: 'Cron job executed.', stats });
     }


     console.warn("Unauthorized attempt to trigger cron job.");
     return res.status(401).json({ message: 'Unauthorized' });
   }

Retry Logic Decision Flow:

Send SMS β†’ Success? ─Yesβ†’ Calculate Next Time β†’ Update DB β†’ Done
              β”‚
              No
              ↓
         Log Error
              ↓
    Retry Count < 3? ─Yesβ†’ Schedule Retry (+5min) β†’ Update DB
              β”‚
              No
              ↓
    Set isActive = false β†’ Notify Admin β†’ Done

Learn more about SMS messaging and API integrations:

  • Phone Number Lookup API - Validate phone numbers before sending SMS reminders
  • E.164 Phone Number Format Guide - Understanding international phone number formatting for SMS
  • Twilio vs Vonage SMS Comparison - Compare SMS API providers for your project

Simpler Alternative: Send the Same SMS With SMSRoute (No-KYC, Direct Routes)

If you want to replace legacy aggregator SDK boilerplate, multi-week compliance queues, and opaque carrier surcharges with a single HTTP request, SMSRoute delivers global SMS over direct Tier-1 carrier interconnects with instant API access and transparent per-country pricing.

curl -X POST https://api.sendsmsnokyc.com/messages \
  -H "x-api-key: $SMSROUTE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "to": ["+14155551234"],
    "channel": ["sms"],
    "body": "Your verification code is 482910"
  }'
  • Instant onboarding: No KYC paperwork or sales queues β€” grab an API key and fund with crypto (BTC/USDT) or card.
  • Direct Tier-1 routes: Verify real handset delivery, DLR latency, and Sender ID preservation before launch via SMSRoute Live Carrier Testing.
  • Transparent global pricing: Compare exact per-country termination rates on our live pricing page.

Ship Global SMS in One API Call

Skip legacy SDK sprawl and carrier bottlenecks. Send OTPs, alerts, and bulk campaigns across 190+ countries with SMSRoute.

Explore SMSRoute Developers β†’