Skip to content

How to set up "cloud cron jobs" using Netlify Scheduled Functions

New Course Coming Soon:

Get Really Good at Git

Find out how to set up Netlify Scheduled Functions

Netlify Scheduled Functions allow us to do some interesting things.

Here’s how to set them up.

Create a serverless function in netlify/functions in your repository, for example test.js:

netlify/functions/test.js

exports.handler = (event, context) => {
  //do something
  return { statusCode: 200 }
}

Then in netlify.toml (create this file if you don’t have it yet) configure how frequently you want this Netlify Scheduled Function to run:

[functions."test"]
schedule = "@hourly"

Alternatively you can set this in the function itself, with no need for this entry:

const handler = (event, context) => {
  //do something
  return { statusCode: 200 }
}

exports.handler = schedule('@hourly', handler)

@hourly runs every hour at minute 0 @daily runs every day at 00:00 @weekly runs every Sunday at 00:00

@monthly and @yearly are available too.

You can also use a cron expression, like 5 4 * * * or any other expression (crontab guru is your friend)

You can also invoke a function manually using netlify functions:invoke test where test is the name of the function.

You can use Netlify Scheduled Functions for many different use cases.

I set a Netlify Scheduled Function to auto-deploy the repository every day to post a schedule blog post, for which I set the publishing date in advance.

I use the Fetch API to call the deploy webhook so I can do automatic deploys on Netlify.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching May 21, 2024. Join the waiting list!

Here is how can I help you: