Skip to main content

Creating Workflows

Workflows are the foundation of n8n automation. A workflow is a collection of nodes connected together to automate tasks and process data. This guide explains how workflows are structured and how to work with them programmatically.

Workflow Structure

A workflow in n8n consists of several key components defined by the WorkflowParameters interface:

Key Components

Nodes are the building blocks of workflows. Each node represents a specific action or operation.

Creating a Workflow

The Workflow class manages workflow execution and node relationships. Here’s how workflows are initialized:
1

Initialize Workflow

Create a new workflow instance with required parameters:
2

Node Type Resolution

During initialization, n8n resolves node types and applies default parameters:
3

Configure Connections

Connections are stored in two formats for efficient traversal:

Node Management

Accessing Nodes

Finding Trigger and Poll Nodes

Connection Traversal

Connections are indexed by source node by default. To find parent nodes, you must first invert the connections using mapConnectionsByDestination().

Finding Parent and Child Nodes

Finding Highest Parent Nodes

Start Node Selection

The workflow determines where to begin execution based on node types and connections:
Starting nodes are defined in constants.ts as STARTING_NODE_TYPES:
  • n8n-nodes-base.manualTrigger
  • n8n-nodes-base.executeWorkflowTrigger
  • n8n-nodes-base.errorTrigger
  • n8n-nodes-base.evaluationTrigger
  • n8n-nodes-base.formTrigger

Static Data Management

Static data persists across workflow executions:

Workflow Settings

Workflows can have custom settings that affect execution:

Pin Data

Pin data allows you to use fixed data for specific nodes during development:

Renaming Nodes

When renaming nodes, all references must be updated:
Node names cannot use restricted JavaScript keywords like hasOwnProperty, constructor, prototype, etc. These names could cause security issues.

Best Practices

  1. Always validate node types: Check that node types exist before adding nodes to workflows
  2. Use workflow methods for traversal: Don’t manually traverse connections; use built-in methods
  3. Respect execution order: Understand that workflows can have different execution orders (v1 vs legacy)
  4. Handle disabled nodes: Disabled nodes (disabled: true) are skipped during execution
  5. Consider timezone settings: Date operations use the workflow’s timezone setting

Next Steps

Nodes Overview

Learn about node types, parameters, and outputs

Expressions

Work with dynamic data using expressions

Error Handling

Handle errors and implement retry logic

Execution Modes

Understand different workflow execution modes