Functions: Introducing Scheduled Functions

Scheduled Functions are now available. They run on a timer instead of in response to document events. Use them for daily digests, cache expiration, periodic content syncs, or any task that needs to happen on a fixed cadence.

Run code on a schedule

Define a scheduled function with defineScheduledFunction from @sanity/blueprints. Schedules accept either a UNIX cron expression or an explicit object with minute, hour, dayOfWeek, month, and dayOfMonth fields.

import {defineBlueprint, defineScheduledFunction} from '@sanity/blueprints'

export default defineBlueprint({
resources: [
defineScheduledFunction({
name: 'expire-cache',
event: {expression: '0 0 * * *'},
}),
],
})

Inside the function, the scheduledEventHandler helper from @sanity/functions gives you typed access to the run context.

import {scheduledEventHandler} from '@sanity/functions'

export const handler = scheduledEventHandler(async ({context}) => {
const time = new Date().toLocaleTimeString()
console.log(`👋 Your Sanity Function was called at ${time}`)
})

Get started

Add a scheduled function with the CLI:

npx sanity@latest functions add --name expire-cache --type scheduled-function --language ts

Scheduled functions run independently of any single project, so they require an organization-scoped blueprint stack. If your stack is project-scoped, run npx sanity@latest blueprints promote before deploying.

Read the Create a Scheduled Function quickstart for a step-by-step walkthrough, or browse the Functions documentation.