AgentSkillsCN

n8n-workflow

遵循Nx Monorepo的最佳实践,优化项目结构、缓存、受影响的命令、目标与生成器。在使用Nx工作区、创建或配置应用/库、优化CI与缓存,或解决Nx配置问题时使用此功能。。

SKILL.md
--- frontmatter
name: n8n-workflow
description: Create, modify, and understand n8n automation workflows. Use when building n8n workflow JSON files, configuring nodes (HTTP Request, Code, IF, Merge, Webhook, Schedule), writing expressions with {{ $json }}, or implementing flow logic (conditionals, loops, error handling). Triggers for requests involving n8n, workflow automation, or node-based pipeline creation.

n8n Workflow Creator

This skill provides guidance for creating valid n8n workflow JSON files.

🚨 Critical: Webhook Data Structure

Most common mistake: Webhook data is nested under .body, NOT at root!

javascript
// ❌ WRONG - Returns undefined
{{ $json.email }}

// ✅ CORRECT - Webhook data is under .body
{{ $json.body.email }}

This applies to expressions AND Code nodes.

Workflow Structure

json
{
  "name": "Workflow Name",
  "nodes": [...],
  "connections": {...},
  "active": false,
  "settings": { "executionOrder": "v1" }
}

The 5 Core Patterns

  1. Webhook Processing - Webhook → Validate → Transform → Respond
  2. HTTP API Integration - Trigger → HTTP Request → Transform → Action
  3. Database Operations - Schedule → Query → Transform → Write → Verify
  4. AI Agent Workflow - Trigger → AI Agent (Model + Tools) → Output
  5. Scheduled Tasks - Schedule → Fetch → Process → Deliver → Log

See patterns.md for complete examples.

Essential Nodes

NodeTypeUse Case
Manual Triggern8n-nodes-base.manualTriggerTest execution
Schedulen8n-nodes-base.scheduleTriggerCron-based runs
Webhookn8n-nodes-base.webhookHTTP endpoints
HTTP Requestn8n-nodes-base.httpRequestAPI calls
Coden8n-nodes-base.codeJavaScript/Python
Setn8n-nodes-base.setModify/create fields
IFn8n-nodes-base.ifConditional branching
Mergen8n-nodes-base.mergeCombine branches
Loop Over Itemsn8n-nodes-base.splitInBatchesBatch processing

See nodes.md for full configurations.

Expression Syntax

Expressions use {{ }} syntax:

javascript
{{ $json.fieldName }}              // Current item
{{ $json.body.email }}             // Webhook data (under .body!)
{{ $('NodeName').item.json.field }} // Other node's output
{{ $now }}                         // Current timestamp

❌ Don't use {{ }} in:

  • Code nodes (use JavaScript directly)
  • Webhook paths
  • Credential fields

See expressions.md for advanced patterns.

Code Node - Critical Rules

ALWAYS return array with json property:

javascript
// ✅ CORRECT
const items = $input.all();
return items.map(item => ({ json: { ...item.json, processed: true } }));

// ✅ CORRECT - Single item
return [{ json: { result: 'success' } }];

// ❌ WRONG - No return
const data = $input.first();
// forgot return!

// ❌ WRONG - Object instead of array
return { json: { result: 'success' } };

Best practices:

  • Validate input: if (!items || items.length === 0) return [];
  • Use null checks: item.json?.user?.email || 'default'
  • Try-catch for API calls
  • Filter early, process late

Common Gotchas

ProblemSolution
Can't access webhook dataUse $json.body.field, not $json.field
Expression shows as textWrap in {{ }}
Unexpected node orderCheck Settings → Execution Order (use v1)
Code node returns nothingAdd return statement
API returns 401/403Use Credentials section, not parameters

Connections Format

json
"connections": {
  "Source Node": {
    "main": [[{ "node": "Target Node", "type": "main", "index": 0 }]]
  }
}

IF node outputs: index: 0 = True, index: 1 = False

Best Practices

✅ Do:

  • Use descriptive node names ("Fetch Users", not "HTTP Request 1")
  • Set onError: "continueRegularOutput" for resilience
  • Test incrementally, node by node
  • Document complex workflows with notes
  • Handle empty data cases

❌ Don't:

  • Build workflows in one shot (iterate!)
  • Skip error handling
  • Hardcode credentials in parameters
  • Use Code node when built-in nodes suffice
  • Deploy without testing

Reference Documentation