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.
// Get all trigger nodes in the workflowconst triggerNodes = workflow.getTriggerNodes();// These are nodes that start workflows automatically// Examples: Webhook, Schedule Trigger, Email Trigger
// Find the topmost nodes (entry points) for a given nodeconst highestNodes = workflow.getHighestNode('MyNode');// These are the nodes with no incoming connections// in the execution path to MyNode
// Global static data (shared across all nodes)const globalData = workflow.getStaticData('global');globalData.executionCount = (globalData.executionCount || 0) + 1;// Node-specific static dataconst nodeData = workflow.getStaticData('node', node);nodeData.lastProcessed = new Date().toISOString();// Static data is stored as an ObservableObject// Changes are automatically trackedif (workflow.staticData.__dataChanged) { // Save workflow with updated static data}
Workflows can have custom settings that affect execution:
Copy
Ask AI
const settings: IWorkflowSettings = { timezone: 'America/New_York', // Timezone for date operations saveManualExecutions: true, // Save manual test runs saveExecutionProgress: false, // Save intermediate states executionTimeout: 3600, // Max execution time (seconds) executionOrder: 'v1', // Execution algorithm version errorWorkflow: 'error-handler-workflow' // Workflow to run on errors};workflow.setSettings(settings);
When renaming nodes, all references must be updated:
Copy
Ask AI
// Rename a node and update all referencesworkflow.renameNode('OldNodeName', 'NewNodeName');// This updates:// 1. The node itself// 2. All connections to/from the node// 3. All expressions referencing the node// 4. Node references in parameters
Node names cannot use restricted JavaScript keywords like hasOwnProperty, constructor, prototype, etc. These names could cause security issues.