Standalone tasks
A standalone task is the simplest unit of work. Usehatchet.task() to create one — it behaves like a workflow with a single step and exposes run(), runNoWait(), schedule(), and cron() directly.
workflow.ts
Task options
The name of the task. Used as the identifier in the Hatchet dashboard and as the key for parent output lookups in DAGs.
The function to execute. Receives the workflow input and a
Context instance. The return value becomes the task output.Number of times to retry the task on failure.
Maximum wall-clock time the task function is allowed to run. Accepts Go duration strings such as
"30s", "5m", "1h".Maximum time the task is allowed to wait in the queue before it starts. Accepts Go duration strings.
Exponential backoff configuration for retries.
factor is the base of the exponent and maxSeconds caps the delay.Rate limit keys consumed when this task runs. Each entry accepts
units, key, staticKey, dynamicKey, limit, and duration.Concurrency controls for this task. See Concurrency below.
desiredWorkerLabels
Record<string, { value: string | number; required?: boolean; weight?: number; comparator?: WorkerLabelComparator }>
Worker label requirements for routing this task to specific workers.
Durable tasks
Durable tasks survive worker restarts and support long-running waits backed by Hatchet’s durable event log. Create them withhatchet.durableTask() or workflow.durableTask().
The task function receives a DurableContext instead of a plain Context. DurableContext extends Context and adds:
ctx.sleepFor(duration)— pause execution for a durable duration (survives restarts)ctx.sleepUntil(date)— pause until a specific timestampctx.waitFor(conditions)— pause until one or more conditions are metctx.waitForEvent(key, expression?, schema?)— pause until a named user event arrivesctx.now()— get a memoized current timestamp (consistent across replays)ctx.spawnChild(workflow, input, opts?)— spawn a child workflow durablyctx.spawnChildren(children)— spawn multiple child workflows durably
workflow.durableTask():
Multi-step workflows (DAGs)
Usehatchet.workflow() to declare a workflow and then add tasks to it. Tasks can declare parents to form a directed acyclic graph (DAG).
workflow.ts
Workflow options
The name of the workflow.
A human-readable description shown in the Hatchet dashboard.
An optional version string for the workflow.
Shorthand for declaring cron and event triggers. Equivalent to
onCrons / onEvents.Cron expressions that automatically trigger the workflow. See Cron triggers.
Event keys that automatically trigger the workflow. See Event triggers.
Workflow-level concurrency controls. See Concurrency.
Default priority for runs of this workflow. Values:
Priority.LOW (1), Priority.MEDIUM (2), Priority.HIGH (3).Sticky strategy for scheduling:
StickyStrategy.SOFT or StickyStrategy.HARD.Default
executionTimeout, scheduleTimeout, retries, backoff, rateLimits, workerLabels, and concurrency applied to all tasks in this workflow. Individual tasks can override these values.A Zod schema for the workflow input. When provided, a JSON Schema is sent to the Hatchet backend for dashboard autocomplete.
Task context
TheContext object is passed as the second argument to every task function.
Accessing parent outputs
ctx.parentOutput(task) accepts either a CreateWorkflowTaskOpts reference or a task name string and returns the typed output of the parent task.
Logging
Other context methods
| Method | Returns | Description |
|---|---|---|
ctx.workflowRunId() | string | ID of the current workflow run. |
ctx.taskRunExternalId() | string | ID of the current task run. |
ctx.taskName() | string | Name of the currently executing task. |
ctx.workflowName() | string | Name of the current workflow. |
ctx.retryCount() | number | Number of times this task has been retried. |
ctx.additionalMetadata() | Record<string, string> | Metadata attached to the workflow run. |
ctx.errors() | Record<string, string> | Task run errors — use in onFailure handlers. |
ctx.triggeredByEvent() | boolean | Whether the workflow was triggered by an event. |
ctx.filterPayload() | Record<string, any> | Payload from the matched event filter. |
ctx.cancelled | boolean | Whether the task has been cancelled. |
ctx.abortController | AbortController | Abort controller for the task run. |
ctx.refreshTimeout(duration) | Promise<void> | Extend the execution timeout by duration. |
ctx.releaseSlot() | Promise<void> | Release the worker slot while the task continues. |
ctx.putStream(data) | Promise<void> | Stream data from the task. |
ctx.runChild(workflow, input, opts?) | Promise<P> | Run a child workflow and wait for its result. |
ctx.runNoWaitChild(workflow, input, opts?) | Promise<WorkflowRunRef<P>> | Enqueue a child workflow without waiting. |
ctx.bulkRunChildren(children) | Promise<P[]> | Run multiple child workflows in parallel, wait for all. |
ctx.bulkRunNoWaitChildren(children) | Promise<WorkflowRunRef<P>[]> | Enqueue multiple child workflows without waiting. |
Lifecycle hooks
Workflows supportonFailure and onSuccess handlers that run when all tasks fail or all tasks succeed:
Triggering runs
.run(input, opts?)
Triggers a run and waits for the result. Accepts a single input object or an array for bulk execution.
.runNoWait(input, opts?)
Enqueues a run and immediately returns a WorkflowRunRef without blocking.
Run options
Arbitrary key-value metadata attached to the run and visible in the dashboard.
Run priority:
Priority.LOW, Priority.MEDIUM, or Priority.HIGH.When
true and called from within a parent task, routes the run to the same worker.A deduplication key when spawning from a parent task.
When
true and input is an array, failures are returned as Error instances instead of rejecting the whole array..schedule(enqueueAt, input, opts?)
Schedules a one-off run at a specific Date.
.delay(durationSeconds, input, opts?)
Schedules a run to trigger after a delay in seconds.
.cron(name, expression, input, opts?)
Creates a named cron schedule that triggers the task on a recurring basis.
Event triggers
Workflows can be triggered automatically when events are pushed to Hatchet. UseonEvents (or the on.event shorthand):
on field accepts both event and cron triggers:
Cron triggers
Declare a recurring cron schedule directly on the workflow:Concurrency
Both workflows and tasks accept aconcurrency option to limit how many runs can execute simultaneously.
Concurrency options
A CEL expression evaluated against the workflow input to produce the concurrency key. For example,
"input.userId" groups runs by user.Maximum number of concurrent runs that share the same concurrency key.
Strategy when the limit is reached. Options:
ConcurrencyLimitStrategy.CANCEL_IN_PROGRESS— cancel the currently running runConcurrencyLimitStrategy.CANCEL_NEWEST— cancel the incoming runConcurrencyLimitStrategy.GROUP_ROUND_ROBIN— round-robin across concurrency groupsConcurrencyLimitStrategy.DROP_NEWEST— drop the incoming run (deprecated)ConcurrencyLimitStrategy.QUEUE_NEWEST— queue the new run, cancel the oldest queued run (deprecated)