Skip to main content

Cloudflare Workflows Example

This example demonstrates using Cloudflare Workflows for stateful, long-running processes. The workflow example is included in the comprehensive Cloudflare Worker example.

Features

  • Workflows: Stateful workflow orchestration
  • OFAC Processing: Example compliance workflow
  • Worker Integration: Workflows bound to Workers
  • Durable Execution: Guaranteed workflow completion

Project Setup

1
Install Dependencies
2
npm install alchemy
3
Create alchemy.run.ts
4
Define a workflow in your worker configuration:
5
import alchemy from "alchemy";
import { Worker, Workflow } from "alchemy/cloudflare";

const app = await alchemy("cloudflare-worker");

export const worker = await Worker("worker", {
  name: `${app.name}-${app.stage}-worker`,
  entrypoint: "./src/worker.ts",
  bindings: {
    WORKFLOW: Workflow("OFACWorkflow", {
      className: "OFACWorkflow",
      workflowName: "ofac-workflow",
    }),
  },
  url: true,
});

await app.finalize();
6
Create Workflow Class
7
Create src/workflow.ts:
8
import { WorkflowEntrypoint, WorkflowStep } from "cloudflare:workers";

export class OFACWorkflow extends WorkflowEntrypoint {
  async run(event: unknown, step: WorkflowStep) {
    // Step 1: Validate input
    const validated = await step.do("validate", async () => {
      console.log("Validating OFAC data...");
      return { valid: true };
    });

    if (!validated.valid) {
      return { status: "failed", reason: "validation" };
    }

    // Step 2: Check OFAC database
    const result = await step.do("check-ofac", async () => {
      console.log("Checking OFAC database...");
      // Simulate OFAC check
      await step.sleep("wait-for-response", 1000);
      return { match: false };
    });

    // Step 3: Process result
    const processed = await step.do("process", async () => {
      if (result.match) {
        return { status: "blocked", reason: "OFAC match" };
      }
      return { status: "approved" };
    });

    return processed;
  }
}
9
Use Workflow in Worker
10
Trigger workflows from your worker:
11
import { env } from "cloudflare:workers";

export default {
  async fetch(request: Request) {
    // Start a new workflow instance
    const instance = await env.WORKFLOW.create({
      id: crypto.randomUUID(),
    });

    // Get workflow status
    const status = await instance.status();

    return new Response(
      JSON.stringify({
        instanceId: instance.id,
        status: status,
      }),
      {
        headers: { "Content-Type": "application/json" },
      }
    );
  },
};
12
Deploy
13
Deploy your workflow:
14
npm exec tsx alchemy.run.ts

Key Features Explained

Workflow Definition

Workflows are defined with a class name and workflow name:
Workflow("OFACWorkflow", {
  className: "OFACWorkflow",
  workflowName: "ofac-workflow",
})

Workflow Steps

Steps provide durability guarantees:
const result = await step.do("step-name", async () => {
  // This code runs exactly once
  return { data: "result" };
});

Sleep/Delays

Workflows can pause execution:
await step.sleep("wait-for-response", 1000);

Instance Management

Create and manage workflow instances:
const instance = await env.WORKFLOW.create({ id: "unique-id" });
const status = await instance.status();

Use Cases

  • Compliance workflows (OFAC, KYC)
  • Multi-step approval processes
  • Long-running data processing
  • Saga pattern implementations
  • Retry-able operations with state

Source Code

View the complete source code: examples/cloudflare-worker

Build docs developers (and LLMs) love