How to create a cron job in Vercel
Tags: javascript, tutorial, programming, beginners
Published: 2023-03-20 11:58:22

Code: https://github.com/cesmunoz/vercel-cron-example

## Introduction

Vercel release a few weeks ago a feature that we were waiting for a long time ago. This feature was already on other platforms as Netlify, so I’m glad that we have it now on this platform.

A corn job is basically a work schedule that runs assignments at a certain specific time, these activities are called Cron Jobs.

Some examples of cron jobs can be:

- send a notification to a user at 9 AM
- do a process every 15 minutes

The schema of a cron job is the following:

```bash
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (0 is Sunday, 6 is Saturday)
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ │
# * * * * *
```

For more about the schema you can check it up over here: [https://crontab.guru/](https://crontab.guru/)

Some examples of corn job periods can be:

- Every hour = `0 * * * *`
- Every minute = `* * * * *`
- Every day of the month = `0 0 1 * *`
- Every week  = `0 0 * * 1`

The Cron kernel is an integrated [Linux](https://www.javatpoint.com/linux-tutorial) functionality that schedules the execution of programs on your scheme. Cron searches the crontab (Cron tables) for previously established instructions and files. You can set up a Cron job to immediately manage code or other instructions by using a specific format.

## Requirements

- Nodejs
- A Vercel account
- Vercel CLI (In this example we are pushing everything directly to vercel)

```bash
npm i -g vercel
```

I hope you have everything set up!. Let’s code 🙂

## Create a simple project

To do it run the following commands in the terminal

```bash
mkdir cron-job-vercel
cd cron-job-vercel
npm init -y

```

Then install the vercel package

```bash
npm install vercel --save-dev
```

Let’s create a folder called API and inside create a file where it will be the cron job

```bash
mkdir api
touch api/hello-world.js
```

In the hello-world.js paste the following code

```jsx
module.exports = (req, res) => {
  res.send({
    status: 200,
    message: "Hello world!!",
  });
};
```

Then add the most important thing about this to run as a cron job. **VERCEL.JSON**

```bash
touch vercel.json
```

## Cron configuration

Here you need to add the configuration of each cron job (if you have several cron jobs)

```bash
{
  "crons": [
    {
      "path": "/api/hello-world", // path of the handler
      "schedule": "* * * * *"  // configuration of the cron job
    }
  ]
}
```

*IMPORTANT!:* 
Take into consideration that if you are using your personal account (Hobby) you can have only 2 cron jobs total, and those can be triggered once per day. So adjust the example to your necessities.

## Deploy to Vercel

To deploy adjust your package.json. Please add the following script:

```json
"scripts": {
    "deploy": "vercel --prod"
},
```

And in the command line type `npm run deploy`. Wait a few minutes and you will have a response similar to this one:

```bash
npm run deploy

> cron-job-vercel@1.0.0 deploy
> vercel --prod
```

Go to vercel after the cron job starts of you run it manually and you will see something like this


![Image CronJob Execution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03btwkjey66hqqaziq0f.png)

## Conclusion

Cron jobs are super useful in the development of software. Vercel makes it super easy and handy to use it. 

See you next week. 

Happy Coding

Ces.