Discover how to set up Netlify Scheduled Functions for your development needs.
Netlify Scheduled Functions provide the ability to perform various tasks. Follow these steps to set them up:
- Create a serverless function in
netlify/functionsdirectory in your repository. For example, you can create a file namedtest.js:
exports.handler = (event, context) => {
// do something
return { statusCode: 200 };
}
- Open or create the
netlify.tomlfile in your repository and configure the frequency at which you want the Netlify Scheduled Function to run. For example, if you want it to run every hour, add this configuration:
[functions."test"]
schedule = "@hourly"
Alternatively, you can set the schedule directly in the function itself without the need for an entry in netlify.toml:
const handler = (event, context) => {
// do something
return { statusCode: 200 };
}
exports.handler = schedule('@hourly', handler);
Here are some common schedules you can use:
@hourly: runs every hour at minute 0@daily: runs every day at 00:00@weekly: runs every Sunday at 00:00@monthlyand@yearlyare also available.
You can also use a cron expression, such as 5 4 * * * or any other expression. Crontab Guru is a helpful tool for creating cron expressions.
To manually invoke a function, you can use the command netlify functions:invoke test, replacing test with the name of your function.
Netlify Scheduled Functions can be used for a variety of purposes. For example, you can set up a function to automatically deploy your repository every day to post a scheduled blog post, with the publishing date set in advance.
To call the deploy webhook and achieve automatic deploys on Netlify, you can use the Fetch API.