{
  "name": "Clawbot \u2014 Lead Intake (ALI-5)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "lead-intake",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook \u2014 New Lead",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Normalize incoming payload from any source:\n// Typeform, manual POST, email parser, etc.\nconst body = $input.first().json.body || $input.first().json;\n\nreturn {\n  source: body.source || 'webhook',\n  company_name: body.company_name || body.company || body.organization || null,\n  contact_name: body.contact_name || body.name || body.full_name || null,\n  contact_email: body.contact_email || body.email || null,\n  contact_phone: body.contact_phone || body.phone || null,\n  service_interest: body.service_interest || body.service || body.interest || null,\n  notes: body.notes || body.message || body.comment || null,\n  raw_payload: body\n};"
      },
      "id": "normalize-payload",
      "name": "Normalize Payload",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.aliviosearch.cloud/leads/create",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "source",
              "value": "={{ $json.source }}"
            },
            {
              "name": "company_name",
              "value": "={{ $json.company_name }}"
            },
            {
              "name": "contact_name",
              "value": "={{ $json.contact_name }}"
            },
            {
              "name": "contact_email",
              "value": "={{ $json.contact_email }}"
            },
            {
              "name": "contact_phone",
              "value": "={{ $json.contact_phone }}"
            },
            {
              "name": "service_interest",
              "value": "={{ $json.service_interest }}"
            },
            {
              "name": "notes",
              "value": "={{ $json.notes }}"
            },
            {
              "name": "raw_payload",
              "value": "={{ $json.raw_payload }}"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "fullResponse": false
            }
          }
        }
      },
      "id": "create-lead",
      "name": "POST /leads/create",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        680,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const lead = $input.first().json;\nreturn {\n  lead_id: lead.id,\n  company_name: lead.company_name,\n  status: lead.status,\n  source: lead.source\n};"
      },
      "id": "extract-lead-id",
      "name": "Extract Lead ID",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.aliviosearch.cloud/runs/create",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "agent_name",
              "value": "intake"
            },
            {
              "name": "model_alias",
              "value": "standard_worker"
            },
            {
              "name": "input_payload",
              "value": "={{ JSON.stringify({ lead_id: $json.lead_id, company_name: $json.company_name, source: $json.source }) }}"
            }
          ]
        },
        "options": {}
      },
      "id": "create-run",
      "name": "POST /runs/create",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const run = $input.first().json;\nconst lead = $('Extract Lead ID').first().json;\nreturn {\n  run_id: run.run_id,\n  lead_id: lead.lead_id,\n  company_name: lead.company_name,\n  agent_name: run.agent_name,\n  status: run.result_status || 'pending',\n  started_at: run.started_at\n};"
      },
      "id": "build-response",
      "name": "Build Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1340,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: true, lead_id: $json.lead_id, run_id: $json.run_id, agent: $json.agent_name, status: $json.status, company: $json.company_name }) }}",
        "options": {
          "responseCode": 201
        }
      },
      "id": "respond-201",
      "name": "Respond 201",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1560,
        180
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "leftValue": "={{ $json.run_id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "isNotEmpty"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "check-run-created",
      "name": "Run Created?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1340,
        480
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: false, error: 'Failed to create agent run', lead_id: $json.lead_id }) }}",
        "options": {
          "responseCode": 500
        }
      },
      "id": "respond-500",
      "name": "Respond 500",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1560,
        480
      ]
    }
  ],
  "connections": {
    "Webhook \u2014 New Lead": {
      "main": [
        [
          {
            "node": "Normalize Payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize Payload": {
      "main": [
        [
          {
            "node": "POST /leads/create",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "POST /leads/create": {
      "main": [
        [
          {
            "node": "Extract Lead ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Lead ID": {
      "main": [
        [
          {
            "node": "POST /runs/create",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "POST /runs/create": {
      "main": [
        [
          {
            "node": "Build Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Response": {
      "main": [
        [
          {
            "node": "Respond 201",
            "type": "main",
            "index": 0
          },
          {
            "node": "Run Created?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Run Created?": {
      "main": [
        [
          {
            "node": "Respond 201",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond 500",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true,
    "callerPolicy": "workflowsFromSameOwner",
    "errorWorkflow": ""
  },
  "staticData": null,
  "tags": [
    {
      "name": "clawbot"
    },
    {
      "name": "phase-1"
    }
  ]
}