Cloudflare Workflow
This example creates a Cloudflare Workflow and a Worker that triggers it.
const processor = new sst.cloudflare.Workflow("OrderProcessor", { handler: "./workflow.ts", className: "OrderProcessor",});
const api = new sst.cloudflare.Worker("Api", { handler: "./api.ts", link: [processor], url: true,});import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep,} from "cloudflare:workers";
type Params = { orderId: string;};
export class OrderProcessor extends WorkflowEntrypoint<{}, Params> { async run(event: WorkflowEvent<Params>, step: WorkflowStep) { const validated = await step.do("validate", async () => { return { orderId: event.payload.orderId, valid: true }; });
await step.sleep("cooldown", "5 seconds");
const charged = await step.do("charge", async () => { return { orderId: validated.orderId, chargedAt: Date.now() }; });
return charged; }}import { Resource } from "sst";
export default { async fetch(req: Request) { const url = new URL(req.url);
if (req.method === "POST" && url.pathname === "/orders") { const orderId = crypto.randomUUID(); const instance = await Resource.OrderProcessor.create({ params: { orderId }, }); return Response.json({ orderId, instanceId: instance.id, status: await instance.status(), }); }
if (req.method === "GET" && url.pathname.startsWith("/orders/")) { const id = url.pathname.slice("/orders/".length); const instance = await Resource.OrderProcessor.get(id); return Response.json({ instanceId: instance.id, status: await instance.status(), }); }
return new Response( "POST /orders to trigger a workflow, GET /orders/:id to check status", { status: 200 }, ); },};View the full example.