---
url: https://void.cloud/reference/api/workflows.md
description: >-
  Define a Cloudflare Workflow, start instances, and check their status with
  void/workflows.
---

# Workflows

Import the Workflow helpers from `void/workflows` in server code. The [Workflows guide](../../guide/workflows.md) shows how to schedule a post and start an instance from a route.

## `defineWorkflow(definition)` {#defineworkflow-definition}

Pass a `run` method to `defineWorkflow()`. You can also supply `params` and `name`. Default-export the definition from a module in `workflows/` and import it wherever you want to start an instance.

* **`run(event, step, { env, ctx })`** receives the instance event, Cloudflare's `WorkflowStep`, your app's typed Worker bindings, and its execution context. The value you return is available as `output` on the completed instance's status.
* **`params`** accepts a Standard Schema. Callers send its input type. Void validates the payload when the instance starts and passes the schema's output to `run` as `event.payload`. For example, a schema can accept a timestamp string and give your handler a `Date`. The same rule applies when Cloudflare starts an instance outside Void.
* **`name`** pins the resource identity when you rename or move the file. Run `void info` to see which name to use.

With no schema, annotate the event as `WorkflowEvent<MyParams>` or supply the type as `defineWorkflow<MyParams>()`. The default environment type is `CloudBindings`, just like `c.env` in a route. You can supply your own environment type with `defineWorkflow<MyParams, MyEnv>()`.

Cloudflare delivers an empty object when a caller omits params. If a schema should supply defaults in that case, make it accept `{}`. The handler receives exactly what the schema returns, including `undefined`.

## `defineWorkflowEntrypoint(definition)` {#defineworkflowentrypoint-definition}

Use `defineWorkflowEntrypoint({ class: MyWorkflow })` to register a native Cloudflare `WorkflowEntrypoint` class. You can also pass a `name` to pin its identity. Void infers the payload type from the class's `run(event, step)` method. Callers can omit params when that type accepts an empty object.

## `WorkflowApi` {#workflowapi}

Both helpers return a definition with these members.

| Member                           | What you can do                                                                                                |
| -------------------------------- | -------------------------------------------------------------------------------------------------------------- |
| `create(options?)`               | Start one instance. Pass `params` when the definition requires input. Use `id` to choose the instance ID.      |
| `createBatch({ instances })`     | Start up to 100 instances with individual IDs and options. Each entry accepts the same options as `create()`.  |
| `createBatch({ count, params })` | Start 1–100 instances with generated IDs and shared options. You can also pass `retention` and `locationHint`. |
| `get(id)`                        | Get an instance to read its status, send it an event, pause, resume, restart, or terminate it.                 |
| `class`                          | Access the registered entrypoint class.                                                                        |

Both batch forms return `{ created, errors }`. For example, if an ID already exists, Cloudflare reports that entry's `index`, `id`, `code`, and `message` in `errors`. A successfully created instance can still fail when it runs. [Cloudflare batch API](https://developers.cloudflare.com/workflows/build/workers-api/#createbatch)

You can pass an explicit binding as the first argument to `create()`, `createBatch()`, or `get()`. After `void prepare`, TypeScript checks calls such as `PublishPost.create(c.env.WORKFLOW_PUBLISH_POST, { params })` against the Workflow's input type.

## `WorkflowParamsError` {#workflowparamserror}

Void throws `WorkflowParamsError` at the entrypoint when the schema rejects the incoming payload. Its `issues` array contains a message and an optional field path for each problem. The instance fails before your `run` callback executes. Use `await instance.status()` to inspect the failure.

## Types {#types}

`WorkflowEvent`, `WorkflowStep`, `WorkflowContext`, `WorkflowCreateOptions`, `WorkflowBatchOptions`, `WorkflowApi`, and `WorkflowRun` are available from `void/workflows`.
