Lý thuyết
35 phút
Bài 5/7

n8n Interface và First Workflow

Làm quen giao diện n8n, các node cơ bản, và xây dựng workflow đầu tiên

n8n Interface và First Workflow

Thực hành là cách học tốt nhất! Bài này sẽ guide bạn qua giao diện n8n và tạo workflow hoàn chỉnh đầu tiên.

🎯 Mục tiêu

  • Nắm vững giao diện n8n
  • Hiểu các loại nodes cơ bản
  • Tạo workflow đầu tiên
  • Debug và test workflow

1. Cài Đặt n8n

Option 1: n8n Cloud (Recommended cho beginners)

  1. Truy cập n8n.io
  2. Sign up free trial
  3. Bắt đầu ngay, không cần setup

Option 2: Self-hosted với Docker

Bash
1# Chạy n8n với Docker (1 command)
2docker run -it --rm \
3 --name n8n \
4 -p 5678:5678 \
5 -v n8n_data:/home/node/.n8n \
6 n8nio/n8n
7
8# Truy cập: http://localhost:5678

Option 3: npm (Node.js required)

Bash
1npm install n8n -g
2n8n start
3
4# Truy cập: http://localhost:5678

2. Giao Diện n8n

2.1. Overview

Text
1┌─────────────────────────────────────────────────────────────┐
2│ [Logo] Workflows ▼ Credentials Executions ⚙️ │ ← Top Bar
3├─────────────────────────────────────────────────────────────┤
4│ │
5│ │
6│ CANVAS AREA │ ← Drag & Drop
7│ │ Nodes Here
8│ [Trigger] ────► [Node] ────► [Node] │
9│ │
10│ │
11├─────────────────────────────────────────────────────────────┤
12│ [+] Add Node [▶] Execute [💾] Save [⚡] Active │ ← Bottom Bar
13└─────────────────────────────────────────────────────────────┘

2.2. Key Areas

AreaFunction
CanvasNơi thiết kế workflow, drag-drop nodes
Node PanelClick [+] để thêm nodes mới
Node SettingsClick node để configure
Execution LogXem history và debug
CredentialsQuản lý API keys, logins

2.3. Shortcuts

ShortcutAction
SpaceOpen node search
Ctrl/Cmd + SSave workflow
Ctrl/Cmd + EExecute workflow
DeleteRemove selected node
Ctrl/Cmd + C/VCopy/Paste nodes

3. Node Types

3.1. Trigger Nodes

Khởi động workflow khi có event.

NodeTrigger when
Manual TriggerClick Execute (testing)
Schedule TriggerCron/interval time
WebhookHTTP request received
Email Trigger (IMAP)New email arrives
App TriggersGoogle Forms, Slack, etc.

3.2. Action Nodes (Apps)

Tương tác với external services.

Text
1Popular integrations:
2├── Google (Sheets, Drive, Gmail, Calendar)
3├── Communication (Slack, Discord, Telegram)
4├── CRM (HubSpot, Salesforce, Airtable)
5├── Social (Twitter, LinkedIn, Facebook)
6└── Others (400+ apps)

3.3. Logic Nodes

Control flow của data.

NodeFunction
IFBranch based on condition
SwitchMultiple branches
MergeCombine data streams
Split In BatchesProcess in chunks
WaitDelay execution

3.4. Data Nodes

Transform và manipulate data.

NodeFunction
SetCreate/modify fields
CodeCustom JavaScript/Python
HTTP RequestCall any API
HTML ExtractScrape web pages
Date & TimeFormat dates

4. Hands-on: First Workflow

Workflow: Automated Daily Quote

Mỗi ngày lấy quote inspirational và gửi đến email/Slack.

Step 1: Create New Workflow

  1. Click "New Workflow"
  2. Đặt tên: "Daily Inspiration Quote"

Step 2: Add Schedule Trigger

  1. Press Space hoặc click [+]
  2. Search "Schedule"
  3. Add Schedule Trigger
  4. Configure:
    • Trigger Interval: Every Day
    • Hour: 8
    • Minute: 0
Text
1┌─────────────────┐
2│ Schedule Trigger│
3│ Every day 8:00 │
4└────────┬────────┘
5
6

Step 3: Add HTTP Request Node

  1. Click [+] → Search "HTTP Request"
  2. Connect từ Schedule Trigger
  3. Configure:
Text
1Method: GET
2URL: https://api.quotable.io/random
3
4Options:
5- Response Format: JSON

Test output:

JSON
1{
2 "content": "The only way to do great work is to love what you do.",
3 "author": "Steve Jobs"
4}

Step 4: Add Set Node (Format Message)

  1. Add Set node
  2. Configure fields:
Text
1Mode: Manual
2
3Add Field:
4- Name: message
5- Value: "💡 Quote of the Day\n\n{{ $json.content }}\n\n— {{ $json.author }}"

