> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/cloudflare/cloudflare-typescript/llms.txt
> Use this file to discover all available pages before exploring further.

# Workflows

> Orchestrate long-running tasks with Cloudflare Workflows

Cloudflare Workflows enables you to build reliable, long-running workflows that can survive Worker restarts and failures. Use the API to create, manage, and monitor workflow executions.

## Overview

Access the Workflows API:

```typescript theme={null}
import Cloudflare from 'cloudflare';

const client = new Cloudflare({
  apiToken: process.env.CLOUDFLARE_API_TOKEN,
});

// Access Workflows resources
const workflows = client.workflows;
```

## Workflows

Manage workflow definitions.

### Create or update a workflow

Create or modify a workflow definition.

```typescript theme={null}
const workflow = await client.workflows.update(
  'my-workflow',
  {
    account_id: '023e105f4ecef8ad9ca31a8372d0c353',
    class_name: 'MyWorkflow',
    script_name: 'my-worker',
  }
);
```

<ParamField path="workflow_name" type="string" required>
  Name for the workflow
</ParamField>

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

<ParamField path="class_name" type="string" required>
  The exported Workflow class name from your Worker
</ParamField>

<ParamField path="script_name" type="string" required>
  The Worker script that contains the Workflow class
</ParamField>

<ResponseField name="id" type="string">
  The workflow ID
</ResponseField>

<ResponseField name="name" type="string">
  The workflow name
</ResponseField>

<ResponseField name="class_name" type="string">
  The Workflow class name
</ResponseField>

<ResponseField name="script_name" type="string">
  The associated Worker script
</ResponseField>

<ResponseField name="created_on" type="string">
  ISO 8601 timestamp when the workflow was created
</ResponseField>

<ResponseField name="modified_on" type="string">
  ISO 8601 timestamp when the workflow was last modified
</ResponseField>

### List workflows

Retrieve all workflows in your account.

```typescript theme={null}
for await (const workflow of client.workflows.list({
  account_id: '023e105f4ecef8ad9ca31a8372d0c353',
})) {
  console.log(workflow);
}
```

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

<ParamField path="search" type="string">
  Filter workflows by name
</ParamField>

<ParamField path="page" type="number">
  Page number for pagination
</ParamField>

<ParamField path="per_page" type="number">
  Number of workflows per page (default: 20, max: 100)
</ParamField>

<ResponseField name="instances" type="object">
  Instance count statistics
</ResponseField>

<ResponseField name="instances.queued" type="number">
  Number of queued instances
</ResponseField>

<ResponseField name="instances.running" type="number">
  Number of running instances
</ResponseField>

<ResponseField name="instances.complete" type="number">
  Number of completed instances
</ResponseField>

<ResponseField name="instances.errored" type="number">
  Number of errored instances
</ResponseField>

### Get a workflow

Retrieve details about a specific workflow.

```typescript theme={null}
const workflow = await client.workflows.get(
  'my-workflow',
  { account_id: '023e105f4ecef8ad9ca31a8372d0c353' }
);
```

<ParamField path="workflow_name" type="string" required>
  The workflow name
</ParamField>

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

### Delete a workflow

Delete a workflow definition.

```typescript theme={null}
await client.workflows.delete(
  'my-workflow',
  { account_id: '023e105f4ecef8ad9ca31a8372d0c353' }
);
```

<ParamField path="workflow_name" type="string" required>
  The workflow name to delete
</ParamField>

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

## Instances

Manage workflow execution instances.

### Create an instance

Start a new workflow instance.

```typescript theme={null}
const instance = await client.workflows.instances.create(
  'my-workflow',
  {
    account_id: '023e105f4ecef8ad9ca31a8372d0c353',
    params: { userId: '123', action: 'process' },
  }
);
```

<ParamField path="workflow_name" type="string" required>
  The workflow name
</ParamField>

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

<ParamField path="params" type="object">
  Parameters to pass to the workflow instance
</ParamField>

<ResponseField name="id" type="string">
  The instance ID
</ResponseField>

<ResponseField name="workflow_name" type="string">
  The workflow name
</ResponseField>

<ResponseField name="status" type="string">
  Instance status: 'queued', 'running', 'complete', 'errored', or 'terminated'
