Tạo Workflow đầu tiên
Mục tiêu bài học
Sau bài học này, bạn sẽ:
- Tự tay xây dựng một workflow hoàn chỉnh từ A-Z
- Hiểu cách Trigger hoạt động
- Biết cách dùng node HTTP Request để lấy dữ liệu từ API
- Biết cách dùng node Filter để lọc dữ liệu
- Hiểu cấu trúc JSON output của n8n
Kịch bản
Chúng ta sẽ xây dựng một workflow đơn giản:
- Kích hoạt thủ công.
- Lấy danh sách công việc (To-do list) từ API JSONPlaceholder.
- Lọc lấy các công việc đã hoàn thành (
completed: true). - Giữ lại chỉ 2 trường:
titlevàid.
Cách hoạt động (Pipeline)
[ Manual Trigger ] ──▶ [ HTTP Request ] ──▶ [ Filter ] ──▶ [ Edit Fields ]
(Start) (Get Data) (Logic) (Transform)Các bước thực hiện
Bước 1: Tạo Trigger
- Bấm nút New (hoặc Add Workflow).
- Mặc định n8n sẽ tạo sẵn node Manual Trigger. Giữ nguyên node này.
Bước 2: Gọi API (HTTP Request)
- Thêm node HTTP Request.
- Nối Manual Trigger --> HTTP Request.
- Cấu hình:
- Method:
GET - URL:
https://jsonplaceholder.typicode.com/todos
- Method:
- Bấm Execute Node.
Kết quả (Output):
json
[
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
},
...
]Bước 3: Lọc dữ liệu (Filter)
- Thêm node Filter.
- Nối HTTP Request --> Filter.
- Cấu hình:
- Condition:
- Value 1:
{{ $json.completed }}(Kéo thả từ input) - Operation: Is True
- Value 1:
- Condition:
- Bấm Execute Node.
Bước 4: Chọn trường dữ liệu (Edit Fields)
- Thêm node Edit Fields (Set).
- Nối Filter --> Edit Fields.
- Cấu hình:
- Mode: "Keep Only Set"
- Add Field:
Task Name={{ $json.title }} - Add Field:
ID={{ $json.id }}
- Bấm Execute Node.
Ví dụ đầy đủ (JSON Workflow)
Bạn có thể copy đoạn JSON dưới đây và paste trực tiếp vào n8n canvas (Ctrl+V) để có ngay workflow này.
json
{
"nodes": [
{
"parameters": {},
"name": "Start",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [250, 300]
},
{
"parameters": {
"url": "https://jsonplaceholder.typicode.com/todos",
"options": {}
},
"name": "HTTP Request",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.1,
"position": [450, 300]
},
{
"parameters": {
"conditions": {
"boolean": [
{
"value1": "={{ $json.completed }}",
"value2": true
}
]
}
},
"name": "Filter",
"type": "n8n-nodes-base.filter",
"typeVersion": 1,
"position": [650, 300]
},
{
"parameters": {
"mode": "keepOnlySet",
"values": {
"string": [
{
"name": "Task Name",
"value": "={{ $json.title }}"
},
{
"name": "ID",
"value": "={{ $json.id }}"
}
]
}
},
"name": "Edit Fields",
"type": "n8n-nodes-base.set",
"typeVersion": 2,
"position": [850, 300]
}
],
"connections": {
"Start": {
"main": [[{ "node": "HTTP Request", "type": "main", "index": 0 }]]
},
"HTTP Request": {
"main": [[{ "node": "Filter", "type": "main", "index": 0 }]]
},
"Filter": {
"main": [[{ "node": "Edit Fields", "type": "main", "index": 0 }]]
}
}
}Tổng kết
Bạn đã hoàn thành workflow đầu tiên! Bạn đã học được cách:
- Gọi API lấy dữ liệu
- Lọc dữ liệu theo điều kiện
- Chỉnh sửa và đổi tên trường dữ liệu
Bài học tiếp theo
Chúng ta sẽ đi sâu vào các khái niệm cốt lõi để hiểu rõ hơn về Node, Connection và JSON.