---
url: https://void.cloud/guide/workflows.md
description: >-
  Schedule work, retry individual steps, and start Cloudflare Workflows from a
  Void route.
---

# Workflows

A Workflow can keep running after a request ends. For example, a route can schedule a blog post and return an ID immediately. The Workflow waits until publication time, writes the post, and retries the write if it fails.

## Define a Workflow

Default-export `defineWorkflow()` from `workflows/publish-post.ts`. This example uses Zod (`vp add zod`) to accept a timestamp string from the caller and give the Workflow a `Date`:

```ts
// workflows/publish-post.ts
import { kv } from 'void/kv';
import { defineWorkflow } from 'void/workflows';
import { z } from 'zod';

export default defineWorkflow({
  params: z.object({
    slug: z.string().min(1),
    title: z.string().min(1),
    publishAt: z.iso.datetime().transform((value) => new Date(value)),
  }),
  async run({ payload: post }, step) {
    await step.sleepUntil('publication time', post.publishAt);
    await step.do('publish post', () => kv.put(`posts:${post.slug}`, post.title));
    return { slug: post.slug };
  },
});
```

`sleepUntil()` pauses the instance. When it wakes, `step.do()` writes the title to KV. Cloudflare remembers completed steps, so resuming this Workflow skips a write that already succeeded. See the [Cloudflare step API](https://developers.cloudflare.com/workflows/build/workers-api/#workflowstep) for retry options.

You can use the same [KV](./kv.md) and [database](./database.md) helpers as in a route. If a step needs a D1 bookmark, call `setSession({ bookmark })` before its first query on each attempt.

## Start an instance from a route

Call `create()` from a route to schedule the post ten minutes from now:

```ts
// routes/api/publish.ts
import { defineHandler } from 'void';
import PublishPost from '../../workflows/publish-post.ts';

export const POST = defineHandler(async () => {
  const instance = await PublishPost.create({
    params: {
      slug: 'hello-void',
      title: 'Hello, Void!',
      publishAt: new Date(Date.now() + 10 * 60_000).toISOString(),
    },
  });
  return { id: instance.id };
});
```

`POST /api/publish` returns the instance ID. To check whether the post is still waiting or has been published, get the instance with `await PublishPost.get(id)` and call `await instance.status()`.

### Validate params

Validation happens when the instance starts, including instances started from the Cloudflare dashboard or REST API. An invalid `publishAt` fails the instance with `WorkflowParamsError` before it sleeps. If you want your endpoint to return HTTP 400 for a bad date, validate the request in the route before calling `create()`.

Use any [Standard Schema](https://standardschema.dev/) validator, including Zod, Valibot, or ArkType. Keep transforms deterministic because Cloudflare can run the entrypoint again when it resumes an instance. For TypeScript-only checks, omit `params` and pass your input type to `defineWorkflow<MyParams>()`.

### Start a batch

Use `{ instances }` when each post has its own params or ID. Use `{ count, params }` to start several instances with the same input. Both forms accept up to 100 instances and return `{ created, errors }`. Check `errors` for entries Cloudflare could not create. Each error includes its position in the batch. [Workflow API reference](../reference/api/workflows.md#workflowapi)

## Native Workflow classes

If you already have a class that extends Cloudflare's `WorkflowEntrypoint`, you can keep its `run(event, step)` implementation. For example, if your class lives in `src/import-posts.ts`, register it in `workflows/import-posts.ts`.

```ts
// workflows/import-posts.ts
import { defineWorkflowEntrypoint } from 'void/workflows';
import { ImportPosts } from '../src/import-posts.ts';

export default defineWorkflowEntrypoint({ class: ImportPosts });
```

The default export has the same `create()`, `createBatch()`, and `get()` methods. TypeScript reads the payload type from the class's `run` method.

## Names and bindings

Void turns `workflows/publish-post.ts` into the `WORKFLOW_PUBLISH_POST` binding and the `PublishPostWorkflow` class. On Cloudflare the Workflow is named `<worker-name>_publish-post`. Instances belong to that name. Before moving a deployed Workflow, run `void info` and copy the suggested `name` into its definition. New and existing instances will still belong to the same Workflow.

Keep `workflows/` at the project root, including when you set `sourceDir`. A path like `workflows/posts/publish.ts` uses the name `posts-publish`. Prefix a file or directory with `_` to keep step helpers beside the Workflow without registering them. In a CommonJS project, use `.mts` or `.mjs` for Workflow modules. If your tsconfig has an explicit `include` list, add `workflows`.

The definition's `name` must be a static string containing an ASCII letter or digit. To use an existing account-wide Workflow name or shorten a generated name that exceeds 64 characters, set the name on its binding.

```ts
// void.config.ts
import { defineConfig } from 'void/config';

export default defineConfig({
  cloudflare: {
    workflows: [
      { binding: 'WORKFLOW_PUBLISH_POST', class_name: 'PublishPostWorkflow', name: 'publish-post' },
    ],
  },
});
```

If your config already declares the Workflow in `cloudflare.exports`, use the generated class name as the export key. Void uses the name and settings from that export.

## Deploy to Cloudflare

Run the Workflow locally in a native Void app, then deploy it to your Cloudflare account with `void deploy --platform cloudflare`. [Workers Free includes Workflows](https://developers.cloudflare.com/workflows/reference/limits/).

Recurring Workflow schedules, more than 1,024 steps, concurrency above 100, or default retention longer than 3 days require [Workers Paid](https://dash.cloudflare.com/?to=/:account/workers/plans). Paid accounts support up to 25,000 steps and 30 days of retention. To keep completed or failed instances longer, set `default_retention.success_retention` or `default_retention.error_retention` to a duration like `"7 days"` or a whole number of milliseconds. For paid settings, your API token needs **Account Settings: Read** to check the account limits and **Workers Scripts: Edit** to publish.

If Cloudflare cannot stage a Workflow addition, rename, or removal, review the change and run `void deploy --platform cloudflare --atomic`. The new version will receive traffic before Void checks readiness.

Void platforms do not host Workflows. To publish just the static site there, build it and run `void deploy --platform void --dir dist/client`.
