{
  "id": "RklSWzaSOgYJ7PJI",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Smart Inbox Organizer",
  "tags": [],
  "nodes": [
    {
      "id": "b6e5ab88-a3d6-4e2d-bb9a-ab5c1990cda6",
      "name": "Apply Work Label",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2848,
        368
      ],
      "parameters": {
        "labelIds": [
          "Label_2"
        ],
        "messageId": "={{ $('Watch Incoming Emails').item.json.id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "a98a3c0c-bfe7-4fc3-963f-f34c539d573c",
      "name": "Apply Personal Label",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2848,
        560
      ],
      "parameters": {
        "labelIds": [
          "Label_854+1234567890"
        ],
        "messageId": "={{ $('Watch Incoming Emails').item.json.id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "1b249755-237d-43e6-a41a-e3043679dea6",
      "name": "Apply Finance Label",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2848,
        752
      ],
      "parameters": {
        "labelIds": [
          "Label_7404936837042728690"
        ],
        "messageId": "={{ $('Watch Incoming Emails').item.json.id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "fcaa7a3f-66fc-4cf5-ab04-c0ce784d468c",
      "name": "Apply Newsletter Label",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2848,
        944
      ],
      "parameters": {
        "labelIds": [
          "Label_8585496128596791980"
        ],
        "messageId": "={{ $('Watch Incoming Emails').item.json.id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "b121cc57-9b6d-4c97-9b8f-3d2c98f8e50c",
      "name": "Groq Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "position": [
        1904,
        656
      ],
      "parameters": {
        "model": "openai/gpt-oss-20b",
        "options": {}
      },
      "credentials": {
        "groqApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "4b577875-59e2-4b20-a6b7-d915472b9c25",
      "name": "Extract & Clean Category",
      "type": "n8n-nodes-base.set",
      "position": [
        2400,
        560
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "category",
              "value": "={{$json.output?.trim() || 'Personal'}}"
            },
            {
              "name": "id",
              "value": "={{$json.emailId}}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 2
    },
    {
      "id": "b4eafe4b-439b-41ea-ae15-84fb1cbf279c",
      "name": "Normalize Email Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        1600,
        544
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "email_from",
              "value": "={{$json.from}}"
            },
            {
              "name": "email_subject",
              "value": "={{$json.subject}}"
            },
            {
              "name": "email_body",
              "value": "={{$json.text}}"
            },
            {
              "name": "received_at",
              "value": "={{$now}}"
            },
            {
              "name": "=emailId",
              "value": "={{ $json.id }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 2
    },
    {
      "id": "2782f879-670b-4256-9775-519c7fa1c58d",
      "name": "AI Agent - Classify Email Category",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1824,
        432
      ],
      "parameters": {
        "text": "=You are an email classification assistant.\n\nClassify the following email into exactly one of these categories:\n- Work\n- Personal\n- Financial\n- Newsletter\n\nDefinitions:\n- Work: Job-related communication, colleagues, meetings, projects or professional matters.\n- Personal: Messages from friends/family, casual conversations or non-professional topics.\n- Financial: Banking, invoices, bills, payments, transactions or financial services.\n- Newsletter: Subscriptions, promotions, marketing emails or mass-distributed updates.\n\nEmail:\nSubject: {{$json.subject}}\nContent: {{$json.text}}\n\nRules:\n- Choose only one category.\n- Base your decision on both subject and content.\n- If uncertain, select the most likely category.\n\nOutput:\nReturn only the category name (no explanation, no extra text).",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 3.1
    },
    {
      "id": "097746c8-27dc-499d-9ccc-330225f8553c",
      "name": "Merge Email Data and Category",
      "type": "n8n-nodes-base.merge",
      "position": [
        2176,
        560
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "combineBy": "combineByPosition"
      },
      "typeVersion": 3.2
    },
    {
      "id": "a4058efa-8bdf-4009-a772-6481051eb67a",
      "name": "Create Review Mail Task",
      "type": "n8n-nodes-base.googleTasks",
      "position": [
        3072,
        752
      ],
      "parameters": {
        "task": "QkhFNWRXS0hXcG5DempQTQ",
        "title": "={{'Review finance email: ' + $('Watch Incoming Emails').item.json.headers.subject}}",
        "additionalFields": {
          "notes": "={{'Sender: ' + $('Watch Incoming Emails').item.json.from.value[0].address + '\\nSubject: ' + $('Watch Incoming Emails').item.json.subject + '\\nReceived: ' + $('Watch Incoming Emails').item.json.date + '\\n\\nBody:\\n' + $('Watch Incoming Emails').item.json.text}}"
        }
      },
      "credentials": {
        "googleTasksOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "8a9f8b5b-9f7b-4f63-a08f-b031cc469ecc",
      "name": "Route Based on Category",
      "type": "n8n-nodes-base.switch",
      "position": [
        2624,
        624
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "00049f22-00df-40e2-b809-4bb64a29bd0b",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{$json.category}}",
                    "rightValue": "Work"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "78099d33-6cbb-4401-adfd-f7a4e1a794e4",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{$json.category}}",
                    "rightValue": "Personal"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "a6881692-21a2-49dd-8396-2f626636908a",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{$json.category}}",
                    "rightValue": "Financial"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "d93fffd9-1357-4da3-9197-f47718fef8a1",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{$json.category}}",
                    "rightValue": "Newsletter"
                  }
                ]
              }
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "e9390320-0411-46b3-b3e5-a8481cbc5e31",
      "name": "Log data to Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        2624,
        432
      ],
      "parameters": {
        "columns": {
          "value": {
            "Sender": "={{ $('Watch Incoming Emails').item.json.from.text }}",
            "Subject": "={{ $('Watch Incoming Emails').item.json.subject || \"No Subject\"}}",
            "Category": "={{ $json.category }}",
            "Timestamp": "={{ $('Watch Incoming Emails').item.json.date }}"
          },
          "schema": [
            {
              "id": "Sender",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Sender",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Subject",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Subject",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Category",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Category",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Timestamp",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Timestamp",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "MessageId",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "MessageId",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Status",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "Status",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 2113104548,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1KA65Fzm1uP4kP8UiCN7rlQxUmcCv5ESxhivfxfDui2s/edit#gid=2113104548",
          "cachedResultName": "Inbox"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1KA65Fzm1uP4kP8UiCN7rlQxUmcCv5ESxhivfxfDui2s",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1KA65Fzm1uP4kP8UiCN7rlQxUmcCv5ESxhivfxfDui2s/edit?usp=drivesdk",
          "cachedResultName": "N8N Nirmit"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4
    },
    {
      "id": "532ca107-b7f3-43ab-b880-c90e76f6138d",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        800,
        -144
      ],
      "parameters": {
        "width": 416,
        "height": 416,
        "content": "## How it works:\nThis workflow automatically organizes incoming Gmail emails using AI. Each email is classified into Work, Personal, Finance or Newsletter. Based on the category, the workflow applies the correct Gmail label, logs the email details into Google Sheets and creates a task if it's finance-related.\n\n## Setup steps:\n1. Connect Gmail, Google Sheets and Google Tasks accounts\n2. Create Gmail labels: Work, Personal, Finance, Newsletter\n3. Update label names in Gmail nodes if they differ\n4. Configure your Google Sheet (columns: Sender, Subject, Category, Timestamp)\n5. Test the AI classification output format\n6. Activate the workflow"
      },
      "typeVersion": 1
    },
    {
      "id": "27649e5b-784e-4c16-a6f4-201dcac3e635",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1344,
        256
      ],
      "parameters": {
        "color": 7,
        "width": 768,
        "height": 864,
        "content": "## Email Intake & AI Classification\nCaptures every new Gmail message and standardizes key fields like sender and subject using a Set node. The AI Agent then analyzes the email content and classifies it into one of four categories: Work, Personal, Finance or Newsletter."
      },
      "typeVersion": 1
    },
    {
      "id": "92b0201f-11da-4328-ac3a-ed780dfd87d0",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2128,
        256
      ],
      "parameters": {
        "color": 7,
        "width": 432,
        "height": 864,
        "content": "## Data Merging & Category Cleaning\nCombines the original email data with the AI-generated category. The Set node cleans and standardizes the category value to ensure consistent routing, logging and label application across the workflow."
      },
      "typeVersion": 1
    },
    {
      "id": "26c85959-035c-43cd-b427-3f10708618af",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2576,
        256
      ],
      "parameters": {
        "color": 7,
        "width": 688,
        "height": 864,
        "content": "## Routing, Labeling & Actions\nRoutes emails based on their category and performs the corresponding actions. Applies Gmail labels, logs email details into Google Sheets and creates a Google Task reminder for finance-related emails."
      },
      "typeVersion": 1
    },
    {
      "id": "7abf61e1-84e1-42d5-a5ca-75bda2d5eebf",
      "name": "Watch Incoming Emails",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        1376,
        544
      ],
      "parameters": {
        "simple": false,
        "filters": {},
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "versionId": "d7ee5e46-ed44-45b9-b3ba-1bc73df70fb5",
  "connections": {
    "Groq Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent - Classify Email Category",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Apply Finance Label": {
      "main": [
        [
          {
            "node": "Create Review Mail Task",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Watch Incoming Emails": {
      "main": [
        [
          {
            "node": "Normalize Email Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize Email Fields": {
      "main": [
        [
          {
            "node": "AI Agent - Classify Email Category",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge Email Data and Category",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route Based on Category": {
      "main": [
        [
          {
            "node": "Apply Work Label",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Apply Personal Label",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Apply Finance Label",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Apply Newsletter Label",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract & Clean Category": {
      "main": [
        [
          {
            "node": "Log data to Google Sheets",
            "type": "main",
            "index": 0
          },
          {
            "node": "Route Based on Category",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Email Data and Category": {
      "main": [
        [
          {
            "node": "Extract & Clean Category",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent - Classify Email Category": {
      "main": [
        [
          {
            "node": "Merge Email Data and Category",
            "type": "main",
            "index": 1
          }
        ]
      ]
    }
  }
}