This page documents the API for defining tasks in Trigger.dev using the @trigger.dev/sdk/v3 package. Tasks are the fundamental unit of work in Trigger.dev—async functions that can be triggered programmatically, scheduled via cron, and monitored through the platform.
API Coverage:
task() - Define manually-triggered tasks (implemented via createTask() in packages/trigger-sdk/src/v3/shared.ts132-282)schemaTask() - Define tasks with Zod schema validation (implemented via createSchemaTask() in packages/trigger-sdk/src/v3/shared.ts284-445)toolTask() - Define AI tool-compatible tasks (implemented via createToolTask() in packages/trigger-sdk/src/v3/shared.ts447-618)schedules.task() - Define cron-scheduled tasksCore Type Definitions:
TaskOptions<TIdentifier, TInput, TOutput, TInitOutput> - Task configuration interface packages/core/src/v3/types/tasks.tsTaskRunContext - Runtime context passed to task functions packages/core/src/v3/schemas/index.tsTask<TIdentifier, TInput, TOutput> - Task object with trigger methods packages/core/src/v3/types/tasks.tsRelated Documentation:
onSuccess, onFailure, onCancel): See page 3.4 (Lifecycle Hooks)Sources: packages/trigger-sdk/src/v3/shared.ts132-618 packages/trigger-sdk/src/v3/tasks.ts1-71 packages/core/src/v3/types/tasks.ts1-300
Tasks are defined using the task() function exported from @trigger.dev/sdk/v3. The task() function is a thin wrapper around createTask() packages/trigger-sdk/src/v3/shared.ts132-160 which constructs a Task<TIdentifier, TInput, TOutput> object.
Return Value: The task() function returns a Task object packages/core/src/v3/types/tasks.ts with the following methods:
trigger(payload, options?) - Implemented in packages/trigger-sdk/src/v3/shared.ts165-176batchTrigger(items, options?) - Implemented in packages/trigger-sdk/src/v3/shared.ts177-187triggerAndWait(payload, options?) - Returns a TaskRunPromise packages/trigger-sdk/src/v3/shared.ts188-201batchTriggerAndWait(items, options?) - Returns a promise of run handles packages/trigger-sdk/src/v3/shared.ts202-216The exported task is automatically discovered during deployment (via the trigger deploy command) and registered with the platform through the task indexing process.
Sources: packages/trigger-sdk/src/v3/shared.ts132-216 packages/trigger-sdk/src/v3/tasks.ts15-24 packages/core/src/v3/types/tasks.ts95-150
Diagram: TaskOptions Type Structure
Sources: packages/core/src/v3/types/tasks.ts17-94 packages/core/src/v3/lifecycleHooks/types.ts1-100 packages/core/src/v3/schemas/index.ts
The id property is a unique string identifier for the task within your project. Task IDs must be URL-friendly and unique across all tasks in an environment.
| Property | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Unique task identifier. URL-safe characters only. |
Task IDs are used when triggering tasks programmatically and appear in logs, traces, and the dashboard. Changing a task's ID creates a new task from the platform's perspective.
Sources: packages/trigger-sdk/CHANGELOG.md203-206 docs/guides/examples/fal-ai-image-to-cartoon.mdx31-53
The run property is an async function containing the task's execution logic. It receives the task payload and a context object.
| Property | Type | Required | Description |
|---|---|---|---|
run | async (payload, { ctx }) => Promise<TOutput> | Yes | Task execution function |
The function signature is:
Sources: packages/trigger-sdk/CHANGELOG.md125-149 docs/guides/examples/scrape-hacker-news.mdx80-276
Tasks support type-safe payload and output definitions using Zod schemas. Schemas provide both compile-time TypeScript types and runtime validation.
This approach provides compile-time type checking but no runtime validation.
Sources: packages/trigger-sdk/CHANGELOG.md36-46 docs/guides/examples/fal-ai-image-to-cartoon.mdx30-80
The schemaTask() function is implemented via createSchemaTask() packages/trigger-sdk/src/v3/shared.ts284-445 and adds runtime schema validation using Zod schemas. The function uses getSchemaParseFn() packages/trigger-sdk/src/v3/shared.ts295-313 to create a validation function from the Zod schema.
schemaTask() Configuration:
| Property | Type | Required | Description |
|---|---|---|---|
schema | ZodSchema | Yes | Zod schema for validating task payload |
output | ZodSchema | No | Zod schema for validating return value |
Validation Behavior:
parse() method before run() executes. The #parsePayload() method packages/core/src/v3/workers/taskExecutor.ts133-135 handles this validation.TaskExecutor after run() completes packages/core/src/v3/workers/taskExecutor.ts251-294z.infer<typeof schema>.Sources: packages/trigger-sdk/src/v3/shared.ts284-445 packages/core/src/v3/workers/taskExecutor.ts133-135 packages/core/src/v3/types/tasks.ts27-40
Diagram: Task Execution with Schema Validation
Sources: packages/core/src/v3/workers/taskExecutor.ts92-359 packages/trigger-sdk/src/v3/shared.ts284-313
Tasks automatically retry on failure using exponential backoff. Retry behavior is configurable via the retry property.
| Property | Type | Default | Description |
|---|---|---|---|
maxAttempts | number | 3 | Maximum number of execution attempts |
minTimeout | number (seconds) | 1 | Initial retry delay |
maxTimeout | number (seconds) | 60 | Maximum retry delay |
factor | number | 2 | Exponential backoff multiplier |
randomize | boolean | true | Add jitter to retry delays |
The retry mechanism follows this pattern:
minTimeout (e.g., 2s)minTimeout * factor (e.g., 4s)maxAttempts reached or maxTimeout limit hitRandomization (jitter) helps prevent thundering herd problems when many tasks fail simultaneously.
Sources: packages/trigger-sdk/CHANGELOG.md364-389 packages/core/CHANGELOG.md457-461
Sources: packages/trigger-sdk/CHANGELOG.md154-195 packages/core/CHANGELOG.md356-389
The machine property specifies the compute resources allocated to task execution. Trigger.dev provides several preset configurations optimized for different workload types.
| Preset | vCPU | Memory | Use Case |
|---|---|---|---|
micro | 0.5 | 0.5 GB | Lightweight tasks, API calls |
small-1x | 1 | 1 GB | Default, general purpose |
small-2x | 2 | 2 GB | CPU-intensive operations |
medium-1x | 2 | 4 GB | Memory-intensive operations |
medium-2x | 4 | 8 GB | Large data processing |
large-1x | 4 | 16 GB | Very large datasets |
large-2x | 8 | 32 GB | Maximum resources |
Machine presets can be overridden when triggering a task:
Sources: packages/trigger-sdk/CHANGELOG.md644-672 packages/core/CHANGELOG.md240-269
The queue property controls task concurrency and execution behavior. Queues allow grouping tasks and managing how many instances can run simultaneously.
| Property | Type | Description |
|---|---|---|
name | string | Queue identifier (optional, defaults to task ID) |
concurrencyLimit | number | Max concurrent runs (per environment) |
QUEUED state)Sources: packages/trigger-sdk/CHANGELOG.md154-160 docs/manual-setup.mdx1-24
The maxDuration property sets a timeout for task execution, specified in seconds.
| Property | Type | Default | Description |
|---|---|---|---|
maxDuration | number | No limit | Maximum execution time in seconds |
When a task exceeds maxDuration:
TIMED_OUT stateSources: packages/trigger-sdk/CHANGELOG.md169 packages/core/CHANGELOG.md482
The run function receives a RunFnParams<TInit> object packages/core/src/v3/types/tasks.ts as its second parameter, which includes the ctx: TaskRunContext providing access to run metadata, environment information, and platform utilities.
The TaskRunContext interface is defined in packages/core/src/v3/schemas/index.ts and passed to the task executor in packages/core/src/v3/workers/taskExecutor.ts92-96:
Sources: packages/core/src/v3/schemas/index.ts packages/core/src/v3/types/tasks.ts95-110 packages/core/src/v3/workers/taskExecutor.ts92-96
Diagram: TaskRunContext Structure and Code Mappings
Sources: packages/core/src/v3/schemas/index.ts packages/core/src/v3/workers/taskExecutor.ts92-96
The init property is typed as OnInitHookFunction<TPayload, TInitOutput> packages/core/src/v3/lifecycleHooks/types.ts13-15 and allows running setup code once per task execution environment (process or container). The init hook is executed by TaskExecutor#callInitFunctions() packages/core/src/v3/workers/taskExecutor.ts180
The init hook can return TaskInitOutput, which is defined as Record<string, any> | void | undefined packages/core/src/v3/lifecycleHooks/types.ts4
The init hook:
TaskInitHookParams<TPayload> containing { ctx, payload, task, signal } packages/core/src/v3/lifecycleHooks/types.ts6-10TaskInitOutput object that becomes available in run as the init parameter packages/core/src/v3/lifecycleHooks/types.ts4lifecycleHooks.registerGlobalInitHook() and lifecycleHooks.registerTaskInitHook() packages/core/src/v3/lifecycleHooks/manager.ts20-22Sources: packages/core/src/v3/lifecycleHooks/types.ts1-17 packages/core/src/v3/workers/taskExecutor.ts180 packages/core/src/v3/lifecycleHooks/manager.ts20-22
The schedules.task() function defines tasks that run automatically on a cron schedule. These tasks do not require manual triggering and execute according to their configured schedule.
| Property | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Unique task identifier |
cron | string | Yes | Cron expression (5-field or 6-field format) |
run | function | Yes | Execution function (receives timestamp payload) |
timezone | string | No | IANA timezone (default: "UTC") |
Cron Expression Format:
"* * * * *" (minute, hour, day, month, day-of-week)"* * * * * *" (second, minute, hour, day, month, day-of-week)Payload Structure: Scheduled tasks automatically receive a payload with:
timestamp: Date - When the schedule was triggeredtimezone: string - The timezone used for schedulinglastTimestamp: Date (optional) - The previous execution timestampConfiguration Options: Scheduled tasks support the same configuration options as regular tasks:
retry - Retry configurationmachine - Machine presetqueue - Queue configurationmaxDuration - Timeout in secondsinit - Initialization hookSources: packages/trigger-sdk/CHANGELOG.md1-50 packages/core/CHANGELOG.md1-100
This example demonstrates a fully-configured task using most available options:
Sources: packages/trigger-sdk/CHANGELOG.md1-206
Sources: packages/trigger-sdk/CHANGELOG.md1-206 packages/core/CHANGELOG.md1-632 packages/cli-v3/CHANGELOG.md1-700
Refresh this wiki