</ResponseField>

<ResponseField name="created_on" type="string">
  ISO 8601 timestamp when the instance was created
</ResponseField>

### List instances

Retrieve all instances for a workflow.

```typescript theme={null}
for await (const instance of client.workflows.instances.list(
  'my-workflow',
  { account_id: '023e105f4ecef8ad9ca31a8372d0c353' }
)) {
  console.log(instance);
}
```

<ParamField path="workflow_name" type="string" required>
  The workflow name
</ParamField>

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

<ParamField path="status" type="string">
  Filter by status: 'queued', 'running', 'complete', 'errored', 'paused', 'waiting', 'terminated', or 'waitingForPause'
</ParamField>

<ParamField path="page" type="number">
  Page number for pagination
</ParamField>

<ParamField path="per_page" type="number">
  Number of instances per page (default: 20, max: 100)
</ParamField>

### Get an instance

Retrieve details about a specific workflow instance.

```typescript theme={null}
const instance = await client.workflows.instances.get(
  'my-workflow',
  instanceId,
  { account_id: '023e105f4ecef8ad9ca31a8372d0c353' }
);
```

<ParamField path="workflow_name" type="string" required>
  The workflow name
</ParamField>

<ParamField path="instance_id" type="string" required>
  The instance ID
</ParamField>

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

<ResponseField name="id" type="string">
  The instance ID
</ResponseField>

<ResponseField name="status" type="string">
  Current instance status
</ResponseField>

<ResponseField name="output" type="object">
  The workflow output (if completed)
</ResponseField>

<ResponseField name="error" type="object">
  Error details (if errored)
</ResponseField>

### Bulk instance operations

Terminate or pause multiple instances.

```typescript theme={null}
const result = await client.workflows.instances.bulk(
  'my-workflow',
  {
    account_id: '023e105f4ecef8ad9ca31a8372d0c353',
    action: 'terminate',
    instance_ids: [instanceId1, instanceId2],
  }
);
```

<ParamField path="workflow_name" type="string" required>
  The workflow name
</ParamField>

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

<ParamField path="action" type="string" required>
  Action to perform: 'terminate' or 'pause'
</ParamField>

<ParamField path="instance_ids" type="array" required>
  Array of instance IDs to act on
</ParamField>

## Versions

Track workflow version history.

### List versions

Retrieve version history for a workflow.

```typescript theme={null}
for await (const version of client.workflows.versions.list(
  'my-workflow',
  { account_id: '023e105f4ecef8ad9ca31a8372d0c353' }
)) {
  console.log(version);
}
```

### Get a version

Retrieve details about a specific workflow version.

```typescript theme={null}
const version = await client.workflows.versions.get(
  'my-workflow',
  versionId,
  { account_id: '023e105f4ecef8ad9ca31a8372d0c353' }
);
```

## Defining workflows in Workers

Define a Workflow class in your Worker:

```typescript theme={null}
import { WorkflowEntrypoint, WorkflowStep } from 'cloudflare:workers';

export class MyWorkflow extends WorkflowEntrypoint {
  async run(event, step) {
    // Step 1: Fetch data
    const data = await step.do('fetch-data', async () => {
      const response = await fetch('https://api.example.com/data');
      return response.json();
    });
    
    // Step 2: Process data
    const result = await step.do('process-data', async () => {
      return processData(data);
    });
    
    // Step 3: Store result
    await step.do('store-result', async () => {
      await env.DB.prepare('INSERT INTO results VALUES (?)')
        .bind(JSON.stringify(result))
        .run();
    });
    
    return { success: true, result };
  }
}

export default {
  async fetch(request, env) {
    // Start a workflow instance
    const instance = await env.MY_WORKFLOW.create({
      params: { userId: '123' },
    });
    
    return Response.json({ instanceId: instance.id });
  },
};
```

## Best practices

1. **Idempotent steps**: Each step should be idempotent to handle retries
2. **Step naming**: Use descriptive step names for easier debugging
3. **Error handling**: Use try-catch blocks within steps for granular error handling
4. **State management**: Store state in step results, not in memory
5. **Timeouts**: Be aware of step execution time limits
6. **Monitoring**: Regularly check instance status and error rates
