Tutorial

Cron Jobs Quickstart

Learn how to schedule cron jobs to run at specific times or intervals.
Table of Contents

Cron Jobs are available on all plans

The following guide will show you how to create a cron job on Vercel that executes every day at 5 am UTC.

  1. This function contains the code that will be executed by the cron job. This example uses a simple function that returns the user's region.

    Next.js (/app)
    Next.js (/pages)
    Other frameworks
    app/api/hello/route.ts
    export const dynamic = 'force-dynamic'; // static by default, unless reading the request
     
    export function GET(request: Request) {
      return new Response(`Hello from ${process.env.VERCEL_REGION}`);
    }
  2. Create or go to your vercel.json file and add the following code:

    vercel.json
    {
      "crons": [
        {
          "path": "/api/hello",
          "schedule": "0 5 * * *"
        }
      ]
    }

    The crons property is an array of cron jobs. Each cron job has two properties:

    • The path, which must start with /
    • The schedule property, which must be a string that represents a cron expression. In this example, the job is scheduled to execute every day at 5:00 am UTC
  3. When you deploy your project, Vercel's build process creates the cron job. Vercel invokes cron jobs only for production deployments and not for preview deployments

    You can also deploy to your production domain using the CLI:

    terminal
    vercel deploy --prod

Your cron job is now active and will call the /api/hello path every day at 5:00 am UTC.

Last updated on August 6, 2024