{
  "name": "Lead Scoring Agent — Agentcask",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "hubspot-lead-webhook",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "a1b2c3d4-0001-4aaa-8888-111111111111",
      "name": "HubSpot Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [240, 300],
      "webhookId": "lead-scoring-agentcask-v1"
    },
    {
      "parameters": {
        "jsCode": "const contact = $input.first().json.properties;\nconst email = contact.email;\nconst company = contact.company;\nconst title = contact.jobtitle;\nconst revenue = parseInt(contact.annualrevenue) || 0;\nconst employees = parseInt(contact.numberofemployees) || 0;\n\nreturn [{ json: { email, company, title, revenue, employees, rawContact: contact } }];"
      },
      "id": "a1b2c3d4-0002-4aaa-8888-222222222222",
      "name": "Extract Contact Fields",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [460, 300]
    },
    {
      "parameters": {
        "url": "=https://person.clearbit.com/v2/combined/find?email={{ $json.email }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "response": { "response": { "neverError": true } }
        }
      },
      "id": "a1b2c3d4-0003-4aaa-8888-333333333333",
      "name": "Clearbit Enrich",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [680, 300],
      "credentials": {
        "httpHeaderAuth": { "id": "clearbit-api-key", "name": "Clearbit API Key" }
      }
    },
    {
      "parameters": {
        "jsCode": "const contact = $('Extract Contact Fields').first().json;\nconst clearbit = $input.first().json;\n\nconst person = clearbit?.person || {};\nconst company = clearbit?.company || {};\n\nconst icp = {\n  email: contact.email,\n  name: person.name?.fullName || '',\n  title: person.employment?.title || contact.title || '',\n  company: company.name || contact.company || '',\n  industry: company.category?.industry || '',\n  employees: company.metrics?.employees || contact.employees || 0,\n  revenue: company.metrics?.annualRevenue || contact.revenue || 0,\n  seniority: person.employment?.seniority || '',\n  role: person.employment?.role || '',\n  country: company.geo?.country || '',\n  raised: company.crunchbase?.handle ? true : false,\n  technologies: company.tech || []\n};\n\nreturn [{ json: icp }];"
      },
      "id": "a1b2c3d4-0004-4aaa-8888-444444444444",
      "name": "Build ICP Profile",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [900, 300]
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "message",
        "model": "claude-3-5-sonnet-20241022",
        "messages": {
          "values": [
            {
              "content": "=You are a B2B lead scoring expert. Score this lead from 0-100 for ICP fit and return JSON only.\n\nLead:\n- Name: {{ $json.name }}\n- Title: {{ $json.title }}\n- Company: {{ $json.company }}\n- Industry: {{ $json.industry }}\n- Employees: {{ $json.employees }}\n- Annual Revenue: ${{ $json.revenue }}\n- Seniority: {{ $json.seniority }}\n- Role: {{ $json.role }}\n- Country: {{ $json.country }}\n- Has raised funding: {{ $json.raised }}\n\nOur ICP: B2B SaaS companies, 10-500 employees, decision-maker titles (VP, Director, Head of, C-level), English-speaking markets.\n\nReturn only valid JSON:\n{\n  \"score\": <0-100>,\n  \"tier\": <\"A\"|\"B\"|\"C\"|\"D\">,\n  \"reason\": \"<one sentence>\",\n  \"signals\": [\"<signal1>\", \"<signal2>\", \"<signal3>\"]\n}"
            }
          ]
        },
        "options": {
          "temperature": 0.1,
          "maxTokens": 300
        }
      },
      "id": "a1b2c3d4-0005-4aaa-8888-555555555555",
      "name": "Claude Score Lead",
      "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
      "typeVersion": 1,
      "position": [1120, 300],
      "credentials": {
        "anthropicApi": { "id": "anthropic-api-key", "name": "Anthropic API Key" }
      }
    },
    {
      "parameters": {
        "jsCode": "const icp = $('Build ICP Profile').first().json;\nconst rawScore = $input.first().json.content[0].text;\n\nlet parsed;\ntry {\n  parsed = JSON.parse(rawScore);\n} catch(e) {\n  const match = rawScore.match(/\\{[\\s\\S]*\\}/);\n  parsed = match ? JSON.parse(match[0]) : { score: 0, tier: 'D', reason: 'Parse error', signals: [] };\n}\n\nreturn [{\n  json: {\n    ...icp,\n    score: parsed.score,\n    tier: parsed.tier,\n    reason: parsed.reason,\n    signals: parsed.signals,\n    scoredAt: new Date().toISOString()\n  }\n}];"
      },
      "id": "a1b2c3d4-0006-4aaa-8888-666666666666",
      "name": "Parse Score",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1340, 300]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": false },
          "conditions": [
            {
              "leftValue": "={{ $json.tier }}",
              "rightValue": "D",
              "operator": { "type": "string", "operation": "notEquals" }
            }
          ]
        }
      },
      "id": "a1b2c3d4-0007-4aaa-8888-777777777777",
      "name": "Filter D Leads",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [1560, 300]
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "resource": "contact",
        "operation": "update",
        "contactId": "={{ $json.email }}",
        "additionalFields": {
          "customPropertiesUi": {
            "customPropertiesValues": [
              { "property": "lead_score", "value": "={{ $json.score }}" },
              { "property": "lead_tier", "value": "={{ $json.tier }}" },
              { "property": "score_reason", "value": "={{ $json.reason }}" }
            ]
          }
        }
      },
      "id": "a1b2c3d4-0008-4aaa-8888-888888888888",
      "name": "Update HubSpot",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 2,
      "position": [1780, 200],
      "credentials": {
        "hubspotOAuth2Api": { "id": "hubspot-oauth", "name": "HubSpot OAuth" }
      }
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "resource": "message",
        "operation": "post",
        "channel": "sales-leads",
        "text": "=*New Lead Scored* — Tier {{ $json.tier }} ({{ $json.score }}/100)\n\n*{{ $json.name }}* · {{ $json.title }} @ {{ $json.company }}\n📧 {{ $json.email }}\n🏢 {{ $json.employees }} employees · {{ $json.industry }}\n\n💡 _{{ $json.reason }}_\n\n*Signals:*\n{{ $json.signals.map(s => `• ${s}`).join('\\n') }}\n\n<https://app.hubspot.com/contacts/|Open in HubSpot →>"
      },
      "id": "a1b2c3d4-0009-4aaa-8888-999999999999",
      "name": "Slack Notify Rep",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2,
      "position": [1780, 380],
      "credentials": {
        "slackOAuth2Api": { "id": "slack-oauth", "name": "Slack OAuth" }
      }
    }
  ],
  "connections": {
    "HubSpot Webhook": {
      "main": [[{ "node": "Extract Contact Fields", "type": "main", "index": 0 }]]
    },
    "Extract Contact Fields": {
      "main": [[{ "node": "Clearbit Enrich", "type": "main", "index": 0 }]]
    },
    "Clearbit Enrich": {
      "main": [[{ "node": "Build ICP Profile", "type": "main", "index": 0 }]]
    },
    "Build ICP Profile": {
      "main": [[{ "node": "Claude Score Lead", "type": "main", "index": 0 }]]
    },
    "Claude Score Lead": {
      "main": [[{ "node": "Parse Score", "type": "main", "index": 0 }]]
    },
    "Parse Score": {
      "main": [[{ "node": "Filter D Leads", "type": "main", "index": 0 }]]
    },
    "Filter D Leads": {
      "main": [
        [{ "node": "Update HubSpot", "type": "main", "index": 0 }],
        []
      ]
    },
    "Update HubSpot": {
      "main": [[{ "node": "Slack Notify Rep", "type": "main", "index": 0 }]]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true,
    "callerPolicy": "workflowsFromSameOwner",
    "errorWorkflow": ""
  },
  "versionId": "agentcask-lead-scoring-v1.2.0",
  "id": "agentcask-lead-scoring-001",
  "meta": {
    "templateCredsSetupCompleted": false,
    "instanceId": "agentcask"
  },
  "tags": [{ "name": "Sales" }, { "name": "Agentcask" }, { "name": "Lead Scoring" }]
}
