{
  "id": "4Fm1aToIhbW9opSI",
  "name": "Auto-Label Gmail Messages with GPT-5",
  "tags": [],
  "nodes": [
    {
      "id": "18eb3799-22c4-49c1-9672-51e3f939d185",
      "name": "Sticky Note - Main",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "parameters": {
        "width": 400,
        "height": 680,
        "content": "## Auto-Label Gmail Messages with GPT-5\nEvery new email gets read by AI and automatically labeled - sales, support, spam, personal, or urgent - so your inbox sorts itself.\n\n## How it works\n1. New email triggers the workflow\n2. AI reads the sender, subject, and body and picks one category\n3. The category is mapped to your real Gmail label ID\n4. The label gets applied to the message\n\n## Setup\n- [ ] Connect Gmail OAuth2 credentials (used in both Gmail nodes)\n- [ ] Connect OpenAI credentials in OpenAI GPT-5 Mini Model\n- [ ] Open Set Label Taxonomy and replace each placeholder with your real Gmail label IDs (Gmail Settings > Labels, or via the Gmail API - label names won't work here, only IDs)\n- [ ] Edit the category list in Set Label Taxonomy if you want different categories than Sales/Support/Spam/Personal/Urgent"
      },
      "typeVersion": 1
    },
    {
      "id": "f3d33d49-ed02-4f08-a3f9-04979306770a",
      "name": "Sticky Note - Classify and Apply",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1008,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 750,
        "height": 140,
        "content": "## Classify then Label\nAI picks a category, this maps it to a real label ID, then applies it."
      },
      "typeVersion": 1
    },
    {
      "id": "2d9af757-fd51-4683-8f55-b0b524bb6b60",
      "name": "Sticky Note - Warning",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1264,
        432
      ],
      "parameters": {
        "color": 3,
        "width": 260,
        "height": 132,
        "content": "**Label IDs, not label names.** If Apply Label in Gmail fails, check that the IDs in Set Label Taxonomy are real and still exist."
      },
      "typeVersion": 1
    },
    {
      "id": "c753b883-ec06-4692-9cf8-5195dc4d388f",
      "name": "When Email Received in Gmail",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        512,
        240
      ],
      "parameters": {
        "simple": false,
        "filters": {},
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "258d67ae-e0cc-4f41-8325-87ee91cac416",
      "name": "Set Label Taxonomy",
      "type": "n8n-nodes-base.set",
      "position": [
        752,
        240
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "asn-1",
              "name": "CategoryList",
              "type": "string",
              "value": "Sales, Support, Spam, Personal, Urgent"
            },
            {
              "id": "asn-2",
              "name": "Sales_LabelId",
              "type": "string",
              "value": "REPLACE_WITH_SALES_LABEL_ID"
            },
            {
              "id": "asn-3",
              "name": "Support_LabelId",
              "type": "string",
              "value": "REPLACE_WITH_SUPPORT_LABEL_ID"
            },
            {
              "id": "asn-4",
              "name": "Spam_LabelId",
              "type": "string",
              "value": "REPLACE_WITH_SPAM_LABEL_ID"
            },
            {
              "id": "asn-5",
              "name": "Personal_LabelId",
              "type": "string",
              "value": "REPLACE_WITH_PERSONAL_LABEL_ID"
            },
            {
              "id": "asn-6",
              "name": "Urgent_LabelId",
              "type": "string",
              "value": "REPLACE_WITH_URGENT_LABEL_ID"
            },
            {
              "id": "asn-7",
              "name": "Default_LabelId",
              "type": "string",
              "value": "REPLACE_WITH_DEFAULT_LABEL_ID"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "15c722d2-2115-4fc0-b8b7-ba968612504c",
      "name": "OpenAI GPT-5 Mini Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1008,
        432
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-5-mini"
        },
        "options": {},
        "builtInTools": {}
      },
      "typeVersion": 1.3
    },
    {
      "id": "1a222393-cec7-4594-8c89-557ced140506",
      "name": "AI Email Classifier",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "position": [
        1008,
        240
      ],
      "parameters": {
        "text": "=Classify this email into EXACTLY one of these categories: {{ $json.CategoryList }}\n\nFrom: {{ $('When Email Received in Gmail').item.json.from }}\nSubject: {{ $('When Email Received in Gmail').item.json.subject }}\nBody: {{ $('When Email Received in Gmail').item.json.snippet }}\n\nRespond with ONLY the category name, exactly as spelled in the list above. No punctuation, no explanation, nothing else.",
        "promptType": "define"
      },
      "typeVersion": 1.6,
      "continueOnFail": true
    },
    {
      "id": "08ae8108-bfc1-49b4-9f8c-eb8c288f5cd2",
      "name": "Map Category to Gmail Label",
      "type": "n8n-nodes-base.code",
      "position": [
        1296,
        240
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const taxonomy = $('Set Label Taxonomy').item.json;\nconst category = ($input.item.json.text || '').trim();\n\nconst map = {\n  'Sales': taxonomy.Sales_LabelId,\n  'Support': taxonomy.Support_LabelId,\n  'Spam': taxonomy.Spam_LabelId,\n  'Personal': taxonomy.Personal_LabelId,\n  'Urgent': taxonomy.Urgent_LabelId\n};\n\nconst labelId = map[category] || taxonomy.Default_LabelId;\n\nreturn {\n  json: {\n    category: category || 'Unclassified',\n    labelId\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "b74bf4dc-5da5-41ea-9962-7d1f138d5d4e",
      "name": "Apply Label in Gmail",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1504,
        240
      ],
      "parameters": {
        "labelIds": "={{ [$json.labelId] }}",
        "messageId": "={{ $('When Email Received in Gmail').item.json.id }}",
        "operation": "addLabels"
      },
      "typeVersion": 2.1,
      "continueOnFail": true
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "versionId": "cfd31970-68b2-48bd-abe6-930d2b48b265",
  "nodeGroups": [],
  "connections": {
    "Set Label Taxonomy": {
      "main": [
        [
          {
            "node": "AI Email Classifier",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Email Classifier": {
      "main": [
        [
          {
            "node": "Map Category to Gmail Label",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI GPT-5 Mini Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Email Classifier",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Map Category to Gmail Label": {
      "main": [
        [
          {
            "node": "Apply Label in Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When Email Received in Gmail": {
      "main": [
        [
          {
            "node": "Set Label Taxonomy",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}