{
 "id": "realwebleadcaptu",
 "name": "AutomateRecipes \u2013 web-lead-capture",
 "nodes": [
  {
   "parameters": {
    "httpMethod": "POST",
    "path": "new-lead",
    "responseMode": "responseNode",
    "options": {}
   },
   "name": "Lead form webhook",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2,
   "position": [
    0,
    0
   ],
   "id": "a3b38a4d-68f5-402e-a80e-f9ec74a8871a",
   "webhookId": "6bc7bc7a-f3f2-4516-9dd2-a430165fe872"
  },
  {
   "parameters": {
    "jsCode": "// EDIT THESE SETTINGS\nreturn [{json:{\n \"ntfyTopic\": \"CHANGE-ME-to-a-long-random-topic\",\n \"cfAccountId\": \"YOUR_CLOUDFLARE_ACCOUNT_ID\",\n \"kvNamespaceId\": \"YOUR_KV_NAMESPACE_ID\",\n \"keyPrefix\": \"lead:\"\n}}];"
   },
   "name": "Config",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    240,
    0
   ],
   "id": "2526416f-ebbb-4b56-88f2-63b4c9a9e5f3"
  },
  {
   "parameters": {
    "jsCode": "const b=$('Lead form webhook').first().json.body||{};const cfg=$('Config').first().json;if(!/^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(b.email||''))throw new Error('Invalid email');const lead={name:String(b.name||'').slice(0,200),email:String(b.email).slice(0,200),phone:String(b.phone||'').slice(0,50),message:String(b.message||'').slice(0,2000),received:new Date().toISOString()};return [{json:{lead,key:cfg.keyPrefix+lead.received+':'+lead.email}}];"
   },
   "name": "Validate lead",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    480,
    0
   ],
   "id": "b8becd6c-fe42-41de-be9e-1f70db6ae91a"
  },
  {
   "parameters": {
    "method": "PUT",
    "url": "=https://api.cloudflare.com/client/v4/accounts/{{ $('Config').first().json.cfAccountId }}/storage/kv/namespaces/{{ $('Config').first().json.kvNamespaceId }}/values/{{ $json.key }}",
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth",
    "sendBody": true,
    "contentType": "raw",
    "rawContentType": "application/json",
    "body": "={{ JSON.stringify($json.lead) }}",
    "options": {}
   },
   "name": "Save lead to Cloudflare KV",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    720,
    0
   ],
   "id": "620d9b54-27f5-440a-9ba9-75148dec976f",
   "credentials": {
    "httpHeaderAuth": {
     "id": "REPLACE_WITH_YOUR_CREDENTIAL",
     "name": "Cloudflare API token"
    }
   }
  },
  {
   "parameters": {
    "jsCode": "const v=$('Validate lead').first().json;if($input.first().json.success!==true)throw new Error('KV write failed');return [{json:{title:'New lead: '+(v.lead.name||v.lead.email),tags:'moneybag',message:`${v.lead.name} <${v.lead.email}> ${v.lead.phone}\\n${v.lead.message}`,key:v.key}}];"
   },
   "name": "Build alert",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    960,
    0
   ],
   "id": "17a420e6-08ba-4bbf-9e3b-8c728b1c18e1"
  },
  {
   "parameters": {
    "method": "POST",
    "url": "=https://ntfy.sh/{{ $('Config').first().json.ntfyTopic }}",
    "sendHeaders": true,
    "headerParameters": {
     "parameters": [
      {
       "name": "Title",
       "value": "={{ $json.title }}"
      },
      {
       "name": "Tags",
       "value": "={{ $json.tags || 'bell' }}"
      }
     ]
    },
    "sendBody": true,
    "contentType": "raw",
    "rawContentType": "text/plain",
    "body": "={{ $json.message }}",
    "options": {}
   },
   "name": "Send ntfy alert",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    1200,
    0
   ],
   "id": "d9f1d3c7-c931-47ad-939e-519e26596648"
  },
  {
   "parameters": {
    "respondWith": "json",
    "responseBody": "={{ JSON.stringify({ok:true,key:$('Build alert').first().json.key}) }}",
    "options": {}
   },
   "name": "Respond OK",
   "type": "n8n-nodes-base.respondToWebhook",
   "typeVersion": 1.1,
   "position": [
    1440,
    0
   ],
   "id": "398dec20-fdaf-4043-9be9-d25bb6e29e0c"
  }
 ],
 "connections": {
  "Lead form webhook": {
   "main": [
    [
     {
      "node": "Config",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Config": {
   "main": [
    [
     {
      "node": "Validate lead",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Validate lead": {
   "main": [
    [
     {
      "node": "Save lead to Cloudflare KV",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Save lead to Cloudflare KV": {
   "main": [
    [
     {
      "node": "Build alert",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Build alert": {
   "main": [
    [
     {
      "node": "Send ntfy alert",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Send ntfy alert": {
   "main": [
    [
     {
      "node": "Respond OK",
      "type": "main",
      "index": 0
     }
    ]
   ]
  }
 },
 "settings": {
  "executionOrder": "v1"
 },
 "active": false,
 "pinData": {}
}