createTasks() is the main entry point for deploying tasks to Cloudflare Workers. It takes a record of TaskDefinition values and returns a Durable Object class to export from your worker plus a fully type-safe client accessor.
Import paths
The package exposes two entry points:createTasks(definitions)
A record mapping task names to
TaskDefinition values. Every definition must have R = never — use withServices() to eliminate service requirements before passing here.The keys become the task names used in tasks(doNamespace, name) and are checked at compile time.Return value
A Durable Object class that implements
fetch(request) and alarm(). Export this from your worker entry point so Cloudflare can instantiate it.All task definitions share a single DO class. Each unique combination of task name and instance ID gets its own isolated Durable Object with its own storage.A callable that returns a
TaskHandle bound to a specific DO namespace and task name. The task name is checked against the keys of definitions at compile time.TasksAccessor<T>
TasksAccessor is a generic callable. When you pass a task name K, TypeScript infers the correct S and E types from your definitions record, giving you a fully typed TaskHandle with no casts.
TaskHandle<S, E>
Sends an event to a task instance. The
id identifies the specific instance — multiple instances of the same task can run concurrently with different IDs.The event payload is type-checked against the task’s event schema at compile time.Reads the current persisted state of a task instance. Returns
null if no state has been saved yet.If the task defines an onClientGetState hook, the hook runs and its return value is what getState() returns — not the raw stored state.Manually triggers the
onAlarm handler for a task instance without waiting for a scheduled alarm. Useful for testing or for driving alarm logic imperatively.Low-level API
These exports are for advanced setups where you need a custom Durable Object class instead of the generated one.makeTaskEngine(state, config)
makeCloudflareStorage(doStorage)
Storage Effect service layer. Pass state.storage from the DO constructor.
makeCloudflareAlarm(doStorage)
setAlarm, deleteAlarm, getAlarm) into the Alarm Effect service layer. Pass state.storage from the DO constructor — the alarm methods are on the same storage object.
Error types
Returned by
send(), getState(), and alarm() when the HTTP call to the Durable Object fails (network error, non-2xx response, etc.).Surfaced when a task name is not found in the registry — for example, when using the low-level
makeTaskEngine API with an incorrect task name.Surfaced when an incoming event payload fails to decode against the task’s event schema.
Surfaced when
onEvent or onAlarm fails and no onError handler is defined.