Skip to content

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:

  1. Kích hoạt thủ công.
  2. Lấy danh sách công việc (To-do list) từ API JSONPlaceholder.
  3. Lọc lấy các công việc đã hoàn thành (completed: true).
  4. Giữ lại chỉ 2 trường: titleid.

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

  1. Bấm nút New (hoặc Add Workflow).
  2. 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)

  1. Thêm node HTTP Request.
  2. Nối Manual Trigger --> HTTP Request.
  3. Cấu hình:
    • Method: GET
    • URL: https://jsonplaceholder.typicode.com/todos
  4. 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)

  1. Thêm node Filter.
  2. Nối HTTP Request --> Filter.
  3. Cấu hình:
    • Condition:
      • Value 1: {{ $json.completed }} (Kéo thả từ input)
      • Operation: Is True
  4. Bấm Execute Node.

Bước 4: Chọn trường dữ liệu (Edit Fields)

  1. Thêm node Edit Fields (Set).
  2. Nối Filter --> Edit Fields.
  3. Cấu hình:
    • Mode: "Keep Only Set"
    • Add Field: Task Name = {{ $json.title }}
    • Add Field: ID = {{ $json.id }}
  4. 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.

Module 2: Các khái niệm cốt lõi

Internal documentation for iNET Portal