SDK Reference
Typescript SDK
Getting Workflow Run Results

Getting Workflow Run Results

It is possible to wait for or stream the results of a workflow run by getting a WorkflowRunRef. This is the return value of the runWorkflow and getWorkflowRun methods on the hatchet.admin client, or the spawnWorkflow method on a Context object. For example:

get-workflow-run.ts
import Hatchet from "@hatchet-dev/typescript-sdk";
 
const hatchet = Hatchet.init();
 
const workflowRun = hatchet.admin.getWorkflowRun(
  "5a3a617d-1200-4ee2-92e6-be4bd27ca26f",
);
const result = await workflowRun.result();
 
console.log("workflow run result:", result);

This method takes the workflow_run_id as a parameter and returns a reference to the workflow run.

🪓

If you need to get the workflow run id from a different method than where it was invoked, you can store the value of the getWorkflowRunId method of the return value of getWorkflow or spawnWorkflow. For example:

const workflowRun = hatchet.admin.runWorkflow("ManualTriggerWorkflow", {
  test: "test",
});
 
const workflowRunId = await workflowRun.getWorkflowRunId();
 
console.log(`spawned workflow run: ${workflowRunId}`);

Note that the result method must be awaited. It returns a dict of each step run's result in the workflow. For example:

{
    "step1": {
        "result1": "success"
    },
    "step2": {
        "result2": "success"
    }
}

Streaming Results

It is also possible to stream the results of a workflow run as each step is executed. This can be done via the stream method on the WorkflowRunRef object:

stream-workflow-run.ts
import Hatchet from "@hatchet-dev/typescript-sdk";
 
const hatchet = Hatchet.init();
 
const workflowRun = hatchet.admin.getWorkflowRun(
  "5a3a617d-1200-4ee2-92e6-be4bd27ca26f",
);
 
const listener = workflowRun.stream();
 
for await (const event of listener) {
  console.log(event.type, event.payload);
}

Note that this is an async generator, so you must use for await to iterate over the events.