Skip to content
GitHubXDiscordRSS

Workflow

A Cloudflare Workflow allows you to define reusable logic that can be shared across multiple Workers.

Create a basic workflow that can be bound to a Worker.

import { Workflow } from "alchemy/cloudflare";
const workflow = Workflow("my-workflow", {
workflowName: "my-workflow",
className: "MyWorkflow",
});

Reference a workflow implemented in a different worker script using scriptName.

import { Workflow } from "alchemy/cloudflare";
const workflow = Workflow("shared-workflow", {
workflowName: "my-workflow",
className: "MyWorkflow",
scriptName: "shared-worker",
});

Bind a workflow to a Worker to use its functionality.

import { Worker, Workflow } from "alchemy/cloudflare";
const workflow = Workflow("my-workflow", {
workflowName: "my-workflow",
className: "MyWorkflow",
});
await Worker("my-worker", {
name: "my-worker",
script: "console.log('Hello, world!')",
bindings: {
WORKFLOW: workflow,
},
});

Alchemy takes care of migrations automatically when you rename the class name.

import { Workflow } from "alchemy/cloudflare";
const workflow = Workflow("my-workflow", {
className: "MyWorkflow",
className: "MyWorkflowV2",
});

You can share workflows across multiple Workers, allowing one Worker to trigger workflows defined in another Worker.

You can directly reference the workflow binding from the provider Worker:

import { Worker, Workflow } from "alchemy/cloudflare";
// Create the provider Worker with the Workflow
const host = await Worker("Host", {
entrypoint: "./workflow-provider.ts",
bindings: {
SHARED_PROCESSOR: Workflow("shared-processor", {
className: "SharedProcessor",
workflowName: "order-processing",
}),
},
});
// Create the client Worker using the provider's Workflow binding directly
const client = await Worker("client", {
entrypoint: "./client-worker.ts",
bindings: {
// Re-use the exact same Workflow binding from the provider worker
SHARED_PROCESSOR: host.bindings.SHARED_PROCESSOR,
},
});

Alternatively, when creating a Workflow binding in a client Worker, you can reference a workflow defined in another Worker by specifying the scriptName:

import { Worker, Workflow } from "alchemy/cloudflare";
const hostWorkerName = "host";
const sharedWorkflow = Workflow("shared-processor", {
className: "SharedProcessor",
workflowName: "order-processing",
scriptName: hostWorkerName,
});
// First, create the Worker that defines the Workflow
const host = await Worker("host", {
entrypoint: "./workflow-provider.ts",
name: hostWorkerName,
bindings: {
// Define the Workflow in this worker
SHARED_PROCESSOR: sharedWorkflow,
},
});
// Then, create a client Worker that uses the cross-script Workflow
const client = await Worker("client", {
entrypoint: "./client-worker.ts",
bindings: {
// Reference the same Workflow but specify which script it comes from
SHARED_PROCESSOR: sharedWorkflow,
},
});