Skip to content

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 for the API.

Example

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

index.jsjs
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,
    });
  },
};
wrangler.jsoncjsonc
{
  "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:

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 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.

Type to search…

↑↓ navigate↵ selectEsc close