{
  "name": "WF-01 · Speed-to-Lead",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "new-lead",
        "responseMode": "onReceived",
        "options": {}
      },
      "id": "n1",
      "name": "New Lead Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [200, 300],
      "webhookId": "new-lead-trigger"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "1", "name": "firstName", "value": "={{ $json.first_name || $json.firstName || '' }}", "type": "string" },
            { "id": "2", "name": "lastName",  "value": "={{ $json.last_name  || $json.lastName  || '' }}", "type": "string" },
            { "id": "3", "name": "email",     "value": "={{ $json.email }}", "type": "string" },
            { "id": "4", "name": "phone",     "value": "={{ $json.phone || $json.phone_number || '' }}", "type": "string" },
            { "id": "5", "name": "source",    "value": "={{ $json.source || 'web-form' }}", "type": "string" },
            { "id": "6", "name": "inquiry",   "value": "={{ $json.message || $json.inquiry || '' }}", "type": "string" }
          ]
        },
        "options": {}
      },
      "id": "n2",
      "name": "Normalize Lead Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3,
      "position": [420, 300]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.hubapi.com/crm/v3/objects/contacts/search",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "Authorization", "value": "Bearer {{ HUBSPOT_PRIVATE_APP_TOKEN }}" }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"filterGroups\": [{\n    \"filters\": [{\n      \"propertyName\": \"email\",\n      \"operator\": \"EQ\",\n      \"value\": \"{{ $json.email }}\"\n    }]\n  }]\n}",
        "options": {}
      },
      "id": "n3",
      "name": "Lookup in CRM",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [640, 300]
    },
    {
      "parameters": {
        "conditions": {
          "conditions": [
            {
              "id": "c1",
              "leftValue": "={{ $json.total }}",
              "rightValue": 0,
              "operator": { "type": "number", "operation": "equals" }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "n4",
      "name": "New or Existing?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [860, 300]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.hubapi.com/crm/v3/objects/contacts",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "Authorization", "value": "Bearer {{ HUBSPOT_PRIVATE_APP_TOKEN }}" }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"properties\": {\n    \"firstname\": \"{{ $('Normalize Lead Data').item.json.firstName }}\",\n    \"lastname\":  \"{{ $('Normalize Lead Data').item.json.lastName }}\",\n    \"email\":     \"{{ $('Normalize Lead Data').item.json.email }}\",\n    \"phone\":     \"{{ $('Normalize Lead Data').item.json.phone }}\",\n    \"lead_source\": \"{{ $('Normalize Lead Data').item.json.source }}\",\n    \"hs_lead_status\": \"NEW\"\n  }\n}",
        "options": {}
      },
      "id": "n5",
      "name": "Create Contact",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [1080, 180]
    },
    {
      "parameters": {
        "method": "PATCH",
        "url": "=https://api.hubapi.com/crm/v3/objects/contacts/{{ $json.results[0].id }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "Authorization", "value": "Bearer {{ HUBSPOT_PRIVATE_APP_TOKEN }}" }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"properties\": {\n    \"hs_lead_status\": \"NEW_INQUIRY\",\n    \"last_inquiry_note\": \"{{ $('Normalize Lead Data').item.json.inquiry }}\"\n  }\n}",
        "options": {}
      },
      "id": "n6",
      "name": "Update Existing Contact",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [1080, 420]
    },
    {
      "parameters": {
        "mode": "passThrough",
        "output": "input1"
      },
      "id": "n7",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3,
      "position": [1300, 300]
    },
    {
      "parameters": {
        "amount": 30,
        "unit": "seconds"
      },
      "id": "n8",
      "name": "Wait 30 Seconds",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [1500, 300]
    },
    {
      "parameters": {
        "operation": "send",
        "from": "{{ YOUR_TWILIO_PHONE_NUMBER }}",
        "to": "={{ $('Normalize Lead Data').item.json.phone }}",
        "message": "=Hi {{ $('Normalize Lead Data').item.json.firstName }} — got your request. Quick call today or tomorrow? Grab time here: {{ YOUR_CALENDAR_LINK }}"
      },
      "id": "n9",
      "name": "Send SMS",
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [1700, 160],
      "credentials": {
        "twilioApi": { "id": "twilio-creds", "name": "Twilio" }
      }
    },
    {
      "parameters": {
        "sendTo": "={{ $('Normalize Lead Data').item.json.email }}",
        "subject": "=Re: Your inquiry, {{ $('Normalize Lead Data').item.json.firstName }}",
        "emailType": "html",
        "message": "=<p>Hi {{ $('Normalize Lead Data').item.json.firstName }},</p><p>Thanks for reaching out — I wanted to follow up right away.</p><p>Grab a time that works for you: <a href=\"{{ YOUR_CALENDAR_LINK }}\">{{ YOUR_CALENDAR_LINK }}</a></p><p>Talk soon,<br>{{ YOUR_NAME }}</p>",
        "options": {}
      },
      "id": "n10",
      "name": "Send Email",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [1700, 300],
      "credentials": {
        "gmailOAuth2": { "id": "gmail-creds", "name": "Gmail" }
      }
    },
    {
      "parameters": {
        "channel": "{{ SLACK_LEADS_CHANNEL_ID }}",
        "text": "=:zap: *New lead:* {{ $('Normalize Lead Data').item.json.firstName }} {{ $('Normalize Lead Data').item.json.lastName }}\n*Source:* {{ $('Normalize Lead Data').item.json.source }}\n*Phone:* {{ $('Normalize Lead Data').item.json.phone }}\n*Email:* {{ $('Normalize Lead Data').item.json.email }}\n*Inquiry:* {{ $('Normalize Lead Data').item.json.inquiry }}\n\nSMS + email sent. Follow up within the hour.",
        "otherOptions": {}
      },
      "id": "n11",
      "name": "Notify Rep via Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2,
      "position": [1700, 440],
      "credentials": {
        "slackApi": { "id": "slack-creds", "name": "Slack" }
      }
    }
  ],
  "connections": {
    "New Lead Webhook":       { "main": [[{ "node": "Normalize Lead Data",           "type": "main", "index": 0 }]] },
    "Normalize Lead Data":    { "main": [[{ "node": "Lookup in CRM",                 "type": "main", "index": 0 }]] },
    "Lookup in CRM":          { "main": [[{ "node": "New or Existing?",               "type": "main", "index": 0 }]] },
    "New or Existing?": {
      "main": [
        [{ "node": "Create Contact",           "type": "main", "index": 0 }],
        [{ "node": "Update Existing Contact",  "type": "main", "index": 0 }]
      ]
    },
    "Create Contact":          { "main": [[{ "node": "Merge", "type": "main", "index": 0 }]] },
    "Update Existing Contact": { "main": [[{ "node": "Merge", "type": "main", "index": 1 }]] },
    "Merge":                   { "main": [[{ "node": "Wait 30 Seconds", "type": "main", "index": 0 }]] },
    "Wait 30 Seconds": {
      "main": [[
        { "node": "Send SMS",            "type": "main", "index": 0 },
        { "node": "Send Email",          "type": "main", "index": 0 },
        { "node": "Notify Rep via Slack","type": "main", "index": 0 }
      ]]
    }
  },
  "active": false,
  "settings": { "executionOrder": "v1" },
  "id": "wf-01-speed-to-lead",
  "versionId": "1.0.0"
}
