SDK Reference
Typescript SDK
Child Workflows

Running Child Workflows

Hatchet supports running child workflows from within a parent workflow. This allows you to create complex, dynamic workflows that don't map to the concept of a DAG.

To run a child workflow, you can use the context.spawnWorkflow method. For example:

child-workflow.ts
const parentWorkflow: Workflow = {
  id: "parent-workflow",
  description: "Example workflow for spawning child workflows",
  on: {
    event: "fanout:create",
  },
  steps: [
    {
      name: "parent-spawn",
      timeout: "10s",
      run: async (ctx) => {
        const promises: Promise<string>[] = [];
 
        for (let i = 0; i < 5; i++) {
          promises.push(
            ctx
              .spawnWorkflow("child-workflow", {
                input: `child-input-${i}`,
              })
              .result(),
          );
        }
 
        const results = await Promise.all(promises);
 
        return {
          results,
        };
      },
    },
  ],
};

Usage

Type Parameters

  • Q: The type of the input data for the workflow. Default is JsonValue.
  • P: The type of the output data from the workflow. Default is JsonValue.

Parameters

  • workflowName (string): The name of the workflow to be spawned. This will be concatenated with the client's namespace to form the full workflow name.
  • input (Q): The input data for the workflow. The type of this data is specified by the generic type parameter Q.
  • key (string, optional): A caching key for the child workflow. If this is not set, the child workflow will be cached on the index that it was triggered at. The cache is used on retries of the parent workflow so that child workflows which were already triggered are skipped.

Returns

  • WorkflowRunRef: A reference to the workflow run, with the output data type specified by the generic type parameter P.

Exceptions

  • HatchetError: Thrown if there is any error during the workflow spawning process, with the error message provided.