{
  "name": "AI Lead Qualification - Main Workflow",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "new-lead",
        "responseMode": "responseNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        0
      ],
      "id": "aab830a3-ab6b-4f37-9e31-4acc1ae7b0dd",
      "name": "Receive New Lead"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "d7fb3bb4-418d-492e-a697-0e15c7392d59",
              "leftValue": "={{ $json.body.full_name }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            },
            {
              "id": "5e75df0a-0d7b-46f4-8df6-18345a2111d3",
              "leftValue": "={{ $json.body.email }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            },
            {
              "id": "95d19d59-5ad6-4255-bfb1-13dcc56f7519",
              "leftValue": "={{ $json.body.company }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            },
            {
              "id": "124c445c-6688-44e6-8168-d48c5ed16f45",
              "leftValue": "={{ $json.body.message }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        224,
        0
      ],
      "id": "a3e3ff5a-755b-4977-98ad-a5bcc8efaaa6",
      "name": "Validate Required Fields"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "1lLiQ9ymaA_9_PLhv0eZNkjxsAPZ1Ide5cW38MFlJUJ0",
          "mode": "list",
          "cachedResultName": "AI Lead CRM",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1lLiQ9ymaA_9_PLhv0eZNkjxsAPZ1Ide5cW38MFlJUJ0/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Leads",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1lLiQ9ymaA_9_PLhv0eZNkjxsAPZ1Ide5cW38MFlJUJ0/edit#gid=0"
        },
        "filtersUI": {
          "values": [
            {
              "lookupColumn": "Email",
              "lookupValue": "={{$json.body.email}}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        448,
        0
      ],
      "id": "f2c7742f-1622-4aba-ac3f-b89f997d4e9f",
      "name": "Check Existing Lead",
      "alwaysOutputData": true,
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "content": "## Duplicate Lead\n\nThis lead already exists. Stop processing to avoid duplicate records and unnecessary OpenAI API usage."
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        96,
        32
      ],
      "typeVersion": 1,
      "id": "51d76002-7159-4250-ac88-45d56ef9a560",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "14eff524-26cb-45c4-9d13-1675da14d3ae",
              "leftValue": "={{ $json.Email }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        528,
        0
      ],
      "id": "117a0259-40fe-4096-ba71-db514bf35cf8",
      "name": "Is Duplicate Lead?"
    },
    {
      "parameters": {
        "mode": "raw",
        "jsonOutput": "={{ {\n  full_name: $('Receive New Lead').item.json.body.full_name,\n  email: $('Receive New Lead').item.json.body.email,\n  company: $('Receive New Lead').item.json.body.company,\n  job_title: $('Receive New Lead').item.json.body.job_title ?? null,\n  phone: $('Receive New Lead').item.json.body.phone ?? null,\n  country: $('Receive New Lead').item.json.body.country ?? null,\n  message: $('Receive New Lead').item.json.body.message,\n  employees: $('Receive New Lead').item.json.body.employees ?? null,\n  budget: $('Receive New Lead').item.json.body.budget ?? null\n} }}",
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        736,
        96
      ],
      "id": "70e92ce3-5881-4a79-b069-d29a0608a73a",
      "name": "Prepare Lead for AI"
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gpt-4.1-mini",
          "mode": "list",
          "cachedResultName": "GPT-4.1-MINI"
        },
        "responses": {
          "values": [
            {
              "role": "system",
              "content": "You are a B2B sales lead qualification engine for an AI automation agency.\n\nAnalyze only the supplied lead data.\n\nDo not invent facts.\n\nUse null or an empty array when information cannot be responsibly inferred.\n\nReturn only structured output matching the required schema.\n\nQualification rules:\n\nHot:\nscore 80 to 100\n\nWarm:\nscore 60 to 79\n\nCold:\nscore 30 to 59\n\nUnqualified:\nscore 0 to 29"
            },
            {
              "content": "=Evaluate this lead using:\n- clarity of business need\n- buying intent\n- available budget\n- company size\n- likely decision-making authority\n- urgency\n- relevance to AI automation services\n- message quality\n- spam or irrelevant indicators\n\nLead data:\n{{ JSON.stringify($json) }}"
            }
          ]
        },
        "builtInTools": {},
        "options": {
          "textFormat": {
            "textOptions": {
              "type": "json_schema",
              "name": "lead_qualification",
              "schema": "{\n  \"type\": \"object\",\n  \"properties\": {\n    \"lead_score\": {\n      \"type\": \"integer\",\n      \"minimum\": 0,\n      \"maximum\": 100\n    },\n    \"qualification\": {\n      \"type\": \"string\",\n      \"enum\": [\n        \"Hot\",\n        \"Warm\",\n        \"Cold\",\n        \"Unqualified\"\n      ]\n    },\n    \"summary\": {\n      \"type\": \"string\"\n    },\n    \"recommended_action\": {\n      \"type\": \"string\"\n    },\n    \"industry\": {\n      \"type\": [\n        \"string\",\n        \"null\"\n      ]\n    },\n    \"pain_points\": {\n      \"type\": \"array\",\n      \"items\": {\n        \"type\": \"string\"\n      }\n    },\n    \"decision_maker_likelihood\": {\n      \"type\": \"string\",\n      \"enum\": [\n        \"High\",\n        \"Medium\",\n        \"Low\"\n      ]\n    },\n    \"urgency\": {\n      \"type\": \"string\",\n      \"enum\": [\n        \"High\",\n        \"Medium\",\n        \"Low\"\n      ]\n    },\n    \"budget_fit\": {\n      \"type\": \"string\",\n      \"enum\": [\n        \"High\",\n        \"Medium\",\n        \"Low\",\n        \"Unknown\"\n      ]\n    },\n    \"confidence\": {\n      \"type\": \"number\",\n      \"minimum\": 0,\n      \"maximum\": 1\n    },\n    \"reasoning_brief\": {\n      \"type\": \"string\"\n    }\n  },\n  \"additionalProperties\": false,\n  \"required\": [\n    \"lead_score\",\n    \"qualification\",\n    \"summary\",\n    \"recommended_action\",\n    \"industry\",\n    \"pain_points\",\n    \"decision_maker_likelihood\",\n    \"urgency\",\n    \"budget_fit\",\n    \"confidence\",\n    \"reasoning_brief\"\n  ]\n}",
              "strict": true
            }
          },
          "temperature": 0
        }
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 2.3,
      "position": [
        736,
        144
      ],
      "id": "59e06098-f06b-4624-b904-706ebdb6fcc6",
      "name": "Generate AI Qualification",
      "retryOnFail": true,
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1lLiQ9ymaA_9_PLhv0eZNkjxsAPZ1Ide5cW38MFlJUJ0",
          "mode": "list",
          "cachedResultName": "AI Lead CRM",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1lLiQ9ymaA_9_PLhv0eZNkjxsAPZ1Ide5cW38MFlJUJ0/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Leads",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1lLiQ9ymaA_9_PLhv0eZNkjxsAPZ1Ide5cW38MFlJUJ0/edit#gid=0"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Lead ID": "={{ $execution.id + '-' + $itemIndex }}",
            "Created At": "={{ $now.toISO() }}",
            "Full Name": "={{ $('Receive New Lead').item.json.body.full_name }}",
            "Email": "={{ $('Receive New Lead').item.json.body.email }}",
            "Company": "={{ $('Receive New Lead').item.json.body.company }}",
            "Job Title": "={{ $('Receive New Lead').item.json.body.job_title ?? null }}",
            "Phone": "={{ $('Receive New Lead').item.json.body.phone ?? null }}",
            "Country": "={{ $('Receive New Lead').item.json.body.country ?? null }}",
            "Budget": "={{ $('Receive New Lead').item.json.body.budget ?? null }}",
            "Employees": "={{ $('Receive New Lead').item.json.body.employees ?? null }}",
            "Message": "={{ $('Receive New Lead').item.json.body.message }}",
            "Lead Score": "={{ $('Generate AI Qualification').item.json.output[0].content[0].text.lead_score }}",
            "Lead Status": "={{ $('Generate AI Qualification').item.json.output[0].content[0].text.qualification }}",
            "Summary": "={{ $('Generate AI Qualification').item.json.output[0].content[0].text.summary }}",
            "Follow-up Date": "={{ '' }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "Lead ID",
              "displayName": "Lead ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Created At",
              "displayName": "Created At",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Full Name",
              "displayName": "Full Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Email",
              "displayName": "Email",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Company",
              "displayName": "Company",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Job Title",
              "displayName": "Job Title",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Phone",
              "displayName": "Phone",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Country",
              "displayName": "Country",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Budget",
              "displayName": "Budget",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Employees",
              "displayName": "Employees",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Message",
              "displayName": "Message",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Lead Score",
              "displayName": "Lead Score",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Lead Status",
              "displayName": "Lead Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Summary",
              "displayName": "Summary",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Follow-up Date",
              "displayName": "Follow-up Date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        912,
        144
      ],
      "id": "6131ceaa-7e5c-4888-80ee-fd228d5fee3d",
      "name": "Append row in sheet",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { success: true, qualification: $('Generate AI Qualification').item.json.output[0].content[0].text.qualification, lead_score: $('Generate AI Qualification').item.json.output[0].content[0].text.lead_score, summary: $('Generate AI Qualification').item.json.output[0].content[0].text.summary } }}",
        "options": {
          "responseCode": 200
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        912,
        192
      ],
      "id": "281ad959-2816-4fcc-9fbc-837946fb50a5",
      "name": "Return Qualification Success"
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\n  \"success\": false,\n  \"reason\": \"Duplicate lead\"\n}",
        "options": {
          "responseCode": 409
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        736,
        -96
      ],
      "id": "ab790fca-c12e-4411-94ba-034e6cdcf485",
      "name": "Return Duplicate Lead"
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\n  \"success\": false,\n  \"reason\": \"Missing required fields\",\n  \"required_fields\": [\n    \"full_name\",\n    \"email\",\n    \"company\",\n    \"message\"\n  ]\n}",
        "options": {
          "responseCode": 400
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        448,
        -192
      ],
      "id": "64cfb06a-e39c-4bd7-9c71-03ec8bf74cc2",
      "name": "Return Validation Error"
    }
  ],
  "connections": {
    "Receive New Lead": {
      "main": [
        [
          {
            "node": "Validate Required Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate Required Fields": {
      "main": [
        [
          {
            "node": "Check Existing Lead",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Return Validation Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Existing Lead": {
      "main": [
        [
          {
            "node": "Is Duplicate Lead?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Duplicate Lead?": {
      "main": [
        [
          {
            "node": "Return Duplicate Lead",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Prepare Lead for AI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Lead for AI": {
      "main": [
        [
          {
            "node": "Generate AI Qualification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate AI Qualification": {
      "main": [
        [
          {
            "node": "Append row in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append row in sheet": {
      "main": [
        [
          {
            "node": "Return Qualification Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "e23dee20-2df4-4c95-bba9-6e717fc4a66c",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodeGroups": [],
  "id": "RnE85pv4myhCpALP",
  "tags": []
}