Automate Your NextJS API Routes With Vercel Cron Jobs

Mert Enercan
3 min readJan 29, 2024

Hey there, again. Today, i want to introduce you Vercel Cron Jobs and how you can use it with your NextJS App. You can execute your NextJS API Routes by using it, and it’s surprisingly simple! Let’s dive in.

What Is a “Cron Job”?

A Cron Job is a Linux command used to schedule tasks for future execution, commonly employed to automate periodic tasks, such as sending out daily notifications. Certain scripts, like those utilized by Drupal and WHMCS, may necessitate the configuration of cron jobs to carry out specific functions.

Cron’s operations are guided by a crontab (cron table) file — a configuration file detailing shell commands scheduled for periodic execution. These crontab files are located in the repository where the lists of tasks and directives for the cron daemon are maintained. Users may possess their personal crontab files, and in many cases, there exists a system-wide crontab file, typically located in or a subdirectory of (/etc/cron.d) which is editable only by system administrators.

Each line of a crontab file represents a job, and looks like this:

# ┌───────────── minute (0–59)
# │ ┌───────────── hour (0–23)
# │ │ ┌───────────── day of the month (1–31)
# │ │ │ ┌───────────── month (1–12)
# │ │ │ │ ┌───────────── day of the week (0–6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>

OK but How Can I Use It?

Well, first things first you need to define your API route. Let’s say I have a API route called “cron” in the app/api/cron/route.ts directory.

import { NextResponse } from "next/server"

export async function GET() {
const result = "Helo, World! This is CRON route."

return NextResponse.json({ data: result })

}

All you need to do is create a vercel.json file in the root, and define the cron settings in that file. For example, my vercel.json file is looking like this 👇

{
"crons": [
{
"path": "/api/cron",
"schedule": "0 8 * * *"
}
]
}

When you deploy your app on Vercel using this configuration, Vercel will automatically identify your Cron Job command and execute it at the specified intervals. In this example, the Cron Job is set to run every day at 08:00 AM.

👇Here goes a CRON Time Generator, thank me later 👇
https://crontab.cronhub.io/

Keep in mind that the time zone may not align with your local time zone. Verify the location of your project on the project settings page.

Testing

You have the option to manually run your Cron Jobs by navigating to your Project -> Settings -> Cron Jobs Page. Additionally, you can check the Logs to ensure that they are functioning correctly.

And that’s basically it. Now you can periotically execute your NextJS API Routes!

🔗Here goes an example repo: https://github.com/mertthesamael/vercel-cron-example

🌐My Website: https://www.merto.dev/

🔈My LinkedIn: https://www.linkedin.com/in/mertenercan/

Do not forget to drink at least 2L water/day. Until next time!
Peace.

--

--