Scheduled tasks are for recurring work. To trigger a one-off task at a future time, use the
delay option when triggering.Defining a scheduled task
Useschedules.task() to define a task that runs on a schedule. The payload argument contains useful properties about the current invocation:
/trigger/first-scheduled-task.ts
| Field | Type | Description |
|---|---|---|
timestamp | Date | The time the task was scheduled to run (UTC). |
lastTimestamp | Date | undefined | The last time the task ran. |
timezone | string | IANA timezone of the schedule, e.g. "UTC" or "America/New_York". |
scheduleId | string | The ID of the schedule that triggered this run. |
externalId | string | undefined | An optional external ID set when creating the schedule. |
upcoming | Date[] | The next 5 scheduled run times. |
This task will not run on a schedule until you attach at least one schedule to it.
How to attach a schedule
There are two ways to attach a schedule to aschedules.task():
- Declarative — defined directly on the task. Synced automatically when you run
devordeploy. - Imperative — created from the dashboard or via SDK functions like
schedules.create().
A single scheduled task can have multiple schedules attached — a declarative one and/or many
imperative ones.
Declarative schedules
Add acron property to your schedules.task(). This is synced when you run the dev or deploy commands.
A string value runs in UTC:
/trigger/every-two-hours.ts
/trigger/daily-tokyo.ts
Imperative schedules
Create schedules dynamically at runtime usingschedules.create(). Imperative schedules can be created, updated, activated, deactivated, and deleted without redeploying code.
This is useful when each of your users needs their own schedule:
/trigger/reminder.ts
Next.js API route
Supported cron syntax
L means “last”. In the day-of-week field, 1L means the last Monday of the month. In the day-of-month field, L means the last day of the month.
Seconds are not supported in the cron syntax.
When schedules will not trigger
- Development environment — scheduled tasks only trigger while the dev CLI is running.
- Production/Staging — scheduled tasks only trigger for the current deployment. Tasks from previous deployments are not triggered.
Attaching schedules in the dashboard
Create your schedule
Fill in the form and press Create schedule.
| Field | Description |
|---|---|
| Task | The ID of the task to attach the schedule to. |
| Cron pattern | The schedule in cron format. |
| Timezone | The timezone the schedule runs in. Defaults to UTC. |
| External ID | An optional external ID, e.g. a user ID. |
| Deduplication key | If you pass the same key again, the schedule is updated instead of duplicated. |
| Environments | The environments this schedule applies to. |
Attaching schedules with the SDK
schedules.create()
Managing schedules with the SDK
Testing scheduled tasks
You can trigger a scheduled task from the Test page in the dashboard. ThescheduleId will always be sched_1234 in a test run.