Cron Jobs allow you to schedule tasks at specific intervals. Since the Vercel platform is event-driven, therefore not maintaining a running server, we recommend using a third-party service to schedule these tasks.

For example, let's say you want to hit a Serverless Function every 15 minutes. You can use any of the providers below to periodically hit your endpoint. If you need to execute a long job (greater than 30 seconds), you might consider breaking down the function into smaller pieces and using a queue (e.g. AWS SQS). This will allow you to track the state of the background job and potentially parallelize your task.

For example, with GitHub Actions you can define a new workflow .github/workflows/cron.yaml to run a cron every 15 minutes.

name: 15-minute-cron
on:
  schedule:
    - cron: '*/15 * * * *'
jobs:
  cron:
    runs-on: ubuntu-latest
    steps:
      - name: Call our API route
        run: |
          curl --request POST \
          --url 'https://yoursite.com/api/cron' \
          --header 'Authorization: Bearer ${{ secrets.API_SECRET_KEY }}'

An example GitHub Actions workflow for a cron job.

This Cron Job forwards an API key to securely access your Serverless Function. For example:

// pages/api/cron.ts

import { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse,
) {
  if (req.method === 'POST') {
    try {
      const { authorization } = req.headers;

      if (authorization === `Bearer ${process.env.API_SECRET_KEY}`) {
        res.status(200).json({ success: true });
      } else {
        res.status(401).json({ success: false });
      }
    } catch (err) {
      res.status(500).json({ statusCode: 500, message: err.message });
    }
  } else {
    res.setHeader('Allow', 'POST');
    res.status(405).end('Method Not Allowed');
  }
}

An example Serverless Function with authorization.

You can verify this is working locally by running:

curl --request POST \
     --url 'http://localhost:3000/api/cron' \
     --header 'Authorization: Bearer API_SECRET_KEY'

Use cURL to test your Cron Job locally.

Providers

Related

For more information on what to do next, we recommend the following articles: