{
  "name": "Lead Score Lite - Form to AI Score to Google Sheets",
  "nodes": [
    {
      "parameters": {
        "content": "### How it works\nThis workflow turns an n8n-hosted form into an AI lead-scoring pipeline. Every submission is validated, scored from 0 to 100 by an OpenAI-compatible model with a short written reason, then routed to one of two Google Sheets tabs. Leads scoring 70 or higher land in Hot Leads for immediate follow-up; everyone else goes to Nurture. Invalid emails stop with a clear error instead of creating broken rows.\n\n### Setup\n1. Create an HTTP Header Auth credential with header name Authorization and value Bearer <your API key>. Select it in Score Lead with AI. The default URL points at OpenAI; any OpenAI-compatible chat completions endpoint works.\n2. Create one spreadsheet with two tabs, Hot Leads and Nurture. Add these exact headers to row 1 of both tabs: name, email, company, message, source, domain, received_at, score, tier, score_reason, score_summary, scored_at.\n3. Select the Google Sheets credential in both append nodes and set the spreadsheet ID.\n4. Open the form, submit a test request, then check the tab that matches the score.\n\n### Customization\nChange the 70 score threshold in Score 70 or Higher?, edit the scoring prompt in Validate and Prepare Lead, or replace the Google Sheets nodes with an HTTP Request node to post results to your own CRM webhook. Tested with n8n 2.48.8 and core nodes only.",
        "height": 440,
        "width": 620,
        "color": 1
      },
      "id": "f1010000-0000-4000-8000-000000000001",
      "name": "Overview",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -620,
        -240
      ]
    },
    {
      "parameters": {
        "content": "The form trigger collects name, email, company and message. Invalid emails stop here with a clear error.",
        "height": 100,
        "width": 520,
        "color": 7
      },
      "id": "f1010000-0000-4000-8000-000000000002",
      "name": "## Capture and validate",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        -200
      ]
    },
    {
      "parameters": {
        "content": "Scores each lead 0-100 with a written reason. Point this node at any OpenAI-compatible endpoint.",
        "height": 100,
        "width": 520,
        "color": 7
      },
      "id": "f1010000-0000-4000-8000-000000000003",
      "name": "## Score with AI",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        520,
        -200
      ]
    },
    {
      "parameters": {
        "content": "70 or higher goes to Hot Leads. Everyone else goes to Nurture.",
        "height": 100,
        "width": 520,
        "color": 7
      },
      "id": "f1010000-0000-4000-8000-000000000004",
      "name": "## Route by score",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1040,
        -200
      ]
    },
    {
      "parameters": {
        "path": "lead-score-lite",
        "formTitle": "Get a callback",
        "formDescription": "Tell us about your project. Every request is scored automatically.",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Full name",
              "fieldName": "name",
              "fieldType": "text",
              "requiredField": true
            },
            {
              "fieldLabel": "Work email",
              "fieldName": "email",
              "fieldType": "email",
              "requiredField": true,
              "placeholder": "you@company.com"
            },
            {
              "fieldLabel": "Company",
              "fieldName": "company",
              "fieldType": "text",
              "requiredField": false
            },
            {
              "fieldLabel": "What do you need help with?",
              "fieldName": "message",
              "fieldType": "textarea",
              "requiredField": true
            }
          ]
        },
        "options": {
          "buttonLabel": "Score my request",
          "respondWithOptions": {
            "values": {
              "respondWith": "text",
              "formSubmittedText": "Thanks. Your request was received and will be scored."
            }
          }
        }
      },
      "id": "f1010000-0000-4000-8000-000000000010",
      "name": "Lead Intake Form",
      "type": "n8n-nodes-base.formTrigger",
      "typeVersion": 2.6,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const body = $json.body || $json;\nconst email = String(body.email || '').trim().toLowerCase();\nconst emailOk = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(email);\nif (!emailOk) throw new Error('Invalid or missing email address');\nconst domain = email.split('@')[1];\nconst name = String(body.name || '').trim();\nconst company = String(body.company || domain).trim();\nconst message = String(body.message || '').slice(0, 2000);\nreturn [{ json: {\n  name,\n  email,\n  company,\n  message,\n  source: String(body.source || 'n8n_form'),\n  domain,\n  received_at: new Date().toISOString(),\n  ai_user_prompt: [\n    'Score this inbound B2B lead from 0 to 100.',\n    'Name: ' + name,\n    'Email domain: ' + domain,\n    'Company: ' + company,\n    'Message: ' + message,\n    'Consider company fit, buying intent, urgency and information quality.',\n    'Return strict JSON only: {\"score\": <0-100 integer>, \"tier\": \"HOT\"|\"WARM\"|\"COLD\", \"reason\": \"<short reason>\", \"summary\": \"<one sentence>\"}.'\n  ].join('\\n'),\n} }];"
      },
      "id": "f1010000-0000-4000-8000-000000000011",
      "name": "Validate and Prepare Lead",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        260,
        0
      ]
    },
    {
      "parameters": {
        "url": "https://api.openai.com/v1/chat/completions",
        "method": "POST",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ model: 'gpt-4o-mini', temperature: 0.2, response_format: { type: 'json_object' }, messages: [ { role: 'system', content: 'You are a precise B2B lead scoring engine. Return strict JSON with keys: score (0-100 integer), tier (HOT|WARM|COLD), reason (short string), summary (one sentence).' }, { role: 'user', content: $json.ai_user_prompt } ] }) }}",
        "options": {
          "timeout": 60000
        }
      },
      "id": "f1010000-0000-4000-8000-000000000012",
      "name": "Score Lead with AI",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        520,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const raw = $json.choices?.[0]?.message?.content ?? '{}';\nlet parsed = {};\ntry { parsed = JSON.parse(raw); } catch (e) {\n  const m = String(raw).match(/\\{[\\s\\S]*\\}/);\n  if (m) { try { parsed = JSON.parse(m[0]); } catch (e2) {} }\n}\nconst numeric = Number(parsed.score);\nconst score = Number.isFinite(numeric) ? Math.max(0, Math.min(100, Math.round(numeric))) : 0;\nconst tier = score >= 70 ? 'HOT' : score >= 40 ? 'WARM' : 'COLD';\nconst { ai_user_prompt, ...lead } = $('Validate and Prepare Lead').item.json;\nreturn [{ json: {\n  ...lead,\n  score,\n  tier,\n  score_reason: parsed.reason || '',\n  score_summary: parsed.summary || '',\n  scored_at: new Date().toISOString(),\n} }];"
      },
      "id": "f1010000-0000-4000-8000-000000000013",
      "name": "Parse AI Score",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        780,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "c1",
              "leftValue": "={{ $json.score }}",
              "rightValue": 70,
              "operator": {
                "type": "number",
                "operation": "gte"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "f1010000-0000-4000-8000-000000000014",
      "name": "Score 70 or Higher?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1040,
        0
      ]
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "REPLACE_WITH_SPREADSHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Hot Leads",
          "mode": "name"
        },
        "operation": "append",
        "columns": {
          "mappingMode": "autoMapInputData",
          "matchingColumns": [],
          "schema": []
        },
        "options": {}
      },
      "id": "f1010000-0000-4000-8000-000000000015",
      "name": "Append to Hot Leads Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1280,
        -120
      ]
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "REPLACE_WITH_SPREADSHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Nurture",
          "mode": "name"
        },
        "operation": "append",
        "columns": {
          "mappingMode": "autoMapInputData",
          "matchingColumns": [],
          "schema": []
        },
        "options": {}
      },
      "id": "f1010000-0000-4000-8000-000000000016",
      "name": "Append to Nurture Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1280,
        120
      ]
    }
  ],
  "pinData": {},
  "connections": {
    "Lead Intake Form": {
      "main": [
        [
          {
            "node": "Validate and Prepare Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate and Prepare Lead": {
      "main": [
        [
          {
            "node": "Score Lead with AI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Score Lead with AI": {
      "main": [
        [
          {
            "node": "Parse AI Score",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse AI Score": {
      "main": [
        [
          {
            "node": "Score 70 or Higher?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Score 70 or Higher?": {
      "main": [
        [
          {
            "node": "Append to Hot Leads Sheet",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Append to Nurture Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "freebie-lead-score-lite",
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
