Workflow.createRun()

The .createRun() method initializes a new workflow run instance. It generates a unique run ID for tracking and returns a start function that begins workflow execution when called.

One reason to use .createRun() vs .execute() is to get a unique run ID for tracking, logging, or subscribing via .watch().

Usage

const { runId, start, watch } = workflow.createRun();
 
const result = await start();

Returns

runId:

string
Unique identifier for tracking this workflow run

start:

() => Promise<WorkflowResult>
Function that begins workflow execution when called

watch:

(callback: (record: WorkflowRunState) => void) => () => void
Function that accepts a callback function that will be called with each transition of the workflow run

resume:

({stepId: string, context: Record<string, any>}) => Promise<WorkflowResult>
Function that resumes a workflow run from a given step ID and context

resumeWithEvent:

(eventName: string, data: any) => Promise<WorkflowResult>
Function that resumes a workflow run from a given event name and data

Error Handling

The start function may throw validation errors if the workflow configuration is invalid:

try {
  const { runId, start, watch, resume, resumeWithEvent } = workflow.createRun();
  await start({ triggerData: data });
} catch (error) {
  if (error instanceof ValidationError) {
    // Handle validation errors
    console.log(error.type); // 'circular_dependency' | 'no_terminal_path' | 'unreachable_step'
    console.log(error.details);
  }
}