Concept #120Mediumextended-ai-concepts

How to create a sub-workflow in n8n?

#gen-ai#mlops

Answer

How to Create a Sub-Workflow in n8n

Sub-workflows in n8n are reusable workflow components that can be called from other workflows — similar to functions in programming. They promote reusability and keep complex workflows organized.

Creating a Sub-Workflow

Step 1: Create the sub-workflow

  1. In n8n, create a new workflow
  2. Add a Workflow Trigger node as the starting point (set it to accept "Execute Workflow" trigger)
  3. Build the sub-workflow logic
  4. Use a Set node or Return to define what data to pass back
  5. Save with a descriptive name like "Send Email Notification"

Step 2: Call it from a parent workflow

Add an Execute Workflow node in your main workflow and select the sub-workflow by name.

n8n Sub-Workflow Types

Trigger TypeUse Case
Execute WorkflowCalled by another workflow (synchronous)
WebhookCalled via HTTP from any system
ScheduleRuns on a schedule independently

Practical Example

Sub-workflow: "Process Customer Data"

text
[Workflow Trigger (Execute Workflow)]
[Validate Input] — check required fields
[Enrich Data] — add timestamp, format name
[Return enriched_customer]

Parent workflow calling it:

text
[New Customer webhook arrives]
[Execute Workflow: "Process Customer Data"]
  Input: {name: "John", email: "john@example.com"}
[Use result: enriched_customer]
[Save to Database]
[Send Welcome Email]

Configuration in n8n

json
// Execute Workflow node settings
{
  "workflowId": "xyz123",  // ID of sub-workflow
  "mode": "executeWorkflow",
  "waitForSubWorkflow": true,  // Sync or async
  "inputData": {
    "customer": "{{ $json.customer_data }}"
  }
}

Passing Data To/From Sub-Workflows

javascript
// In sub-workflow — access input data
const inputData = $input.all()[0].json;
const customerName = inputData.customer.name;

// Return data from sub-workflow (using Set node)
return [{
  json: {
    processed: true,
    enriched_customer: {
      ...inputData.customer,
      processed_at: new Date().toISOString(),
      display_name: customerName.trim()
    }
  }
}];

Benefits of Sub-Workflows

BenefitDescription
ReusabilityCall the same logic from multiple workflows
MaintainabilityFix once, applies everywhere
OrganizationBreak complex workflows into smaller pieces
TestingTest sub-workflows independently
PermissionsShare sub-workflows across teams

Best Practices

  • Name sub-workflows clearly: "Send Slack Alert", "Validate Email Address"
  • Keep sub-workflows focused on ONE task
  • Document expected input/output in the workflow description
  • Use sub-workflows for any logic used in 2+ places
  • Version control your n8n workflows via GitHub integration