{
  "name": "AI Urgent Email Detector (Lite)",
  "nodes": [
    {
      "parameters": {
        "content": "## AI Urgent Email Detector (Lite)\n\nWatches your inbox (IMAP) and asks Claude whether each new email is urgent. If it is, you get an instant notification with a one-line reason.\n\nA free, single-purpose starter. The full version adds 3-way triage, ready-to-send draft replies, configurable tone/context, and error handling.",
        "height": 260,
        "width": 380
      },
      "id": "b1a2c3d4-0001-4000-8000-000000000001",
      "name": "Overview",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -520,
        -240
      ]
    },
    {
      "parameters": {
        "content": "Setup (3 things):\n1. IMAP credential on the trigger (email + app password).\n2. SMTP credential on \"Send Urgent Alert\".\n3. Anthropic API key as a Header Auth credential (header name: x-api-key) on \"Is it Urgent?\".\n\nThen set your own address in the two email fields of \"Send Urgent Alert\".",
        "height": 260,
        "width": 300
      },
      "id": "b1a2c3d4-0002-4000-8000-000000000002",
      "name": "Setup Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -520,
        40
      ]
    },
    {
      "parameters": {
        "mailbox": "INBOX",
        "postProcessAction": "read",
        "options": {}
      },
      "id": "b1a2c3d4-0003-4000-8000-000000000003",
      "name": "Check Inbox (IMAP Trigger)",
      "type": "n8n-nodes-base.emailReadImap",
      "typeVersion": 2,
      "position": [
        -200,
        0
      ],
      "credentials": {
        "imap": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const item = $input.item.json;\nlet bodyText = item.text;\nif (!bodyText && item.html) {\n  bodyText = item.html.replace(/<[^>]*>/g, ' ').replace(/\\s+/g, ' ').trim();\n}\nif (!bodyText) bodyText = '(no body content)';\nreturn {\n  json: {\n    subject: item.subject || '(no subject)',\n    fromAddress: item.from || 'unknown sender',\n    bodyPreview: bodyText.substring(0, 2000)\n  }\n};"
      },
      "id": "b1a2c3d4-0004-4000-8000-000000000004",
      "name": "Normalize Email Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        60,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.anthropic.com/v1/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "anthropic-version",
              "value": "2023-06-01"
            },
            {
              "name": "content-type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({\n  model: \"claude-sonnet-4-6\",\n  max_tokens: 300,\n  system: \"You decide whether an email is urgent and needs the recipient's immediate attention. Respond with ONLY a valid JSON object, no markdown, no code fences: { \\\"is_urgent\\\": boolean, \\\"reason\\\": string with one short sentence }.\",\n  messages: [\n    {\n      role: \"user\",\n      content: \"From: \" + $json.fromAddress + \"\\nSubject: \" + $json.subject + \"\\nBody:\\n\" + $json.bodyPreview\n    }\n  ]\n}) }}",
        "options": {
          "timeout": 30000
        }
      },
      "id": "b1a2c3d4-0005-4000-8000-000000000005",
      "name": "Is it Urgent?",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        320,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const raw = $input.item.json;\nlet parsed;\ntry {\n  let text = raw.content && raw.content[0] && raw.content[0].text ? raw.content[0].text : '';\n  const match = text.trim().match(/\\{[\\s\\S]*\\}/);\n  if (!match) throw new Error('no json');\n  parsed = JSON.parse(match[0]);\n} catch (err) {\n  parsed = { is_urgent: false, reason: 'Could not parse AI response' };\n}\nconst original = $('Normalize Email Data').item.json;\nconst isOwnAlert = /^URGENT:/.test(original.subject || '');\nreturn {\n  json: {\n    ...original,\n    is_urgent: parsed.is_urgent === true && !isOwnAlert,\n    reason: parsed.reason || ''\n  }\n};"
      },
      "id": "b1a2c3d4-0006-4000-8000-000000000006",
      "name": "Parse Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        580,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "cond-urgent",
              "leftValue": "={{ $json.is_urgent }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "b1a2c3d4-0007-4000-8000-000000000007",
      "name": "Is Urgent?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        840,
        0
      ]
    },
    {
      "parameters": {
        "fromEmail": "you@example.com",
        "toEmail": "you@example.com",
        "subject": "=URGENT: {{ $json.subject }}",
        "emailFormat": "text",
        "text": "=An urgent email needs your attention.\n\nFrom: {{ $json.fromAddress }}\nSubject: {{ $json.subject }}\nWhy: {{ $json.reason }}\n\n---\nOriginal message:\n{{ $json.bodyPreview }}",
        "options": {}
      },
      "id": "b1a2c3d4-0008-4000-8000-000000000008",
      "name": "Send Urgent Alert",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        1120,
        -100
      ],
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {},
      "id": "b1a2c3d4-0009-4000-8000-000000000009",
      "name": "Not Urgent - Skip",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1120,
        100
      ]
    }
  ],
  "connections": {
    "Check Inbox (IMAP Trigger)": {
      "main": [
        [
          {
            "node": "Normalize Email Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize Email Data": {
      "main": [
        [
          {
            "node": "Is it Urgent?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is it Urgent?": {
      "main": [
        [
          {
            "node": "Parse Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Response": {
      "main": [
        [
          {
            "node": "Is Urgent?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Urgent?": {
      "main": [
        [
          {
            "node": "Send Urgent Alert",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Not Urgent - Skip",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "00000000-0000-4000-8000-000000000000",
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "id": "ai-urgent-email-detector-lite-001",
  "tags": []
}