$npx -y skills add jackspace/ClaudeSkillz --skill cloudflare-workflowsComplete knowledge domain for Cloudflare Workflows - durable execution framework for building multi-step applications on Workers that automatically retry, persist state, and run for hours or days. Use when: creating long-running workflows, implementing retry logic, building event
| 1 | # Cloudflare Workflows |
| 2 | |
| 3 | **Status**: Production Ready ✅ |
| 4 | **Last Updated**: 2025-10-22 |
| 5 | **Dependencies**: cloudflare-worker-base (for Worker setup) |
| 6 | **Latest Versions**: wrangler@4.44.0, @cloudflare/workers-types@4.20251014.0 |
| 7 | |
| 8 | --- |
| 9 | |
| 10 | ## Quick Start (10 Minutes) |
| 11 | |
| 12 | ### 1. Create a Workflow |
| 13 | |
| 14 | Use the Cloudflare Workflows starter template: |
| 15 | |
| 16 | ```bash |
| 17 | npm create cloudflare@latest my-workflow -- --template cloudflare/workflows-starter --git --deploy false |
| 18 | cd my-workflow |
| 19 | ``` |
| 20 | |
| 21 | **What you get:** |
| 22 | - WorkflowEntrypoint class template |
| 23 | - Worker to trigger workflows |
| 24 | - Complete wrangler.jsonc configuration |
| 25 | |
| 26 | ### 2. Understand the Basic Structure |
| 27 | |
| 28 | **src/index.ts:** |
| 29 | |
| 30 | ```typescript |
| 31 | import { WorkflowEntrypoint, WorkflowStep, WorkflowEvent } from 'cloudflare:workers'; |
| 32 | |
| 33 | type Env = { |
| 34 | MY_WORKFLOW: Workflow; |
| 35 | }; |
| 36 | |
| 37 | type Params = { |
| 38 | userId: string; |
| 39 | email: string; |
| 40 | }; |
| 41 | |
| 42 | export class MyWorkflow extends WorkflowEntrypoint<Env, Params> { |
| 43 | async run(event: WorkflowEvent<Params>, step: WorkflowStep) { |
| 44 | // Access params from event.payload |
| 45 | const { userId, email } = event.payload; |
| 46 | |
| 47 | // Step 1: Do some work |
| 48 | const result = await step.do('process user', async () => { |
| 49 | return { processed: true, userId }; |
| 50 | }); |
| 51 | |
| 52 | // Step 2: Wait before next action |
| 53 | await step.sleep('wait 1 hour', '1 hour'); |
| 54 | |
| 55 | // Step 3: Continue workflow |
| 56 | await step.do('send email', async () => { |
| 57 | // Send email logic |
| 58 | return { sent: true, email }; |
| 59 | }); |
| 60 | |
| 61 | // Optional: return final state |
| 62 | return { completed: true, userId }; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Worker to trigger workflow |
| 67 | export default { |
| 68 | async fetch(req: Request, env: Env): Promise<Response> { |
| 69 | // Create new workflow instance |
| 70 | const instance = await env.MY_WORKFLOW.create({ |
| 71 | params: { userId: '123', email: 'user@example.com' } |
| 72 | }); |
| 73 | |
| 74 | return Response.json({ |
| 75 | id: instance.id, |
| 76 | status: await instance.status() |
| 77 | }); |
| 78 | } |
| 79 | }; |
| 80 | ``` |
| 81 | |
| 82 | ### 3. Configure Wrangler |
| 83 | |
| 84 | **wrangler.jsonc:** |
| 85 | |
| 86 | ```jsonc |
| 87 | { |
| 88 | "name": "my-workflow", |
| 89 | "main": "src/index.ts", |
| 90 | "compatibility_date": "2025-10-22", |
| 91 | "workflows": [ |
| 92 | { |
| 93 | "name": "my-workflow", |
| 94 | "binding": "MY_WORKFLOW", |
| 95 | "class_name": "MyWorkflow" |
| 96 | } |
| 97 | ] |
| 98 | } |
| 99 | ``` |
| 100 | |
| 101 | ### 4. Deploy and Test |
| 102 | |
| 103 | ```bash |
| 104 | # Deploy workflow |
| 105 | npm run deploy |
| 106 | |
| 107 | # Trigger workflow (visit in browser or curl) |
| 108 | curl https://my-workflow.<subdomain>.workers.dev/ |
| 109 | |
| 110 | # View workflow instances |
| 111 | npx wrangler workflows instances list my-workflow |
| 112 | |
| 113 | # Check instance status |
| 114 | npx wrangler workflows instances describe my-workflow <instance-id> |
| 115 | ``` |
| 116 | |
| 117 | --- |
| 118 | |
| 119 | ## WorkflowEntrypoint Class |
| 120 | |
| 121 | ### Extend WorkflowEntrypoint |
| 122 | |
| 123 | Every Workflow must extend `WorkflowEntrypoint` and implement a `run()` method: |
| 124 | |
| 125 | ```typescript |
| 126 | export class MyWorkflow extends WorkflowEntrypoint<Env, Params> { |
| 127 | async run(event: WorkflowEvent<Params>, step: WorkflowStep) { |
| 128 | // Workflow steps here |
| 129 | } |
| 130 | } |
| 131 | ``` |
| 132 | |
| 133 | **Type Parameters:** |
| 134 | - `Env` - Environment bindings (KV, D1, R2, etc.) |
| 135 | - `Params` - Type of workflow parameters passed via `event.payload` |
| 136 | |
| 137 | ### run() Method |
| 138 | |
| 139 | ```typescript |
| 140 | async run( |
| 141 | event: WorkflowEvent<Params>, |
| 142 | step: WorkflowStep |
| 143 | ): Promise<T | void> |
| 144 | ``` |
| 145 | |
| 146 | **Parameters:** |
| 147 | - `event` - Contains workflow metadata and payload |
| 148 | - `step` - Provides step methods (do, sleep, sleepUntil, waitForEvent) |
| 149 | |
| 150 | **Returns:** |
| 151 | - Optional return value (must be serializable) |
| 152 | - Return value available via instance.status() |
| 153 | |
| 154 | **Example:** |
| 155 | |
| 156 | ```typescript |
| 157 | export class OrderWorkflow extends WorkflowEntrypoint<Env, OrderParams> { |
| 158 | async run(event: WorkflowEvent<OrderParams>, step: WorkflowStep) { |
| 159 | const { orderId, customerId } = event.payload; |
| 160 | |
| 161 | // Access bindings via this.env |
| 162 | const order = await this.env.DB.prepare( |
| 163 | 'SELECT * FROM orders WHERE id = ?' |
| 164 | ).bind(orderId).first(); |
| 165 | |
| 166 | const result = await step.do('process payment', async () => { |
| 167 | // Payment processing |
| 168 | return { paid: true, amount: order.total }; |
| 169 | }); |
| 170 | |
| 171 | // Return final state |
| 172 | return { |
| 173 | orderId, |
| 174 | status: 'completed', |
| 175 | paidAmount: result.amount |
| 176 | }; |
| 177 | } |
| 178 | } |
| 179 | ``` |
| 180 | |
| 181 | --- |
| 182 | |
| 183 | ## Step Methods |
| 184 | |
| 185 | ### step.do() - Execute Work |
| 186 | |
| 187 | ```typescript |
| 188 | step.do<T>( |
| 189 | name: string, |
| 190 | config?: WorkflowStepC |