{
  "name": "AI lead qualifier",
  "nodes": [
    {
      "id": "00000000-0000-4000-8000-000000000001",
      "name": "Receive lead form",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        300
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "lead-qualifier",
        "authentication": "headerAuth",
        "responseMode": "onReceived",
        "options": {}
      },
      "webhookId": "lead-qualifier-webhook",
      "credentials": {
        "httpHeaderAuth": {
          "id": "FORM_HEADER_AUTH",
          "name": "Lead form shared secret"
        }
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000002",
      "name": "Normalize lead",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        280,
        300
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "name",
              "name": "name",
              "value": "={{ $json.body.name || $json.body.fullName || \"\" }}",
              "type": "string"
            },
            {
              "id": "email",
              "name": "email",
              "value": "={{ $json.body.email || \"\" }}",
              "type": "string"
            },
            {
              "id": "company",
              "name": "company",
              "value": "={{ $json.body.company || \"\" }}",
              "type": "string"
            },
            {
              "id": "budget",
              "name": "budget",
              "value": "={{ $json.body.budget || \"\" }}",
              "type": "string"
            },
            {
              "id": "need",
              "name": "need",
              "value": "={{ $json.body.need || $json.body.message || \"\" }}",
              "type": "string"
            },
            {
              "id": "website",
              "name": "website",
              "value": "={{ $json.body.website || \"\" }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000003",
      "name": "Qualify lead with OpenAI",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        560,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "options": {
          "timeout": 20000,
          "response": {
            "response": {
              "neverError": true,
              "fullResponse": false
            }
          }
        },
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ model: \"gpt-4.1-mini\", temperature: 0, response_format: { type: \"json_object\" }, messages: [{ role: \"system\", content: \"You qualify inbound leads. Reply with JSON only: {\\\"score\\\": number 0-100, \\\"qualified\\\": boolean, \\\"reason\\\": string, \\\"nextStep\\\": string}. Use only the fields you were given. Do not invent budget, company size, or intent.\" }, { role: \"user\", content: JSON.stringify({ name: $json.name, email: $json.email, company: $json.company, budget: $json.budget, need: $json.need, website: $json.website }) }] }) }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000,
      "onError": "continueErrorOutput",
      "credentials": {
        "httpHeaderAuth": {
          "id": "OPENAI_HEADER_AUTH",
          "name": "OpenAI Authorization header"
        }
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000004",
      "name": "Read qualification JSON",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        840,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const lead = $(\"Normalize lead\").item.json;\nconst content = $json.choices?.[0]?.message?.content || \"{}\";\nlet parsed = { score: 0, qualified: false, reason: \"The model returned an unreadable answer.\", nextStep: \"Review manually\" };\ntry { parsed = { ...parsed, ...JSON.parse(content) }; } catch (error) { parsed.reason = \"The model returned an unreadable answer.\"; }\nreturn [{ json: { ...lead, ...parsed } }];"
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000005",
      "name": "Lead score is at least 70",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1120,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "Lead score is at least 70-condition",
              "leftValue": "={{ $json.score }}",
              "rightValue": 70,
              "operator": {
                "type": "number",
                "operation": "gte"
              }
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000006",
      "name": "Create CRM lead",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1400,
        160
      ],
      "parameters": {
        "method": "POST",
        "url": "https://crm.example.com/api/leads",
        "options": {
          "timeout": 20000,
          "response": {
            "response": {
              "neverError": true,
              "fullResponse": false
            }
          }
        },
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ name: $json.name, email: $json.email, company: $json.company, budget: $json.budget, need: $json.need, score: $json.score, reason: $json.reason }) }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000,
      "onError": "continueErrorOutput",
      "credentials": {
        "httpHeaderAuth": {
          "id": "CRM_HEADER_AUTH",
          "name": "CRM API header"
        }
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000007",
      "name": "Alert sales in Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [
        1680,
        80
      ],
      "parameters": {
        "resource": "message",
        "operation": "post",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#sales-leads"
        },
        "text": "=Qualified lead {{ $json.name }} ({{ $json.email }}) scored {{ $json.score }}. {{ $json.reason }}",
        "otherOptions": {}
      },
      "credentials": {
        "slackApi": {
          "id": "SLACK_CREDENTIAL",
          "name": "Slack account"
        }
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000008",
      "name": "Log nurture lead in Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [
        1400,
        460
      ],
      "parameters": {
        "resource": "message",
        "operation": "post",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#sales-nurture"
        },
        "text": "=Not qualified yet: {{ $json.name }} scored {{ $json.score }}. {{ $json.reason }}",
        "otherOptions": {}
      },
      "credentials": {
        "slackApi": {
          "id": "SLACK_CREDENTIAL",
          "name": "Slack account"
        }
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000009",
      "name": "Alert qualifier failure",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [
        840,
        560
      ],
      "parameters": {
        "resource": "message",
        "operation": "post",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#sales-leads"
        },
        "text": "=Lead qualifier failed for {{ $(\"Normalize lead\").item.json.email || \"an unknown lead\" }}. Check the OpenAI or CRM node.",
        "otherOptions": {}
      },
      "credentials": {
        "slackApi": {
          "id": "SLACK_CREDENTIAL",
          "name": "Slack account"
        }
      }
    }
  ],
  "pinData": {
    "Receive lead form": [
      {
        "json": {
          "headers": {
            "x-form-token": "pinned-test-only"
          },
          "body": {
            "name": "Amina Rahman",
            "email": "amina@example.com",
            "company": "Northwind Clinic",
            "budget": "8000",
            "need": "A voice receptionist that can book visits after hours",
            "website": "https://example.com"
          }
        }
      }
    ]
  },
  "connections": {
    "Receive lead form": {
      "main": [
        [
          {
            "node": "Normalize lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize lead": {
      "main": [
        [
          {
            "node": "Qualify lead with OpenAI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Qualify lead with OpenAI": {
      "main": [
        [
          {
            "node": "Read qualification JSON",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Alert qualifier failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read qualification JSON": {
      "main": [
        [
          {
            "node": "Lead score is at least 70",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Lead score is at least 70": {
      "main": [
        [
          {
            "node": "Create CRM lead",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Log nurture lead in Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create CRM lead": {
      "main": [
        [
          {
            "node": "Alert sales in Slack",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Alert qualifier failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "timezone": "UTC",
    "saveDataErrorExecution": "all",
    "saveDataSuccessExecution": "all",
    "saveManualExecutions": true
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
