Next.js scheduled jobs

Dynamic cron jobs in Next.js

Static cron routes are useful for app maintenance. Dynamic cron jobs are different: your Next.js application creates schedules from product data, such as user preferences, tenant settings, or integration cadences.

The dynamic scheduling problem

A typical Next.js app can expose an API route, but product schedules usually are not fixed at deploy time. A customer might choose an hourly sync, a user might enable a Monday morning digest, or an admin might pause scheduled reports for one tenant.

When schedules are created by users, they belong in your application database and API workflow. You need to create, update, pause, and inspect them programmatically instead of editing deployment configuration.

Expose a scheduled webhook route

Start with a route handler that accepts a POST request and checks a shared secret. Keep the handler idempotent so repeated calls are safe.

// app/api/scheduled/hourly-sync/route.ts
export async function POST(request: Request) {
  if (request.headers.get("x-cron-secret") !== process.env.CRON_SECRET) {
    return new Response("Unauthorized", { status: 401 });
  }
  const payload = await request.json().catch(() => ({}));
  await runCustomerSync(payload.customerId);
  return Response.json({ ok: true });
}

Create schedules from your app

When a customer enables a recurring workflow, your backend can create a CronCoco job through the API. Store the returned job id with your customer or integration record so you can update or disable it later.

// server-side only
await fetch("https://api.croncoco.io/jobs", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.CRONCOCO_API_KEY}`,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    name: `sync-${customer.id}`,
    cron_expression: "0 * * * *",
    webhook_url: `${process.env.APP_URL}/api/scheduled/hourly-sync`,
    headers: { "x-cron-secret": process.env.CRON_SECRET },
    payload: { customerId: customer.id }
  })
});

Common product workflows

Operational tips

For broader platform context, read how to run cron jobs in a serverless app. If you are deciding whether built-in platform cron is enough, read the Vercel Cron alternative guide.