{
  "name": "Auto Gmail Labelling with AI",
  "nodes": [
    {
      "parameters": {
        "rule": { "interval": [{ "field": "minutes", "minutesInterval": 5 }] }
      },
      "id": "c1d2e3f4-0001-0001-0001-000000000001",
      "name": "Every 5 Minutes",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [240, 300]
    },
    {
      "parameters": {
        "operation": "getAll",
        "filters": { "readStatus": "unread", "labelIds": ["INBOX"] },
        "limit": 20
      },
      "id": "c1d2e3f4-0002-0002-0002-000000000002",
      "name": "Get Unread Emails",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 1,
      "position": [460, 300],
      "credentials": { "gmailOAuth2": { "id": "YOUR_GMAIL_CREDENTIAL_ID", "name": "Gmail OAuth2" } }
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "create",
        "model": "gpt-4o",
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "Classify this email into exactly one label. Available labels: INTERESTED_LEAD, NOT_INTERESTED, NEWSLETTER, PRICE_QUESTION, MEETING_REQUEST, FOLLOW_UP, SPAM, ADMIN, OTHER. Reply with only the label name, nothing else."
            },
            {
              "role": "user",
              "content": "=From: {{ $json.from }}\nSubject: {{ $json.subject }}\n\n{{ $json.text ? $json.text.slice(0, 500) : $json.snippet }}"
            }
          ]
        }
      },
      "id": "c1d2e3f4-0003-0003-0003-000000000003",
      "name": "Classify Email",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1,
      "position": [680, 300],
      "credentials": { "openAiApi": { "id": "YOUR_OPENAI_CREDENTIAL_ID", "name": "OpenAI API" } }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "1", "name": "label", "value": "={{ $json.message.content.trim() }}", "type": "string" },
            { "id": "2", "name": "emailId", "value": "={{ $('Get Unread Emails').item.json.id }}", "type": "string" }
          ]
        }
      },
      "id": "c1d2e3f4-0004-0004-0004-000000000004",
      "name": "Set Label",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3,
      "position": [900, 300]
    },
    {
      "parameters": {
        "operation": "addLabel",
        "messageId": "={{ $json.emailId }}",
        "labelIds": ["={{ $json.label }}"]
      },
      "id": "c1d2e3f4-0005-0005-0005-000000000005",
      "name": "Apply Label to Gmail",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 1,
      "position": [1120, 300],
      "credentials": { "gmailOAuth2": { "id": "YOUR_GMAIL_CREDENTIAL_ID", "name": "Gmail OAuth2" } }
    }
  ],
  "connections": {
    "Every 5 Minutes": { "main": [[{ "node": "Get Unread Emails", "type": "main", "index": 0 }]] },
    "Get Unread Emails": { "main": [[{ "node": "Classify Email", "type": "main", "index": 0 }]] },
    "Classify Email": { "main": [[{ "node": "Set Label", "type": "main", "index": 0 }]] },
    "Set Label": { "main": [[{ "node": "Apply Label to Gmail", "type": "main", "index": 0 }]] }
  },
  "active": false,
  "settings": { "executionOrder": "v1" },
  "versionId": "c1d2e3f4-aaaa-bbbb-cccc-000000000003",
  "meta": { "templateCredsSetupCompleted": false },
  "id": "workflow-gmail-auto-labelling",
  "tags": [{ "name": "Gmail" }, { "name": "AI" }, { "name": "Productivity" }]
}
