How to set up "cloud cron jobs" using Netlify Scheduled Functions
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.
download all my books for free
- javascript handbook
 - typescript handbook
 - css handbook
 - node.js handbook
 - astro handbook
 - html handbook
 - next.js pages router handbook
 - alpine.js handbook
 - htmx handbook
 - react handbook
 - sql handbook
 - git cheat sheet
 - laravel handbook
 - express handbook
 - swift handbook
 - go handbook
 - php handbook
 - python handbook
 - cli handbook
 - c handbook
 
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.