Skip to content
25K
Console

Workflow

The Workflow component lets you add Cloudflare Workflows to your app.

A Workflow is a durable, multi-step function that runs on Cloudflare Workers. You define it as a class that extends WorkflowEntrypoint and pass it to this component.

Minimal example

Define a workflow class:

src/workflow.ts
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from "cloudflare:workers";
export class OrderProcessor extends WorkflowEntrypoint<Env, { orderId: string }> {
async run(event: WorkflowEvent<{ orderId: string }>, step: WorkflowStep) {
await step.do("validate", async () => {});
await step.sleep("cooldown", "10 seconds");
await step.do("charge", async () => {});
}
}

Then register it with SST:

sst.config.ts
const processor = new sst.cloudflare.Workflow("OrderProcessor", {
handler: "src/workflow.ts",
className: "OrderProcessor",
});

Trigger from a Worker

Link the workflow to a Worker to get a native Cloudflare Workflow binding:

sst.config.ts
new sst.cloudflare.Worker("Api", {
handler: "src/api.ts",
url: true,
link: [processor],
});

Then invoke it from your worker code:

src/api.ts
import { Resource } from "sst";
export default {
async fetch(req: Request) {
const instance = await Resource.OrderProcessor.create({
params: { orderId: "ord_123" },
});
return Response.json({ id: instance.id });
},
};

Give the workflow access to other resources

sst.config.ts
const bucket = new sst.cloudflare.Bucket("Orders");
const processor = new sst.cloudflare.Workflow("OrderProcessor", {
handler: "src/workflow.ts",
className: "OrderProcessor",
link: [bucket],
});

Constructor

new Workflow(name, args, opts?)

Parameters

WorkflowArgs

className

Type Input<string>

The name of the class in your handler file that extends WorkflowEntrypoint.

Cloudflare Workflows are defined as classes that extend the WorkflowEntrypoint class. You must specify the name of the class so SST can bind to it.

Given this handler file:

src/workflow.ts
import { WorkflowEntrypoint } from "cloudflare:workers";
export class OrderProcessor extends WorkflowEntrypoint<Env, Params> {
async run(event, step) {
// ...
}
}

You would set:

{
className: "OrderProcessor"
}

environment?

Type Input<Record<string, Input<string>>>

Key-value pairs that are set as environment variables on the workflow’s Worker.

They can be accessed in your workflow through this.env.<key>.

{
environment: {
DEBUG: "true"
}
}

handler

Type Input<string>

Path to the handler file that exports the class that extends WorkflowEntrypoint.

The handler path is relative to the root of your repo or the sst.config.ts.

{
handler: "src/workflow.ts"
}

Type Input<any[]>

Link resources to your workflow. This will:

  1. Make them available on this.env inside your workflow class.
  2. Allow you to access them in your workflow using the SDK.

Takes a list of components to link to the workflow.

{
link: [bucket, db]
}

transform?

Type Object

Transform how this component creates its underlying resources.

transform.worker?

Type WorkerArgs | (args: WorkerArgs, opts: ComponentResourceOptions, name: string) => void

Transform the underlying Worker that hosts the workflow class.

transform.workflow?

Type WorkflowArgs | (args: WorkflowArgs, opts: ComponentResourceOptions, name: string) => void

Transform the Workflow resource.

Properties

className

Type Output<string>

The name of the workflow class.

nodes

Type Object

The underlying resources this component creates.

nodes.worker

Type Worker

The Cloudflare Worker that hosts the workflow class.

nodes.workflow

Type Workflow

The Cloudflare Workflow resource.

scriptName

Type Output<string>

The name of the Worker script that hosts the workflow class.

workflowName

Type Output<string>

The name of the Cloudflare Workflow.

SDK

Use the SDK in your runtime to interact with your infrastructure.


This is accessible through the Resource object in the SDK.

  • className string

    The name of the workflow class.

  • scriptName string

    The name of the Worker script that hosts the workflow class.

  • workflowName string

    The name of the Cloudflare Workflow.

Bindings

When you link a workflow to a worker, it automatically creates a Workflow binding on the worker. This lets you trigger, inspect, and manage workflow instances from your worker code.

src/api.ts
import { Resource } from "sst";
const instance = await Resource.MyWorkflow.create({ params: { hello: "world" } });