{
  "name": "Final Expense Leads to Morgan (Vapi)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "new-lead",
        "responseMode": "onReceived",
        "options": {}
      },
      "id": "a1f0c001-0000-4000-8000-000000000001",
      "name": "New Lead Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 0],
      "webhookId": "5d3c9a10-1111-4a6e-9c01-6f0a1b2c3d41"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "s01", "name": "assistantId", "value": "PASTE-YOUR-ASSISTANT-ID", "type": "string" },
            { "id": "s02", "name": "phoneNumberId", "value": "PASTE-YOUR-VAPI-PHONE-NUMBER-ID", "type": "string" },
            { "id": "s03", "name": "phone", "value": "={{ '+1' + String($json.body.phone || '').replace(/[^0-9]/g, '').slice(-10) }}", "type": "string" },
            { "id": "s04", "name": "firstName", "value": "={{ String($json.body.firstName || '').trim() }}", "type": "string" },
            { "id": "s05", "name": "age", "value": "={{ String($json.body.age || '') }}", "type": "string" },
            { "id": "s06", "name": "state", "value": "={{ String($json.body.state || '') }}", "type": "string" },
            { "id": "s07", "name": "product", "value": "={{ $json.body.product || 'final expense' }}", "type": "string" },
            { "id": "s08", "name": "tobacco", "value": "={{ String($json.body.tobacco || '') }}", "type": "string" },
            { "id": "s09", "name": "hasCoverage", "value": "={{ String($json.body.hasCoverage || '') }}", "type": "string" },
            { "id": "s10", "name": "coverageAmount", "value": "={{ String($json.body.coverageAmount || '') }}", "type": "string" },
            { "id": "s11", "name": "consent", "value": "={{ ['true', 'yes', 'on', '1'].includes(String($json.body.consent).toLowerCase()) }}", "type": "boolean" }
          ]
        },
        "options": {}
      },
      "id": "a1f0c001-0000-4000-8000-000000000002",
      "name": "Settings + Lead",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [220, 0]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "loose", "version": 2 },
          "conditions": [
            {
              "id": "c01",
              "leftValue": "={{ $json.consent }}",
              "rightValue": "",
              "operator": { "type": "boolean", "operation": "true", "singleValue": true }
            },
            {
              "id": "c02",
              "leftValue": "={{ $json.phone }}",
              "rightValue": "^[+]1[0-9]{10}$",
              "operator": { "type": "string", "operation": "regex" }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "id": "a1f0c001-0000-4000-8000-000000000003",
      "name": "Consent + Valid Phone?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [440, 0]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.vapi.ai/call",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ assistantId: $json.assistantId, phoneNumberId: $json.phoneNumberId, customer: { number: $json.phone, name: $json.firstName }, assistantOverrides: { variableValues: { firstName: $json.firstName, age: $json.age, state: $json.state, product: $json.product, tobacco: $json.tobacco, hasCoverage: $json.hasCoverage, coverageAmount: $json.coverageAmount } } }) }}",
        "options": {}
      },
      "id": "a1f0c001-0000-4000-8000-000000000004",
      "name": "Call Lead with Morgan",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [680, -100]
    },
    {
      "parameters": {},
      "id": "a1f0c001-0000-4000-8000-000000000005",
      "name": "Skip: No Consent or Bad Phone",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [680, 100]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "vapi-report",
        "responseMode": "onReceived",
        "options": {}
      },
      "id": "a1f0c001-0000-4000-8000-000000000006",
      "name": "Vapi Call Report Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 360],
      "webhookId": "7e4d8b20-2222-4b7f-8d12-7a1b2c3d4e52"
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "loose", "version": 2 },
          "conditions": [
            {
              "id": "c03",
              "leftValue": "={{ $json.body.message.type }}",
              "rightValue": "end-of-call-report",
              "operator": { "type": "string", "operation": "equals" }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "id": "a1f0c001-0000-4000-8000-000000000007",
      "name": "Is End-of-Call Report?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [220, 360]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "r01", "name": "date", "value": "={{ $now.toISO() }}", "type": "string" },
            { "id": "r02", "name": "callId", "value": "={{ $json.body.message.call?.id }}", "type": "string" },
            { "id": "r03", "name": "phone", "value": "={{ $json.body.message.call?.customer?.number }}", "type": "string" },
            { "id": "r04", "name": "firstName", "value": "={{ $json.body.message.call?.assistantOverrides?.variableValues?.firstName }}", "type": "string" },
            { "id": "r05", "name": "endedReason", "value": "={{ $json.body.message.endedReason }}", "type": "string" },
            { "id": "r06", "name": "transferred", "value": "={{ String($json.body.message.analysis?.structuredData?.transferred ?? '') }}", "type": "string" },
            { "id": "r07", "name": "doNotCall", "value": "={{ String($json.body.message.analysis?.structuredData?.doNotCall ?? '') }}", "type": "string" },
            { "id": "r08", "name": "interestLevel", "value": "={{ $json.body.message.analysis?.structuredData?.interestLevel }}", "type": "string" },
            { "id": "r09", "name": "summary", "value": "={{ $json.body.message.analysis?.summary }}", "type": "string" },
            { "id": "r10", "name": "recordingUrl", "value": "={{ $json.body.message.artifact?.recordingUrl ?? $json.body.message.recordingUrl }}", "type": "string" }
          ]
        },
        "options": {}
      },
      "id": "a1f0c001-0000-4000-8000-000000000008",
      "name": "Extract Call Results",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [440, 300]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": { "__rl": true, "mode": "url", "value": "PASTE-YOUR-GOOGLE-SHEET-URL" },
        "sheetName": { "__rl": true, "mode": "name", "value": "Calls" },
        "columns": { "mappingMode": "autoMapInputData", "value": {}, "matchingColumns": [], "schema": [] },
        "options": {}
      },
      "id": "a1f0c001-0000-4000-8000-000000000009",
      "name": "Log Call to Google Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [660, 300]
    }
  ],
  "connections": {
    "New Lead Webhook": { "main": [[{ "node": "Settings + Lead", "type": "main", "index": 0 }]] },
    "Settings + Lead": { "main": [[{ "node": "Consent + Valid Phone?", "type": "main", "index": 0 }]] },
    "Consent + Valid Phone?": {
      "main": [
        [{ "node": "Call Lead with Morgan", "type": "main", "index": 0 }],
        [{ "node": "Skip: No Consent or Bad Phone", "type": "main", "index": 0 }]
      ]
    },
    "Vapi Call Report Webhook": { "main": [[{ "node": "Is End-of-Call Report?", "type": "main", "index": 0 }]] },
    "Is End-of-Call Report?": { "main": [[{ "node": "Extract Call Results", "type": "main", "index": 0 }], []] },
    "Extract Call Results": { "main": [[{ "node": "Log Call to Google Sheet", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "pinData": {}
}
