{
  "name": "Auto-Label Gmail Emails with AI Classification using Groq Llama 3.3",
  "tags": [],
  "nodes": [
    {
      "id": "77be3f3a-a54c-4fc3-87ca-9ca3578d68cc",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -608,
        16
      ],
      "parameters": {
        "width": 480,
        "height": 720,
        "content": "## Auto-Label Gmail Emails with AI Classification using Groq Llama 3.3\n\n### How it works\n\nThis workflow watches Gmail for newly received emails and uses Groq's Llama 3.3 chat API to classify each message. It parses the AI response, routes the email through category checks, and applies the appropriate Gmail label automatically.\n\n### Setup steps\n\n- Connect Gmail credentials for both the trigger and Gmail label nodes.\n- Configure the Groq API credential or authorization header in the HTTP Request node.\n- Verify the AI prompt and response format expected by the Parse code node.\n- Ensure the Gmail labels Action Required, Promotions, and Other exist, or update the label nodes to use your desired labels.\n\n### Customization\n\nAdjust the classification prompt, parsing logic, and IF conditions to support additional categories or different labeling rules."
      },
      "typeVersion": 1
    },
    {
      "id": "5b2455b4-3608-4873-9288-328430afdd07",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -48,
        144
      ],
      "parameters": {
        "color": 7,
        "width": 672,
        "height": 320,
        "content": "## Receive and classify\n\nStarts when a new Gmail message arrives, sends the email content to Groq's Llama model for classification, and parses the AI response into usable category data."
      },
      "typeVersion": 1
    },
    {
      "id": "52f27603-36f6-4883-a551-874a6d06e505",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        672,
        128
      ],
      "parameters": {
        "color": 7,
        "width": 432,
        "height": 464,
        "content": "## Evaluate email category\n\nUses conditional checks to decide whether the parsed classification indicates an action-required email or should continue to the promotions-versus-other routing path."
      },
      "typeVersion": 1
    },
    {
      "id": "6ba680b2-14ad-4bab-ae0f-00930bc9117a",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1136,
        16
      ],
      "parameters": {
        "color": 7,
        "width": 432,
        "height": 672,
        "content": "## Apply Gmail labels\n\nApplies the final Gmail label based on the routing result: Action Required, Promotions, or Other."
      },
      "typeVersion": 1
    },
    {
      "id": "node-trigger-1",
      "name": "When Email Received",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        0,
        300
      ],
      "parameters": {
        "simple": false,
        "filters": {},
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "node-ai-2",
      "name": "Post to AI Classifier API",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        240,
        300
      ],
      "parameters": {
        "url": "https://api.groq.com/openai/v1/chat/completions",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"model\": \"llama-3.3-70b-versatile\",\n  \"temperature\": 0.3,\n  \"response_format\": { \"type\": \"json_object\" },\n  \"messages\": [\n    { \"role\": \"system\", \"content\": \"You are an email triage assistant. Read the incoming email body and reply ONLY with a JSON object with exactly this key:\\n- category: one of \\\"Action Required\\\" | \\\"Promotions\\\" | \\\"Other\\\"\\n\\\"Action Required\\\" = emails that need a reply or work (customer inquiries, requests, invoices, important notices).\\n\\\"Promotions\\\" = marketing emails, newsletters, campaign offers addressed to us as a customer.\\n\\\"Other\\\" = everything else (automated notifications, receipts, etc.).\\nOutput nothing but the JSON.\" },\n    { \"role\": \"user\", \"content\": {{ JSON.stringify(($json.text || $json.snippet || \"\").slice(0, 3000)) }} }\n  ]\n}",
        "sendBody": true,
        "sendHeaders": true,
        "specifyBody": "json",
        "authentication": "none",
        "specifyHeaders": "keypair",
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer YOUR_TOKEN_HERE"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "node-code-3",
      "name": "Parse Classification Response",
      "type": "n8n-nodes-base.code",
      "position": [
        480,
        300
      ],
      "parameters": {
        "jsCode": "const item = $input.first().json;\nconst content = item?.choices?.[0]?.message?.content ?? '{}';\nlet data;\ntry { data = JSON.parse(content); } catch (e) { data = { category: 'Other' }; }\nconst trig = $('When Email Received').first().json;\nreturn [{ json: { category: data.category ?? 'Other', messageId: trig.id } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "node-if-4",
      "name": "Check If Action Required",
      "type": "n8n-nodes-base.if",
      "position": [
        720,
        300
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "cond-4",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.category }}",
              "rightValue": "Action Required"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "node-if-5",
      "name": "Check If Promotions",
      "type": "n8n-nodes-base.if",
      "position": [
        960,
        420
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "cond-5",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.category }}",
              "rightValue": "Promotions"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "node-label-6",
      "name": "Add Label: Action Required",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1184,
        176
      ],
      "parameters": {
        "labelIds": [],
        "resource": "message",
        "messageId": "={{ $json.messageId }}",
        "operation": "addLabels"
      },
      "typeVersion": 2.1
    },
    {
      "id": "node-label-7",
      "name": "Add Label: Promotions",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1424,
        304
      ],
      "parameters": {
        "labelIds": [],
        "resource": "message",
        "messageId": "={{ $json.messageId }}",
        "operation": "addLabels"
      },
      "typeVersion": 2.1
    },
    {
      "id": "node-label-8",
      "name": "Add Label: Other",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1424,
        544
      ],
      "parameters": {
        "labelIds": [],
        "resource": "message",
        "messageId": "={{ $json.messageId }}",
        "operation": "addLabels"
      },
      "typeVersion": 2.1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "connections": {
    "Check If Promotions": {
      "main": [
        [
          {
            "node": "Add Label: Promotions",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Add Label: Other",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When Email Received": {
      "main": [
        [
          {
            "node": "Post to AI Classifier API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check If Action Required": {
      "main": [
        [
          {
            "node": "Add Label: Action Required",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Check If Promotions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Post to AI Classifier API": {
      "main": [
        [
          {
            "node": "Parse Classification Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Classification Response": {
      "main": [
        [
          {
            "node": "Check If Action Required",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}