Step 5: Add Email/Slack Node

Option A: Gmail

To: your@email.com
Subject: Daily Inspiration Quote
Message: Use expression $json.message

Option B: Slack

Channel: #general
Message: Use expression $json.message

Final Workflow

Text
1┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐
2│ Schedule │────►│ HTTP Request │────►│ Set │────►│ Gmail │
3│ (8 AM daily)│ │(Get quote API)│ │(Format msg) │ │(Send email) │
4└──────────────┘ └──────────────┘ └──────────────┘ └──────────────┘

Step 6: Test & Activate

  1. Click Execute Workflow để test
  2. Check output của mỗi node
  3. Click Active toggle để enable

5. Working with Data

5.1. Accessing Data

JavaScript
1// Previous node output
2{{ $json.fieldName }}
3
4// Specific node by name
5{{ $node["HTTP Request"].json.content }}
6
7// All items
8{{ $items() }}
9
10// Current item index
11{{ $itemIndex }}

5.2. Expressions Panel

Click ⚡ icon trong field để mở Expression Editor:

Expression Editor preview:

  • Input: $json.content
  • Output: "The only way to do great work..." └─────────────────────────────────────────┘
Text
1### 5.3. Common Expressions
2
3```javascript
4// String operations
5{{ $json.name.toUpperCase() }}
6{{ $json.text.substring(0, 100) }}
7
8// Date formatting
9{{ $now.format('YYYY-MM-DD') }}
10{{ $now.minus(1, 'day').toISO() }}
11
12// Conditionals
13{{ $json.score >= 50 ? "Pass" : "Fail" }}
14
15// Array operations
16{{ $json.items.length }}
17{{ $json.tags.join(", ") }}
18
19// Math
20{{ $json.price * 1.1 }} // Add 10%
21{{ Math.round($json.value) }}

6. Error Handling

6.1. Common Errors

ErrorCauseSolution
Connection failedBad credentialsCheck API keys
404 Not FoundWrong URLVerify endpoint
401 UnauthorizedAuth missingAdd credentials
TimeoutSlow APIIncrease timeout
Invalid expressionSyntax errorCheck expression syntax

6.2. Debug Tips

  1. Execute single node: Click node → Execute Node
  2. Check output: Click node → Output tab
  3. View execution log: Left panel → Executions
  4. Console log: Use Code node với console.log()

6.3. Error Workflow

Text
1Main Workflow
2
3 ├── Success → Continue
4
5 └── Error → Error Workflow
6
7 ├── Log to Sheet
8 ├── Send alert
9 └── Retry logic

7. Best Practices

7.1. Naming Conventions

Text
1❌ Bad:
2HTTP Request, HTTP Request 1, HTTP Request 2
3
4✅ Good:
5Get Quote API, Send to Slack, Save to Sheet

7.2. Organize Large Workflows

Text
1Main Workflow
2
3 ├── Sub-workflow: Data Collection
4
5 ├── Sub-workflow: Processing
6
7 └── Sub-workflow: Distribution

7.3. Use Sticky Notes

Add notes trong canvas để document workflow logic.

7.4. Version Control

  • Export workflows as JSON
  • Store in Git repository
  • Track changes over time

8. Practice Exercise

Challenge: Build a Weather Alert System

Requirements:

  1. Schedule: Run at 6 AM daily
  2. Get weather data from API: https://api.open-meteo.com/v1/forecast?latitude=21.03&longitude=105.85&daily=precipitation_probability_max
  3. IF rain probability exceeds 60%, send notification
  4. Message format: "☔ Rain alert! [probability]% chance of rain today"

Hints:

  • Use IF node với condition: {{ $json.daily.precipitation_probability_max[0] }} greater than 60
  • Extract probability: {{ $json.daily.precipitation_probability_max[0] }}

📝 Quiz

  1. Node nào bắt đầu workflow?

    • Action node
    • Trigger node
    • Logic node
    • Data node
  2. Cách access data từ node trước?

    • $previous.data
    • {{ $json.fieldName }}
    • node.output
    • getData()
  3. IF node dùng để làm gì?

    • Chạy code custom
    • Call API
    • Branch workflow dựa trên condition
    • Format data
  4. Cách test workflow?

    • Click Execute Workflow
    • Chỉ có thể test sau khi Active
    • Không thể test, phải đợi trigger
    • Export và import lại

🎯 Key Takeaways

  1. Canvas là nơi thiết kế visual workflows
  2. Trigger → Nodes → Output là basic flow
  3. Expressions ({{ }}) để access và transform data
  4. Test often - Execute từng node để debug
  5. Name clearly - Giúp maintain workflow dễ hơn

🚀 Bài tiếp theo

Google Workspace Integration - Kết nối n8n với Google Sheets, Gmail, Drive!