---
title: "Workflows"
---

> Documentation Index
> Fetch the complete documentation index at: https://workerd.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Workflows

A Workflow is a durable function that runs as a named sequence of steps,
sleeps, and events. On one machine, workerd keeps each Workflow instance in
one Durable Object, under `.wrangler/state/workerd/workflows/`. In a
cluster, workerd copies the Workflow instances to the bucket, so another
instance can serve them. Read the
[Cloudflare Workflows documentation](https://developers.cloudflare.com/workflows/build/workers-api/)
for the API.

## Example

The Workflow example fetches a document and
stores the result of a durable step.

```js title="index.js"
import { WorkflowEntrypoint } from "cloudflare:workers";

export class ReportBuilder extends WorkflowEntrypoint {
  async run(event, step) {
    // A completed step returns its stored result when the Workflow resumes.
    return await step.do("build report", async () => {
      const response = await fetch(event.payload.url);
      if (!response.ok) throw new Error(`source answered ${response.status}`);
      const text = await response.text();
      return { bytes: text.length, lines: text.split("\n").length };
    });
  }
}

export default {
  async fetch(request, env) {
    const url = new URL(request.url);
    const id = url.searchParams.get("id");

    if (url.pathname === "/create") {
      const instance = await env.REPORTS.create({
        params: { url: url.searchParams.get("url") ?? "https://example.com" },
      });
      return new Response(instance.id);
    }

    if (url.pathname === "/status" && id) {
      const instance = await env.REPORTS.get(id);
      return Response.json(await instance.status());
    }

    return new Response("Use /create?url=URL or /status?id=ID.", {
      status: 404,
    });
  },
};
```

```jsonc title="wrangler.jsonc"
{
  "name": "workflow",
  "main": "index.js",
  "compatibility_date": "2026-08-18",
  "no_bundle": true,
  "workflows": [
    {
      "binding": "REPORTS",
      "name": "report-builder",
      "class_name": "ReportBuilder"
    }
  ]
}
```

Workflows are experimental, so they need `--experimental`:

```sh
npx workerd serve --experimental ./workflow/wrangler.jsonc
ID=$(curl -s http://localhost:8787/create)
curl "http://localhost:8787/status?id=$ID"
```

`/create` returns the new Workflow instance's id. The status is `running`
until the step has run, then `complete`, with the step's result as
`output`.

A Workflow instance's `run()` stays in memory until it finishes. It
runs again from its first line after a crash or a restart. It also runs
again after a sleep or an event wait of more than 60 seconds, when no other
part of it runs. A completed step then returns its stored result and does
not run its callback. A wait keeps its deadline. Put each side effect
inside a `step.do()` callback, as the
[rules of Workflows](https://developers.cloudflare.com/workflows/build/rules-of-workflows/)
require.

## Differences from Cloudflare

- A `workflows` entry takes `binding`, `name`, `class_name`,
  `limits.steps`, and `default_retention`. workerd ignores `concurrency`
  and `jurisdiction`. It refuses `schedules`, and a `script_name` that
  names another Worker.
- workerd keeps a finished Workflow instance for 30 days. `retention` or
  `default_retention` can set another period. `createBatch()` with an
  array ignores `retention`.
- workerd ignores `locationHint`. Only `createBatch()` with `instances`
  checks its value.
- workerd has no Workflows REST API. Use the binding instead.

See also [Cloudflare compatibility](https://workerd.dev/docs/cloudflare-compat/#services).

Source: https://workerd.dev/docs/services/workflows/index.mdx
