Mục lục

Tạo Workflow đầu tiên

Hướng dẫn từng bước tạo một workflow cơ bản: Lấy dữ liệu API và gửi Email.

Hãy cùng tạo một workflow đơn giản: Mỗi ngày lấy thông tin thời tiết và gửi email báo cáo.

Bước 1: Tạo Trigger

  1. Bấm dấu + màu cam để thêm node.
  2. Tìm kiếm Schedule.
  3. Cấu hình: Chọn Interval, Wait 1 Days. (Để test, bạn có thể chọn Trigger On clicking 'execute').

Bước 2: Lấy dữ liệu (HTTP Request)

  1. Thêm node HTTP Request.
  2. Method: GET.
  3. URL: Sử dụng một API thời tiết miễn phí, ví dụ: https://api.open-meteo.com/v1/forecast?latitude=21.02&longitude=105.83&current_weather=true (Hà Nội).
  4. Bấm Execute Node để xem dữ liệu trả về. Bạn sẽ thấy JSON chứa nhiệt độ, tốc độ gió...

Bước 3: Xử lý dữ liệu (Optional)

Nếu bạn muốn định dạng lại dữ liệu:

  1. Thêm node Set.
  2. Tạo field mới message.
  3. Value: Sử dụng Expression.
    • Nhập: Hôm nay nhiệt độ là:
    • Mở Expression Editor, chọn biến temperature từ node HTTP Request.
    • Kết quả: Hôm nay nhiệt độ là: 25 độ C.

Bước 4: Gửi Email (Gmail Node)

Lưu ý: Cần cấu hình Credential cho Gmail (OAuth2 hoặc App Password) trước.

  1. Thêm node Gmail.
  2. Operation: Send.
  3. To Email: Nhập email của bạn.
  4. Subject: "Báo cáo thời tiết hàng ngày".
  5. Message: Dùng Expression lấy giá trị từ node Set (hoặc trực tiếp từ HTTP Request).

Bước 5: Kích hoạt (Activate)

  • Bấm nút Active ở góc trên bên phải màn hình để workflow chạy tự động theo lịch (Schedule) bạn đã cài đặt.
  • Nhớ lưu (Save) workflow lại.

Code Workflow mẫu (JSON)

Bạn có thể copy đoạn JSON dưới đây và dán trực tiếp vào giao diện n8n (Ctrl+V) để import nhanh workflow này:

json:
{
  "name": "Daily Weather Report",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "seconds": 0
            }
          ]
        }
      },
      "name": "Schedule",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://api.open-meteo.com/v1/forecast?latitude=21.02&longitude=105.83&current_weather=true",
        "options": {}
      },
      "name": "Get Weather",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "message",
              "value": "={{ 'Hôm nay nhiệt độ là: ' + $json.current_weather.temperature + ' độ C' }}"
            },
            {
              "name": "subject",
              "value": "Báo cáo thời tiết hôm nay"
            }
          ]
        },
        "options": {}
      },
      "name": "Format Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "subject": "={{ $json.subject }}",
        "message": "={{ $json.message }}",
        "toEmail": "user@example.com"
      },
      "name": "Gmail",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [
        850,
        300
      ],
      "credentials": {
        "gmailOAuth2": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Gmail Account"
        }
      }
    }
  ],
  "connections": {
    "Schedule": {
      "main": [
        [
          {
            "node": "Get Weather",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Weather": {
      "main": [
        [
          {
            "node": "Format Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Data": {
      "main": [
        [
          {
            "node": "Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
Quảng cáo
mdhorizontal