{
  "name": "ControlLaunchPad - Signed new lead to Google Sheets (starter)",
  "active": false,
  "nodes": [
    {
      "parameters": {
        "content": "## Setup\nSet CLP_WEBHOOK_SECRET in private n8n Variables. Variables are readable by users with access to them: restrict your workspace, and never share a configured export containing secrets. n8n Cloud includes crypto; self-hosted instances must allow it with NODE_FUNCTION_ALLOW_BUILTIN=crypto. Keep Raw Body enabled. Activate/publish and subscribe the production webhook in CLP. Signed test events validate and stop before output. Wrong, missing or stale signatures must fail. Use one lead you control for the final output test. Retries can repeat outputs; add durable event-ID deduplication if one output per event is required. Provider import/runtime is not verified by offline checks. Set CLP_REPORT_SPREADSHEET_ID and CLP_REPORT_RANGE (default Sheet1!A:I). Connect a Google Sheets OAuth2 credential on the HTTP node. Create columns Event ID, Occurred at, Lead ID, First name, Last name, Email, Phone, Property address, Source. Subscribe only to lead.created. RAW input stores seller text as data, not formulas.",
        "height": 370,
        "width": 820
      },
      "id": "setup-notes",
      "name": "Setup notes",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        200,
        0
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "clp-new-lead-report",
        "responseMode": "lastNode",
        "options": {
          "rawBody": true
        }
      },
      "id": "clp-event",
      "name": "CLP event",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        460
      ],
      "webhookId": "clp-new-lead-report"
    },
    {
      "parameters": {
        "jsCode": "const crypto = require('crypto');\nconst input = $input.first();\nconst headers = input.json.headers || {};\nconst secret = $vars.CLP_WEBHOOK_SECRET;\nif (typeof secret !== 'string' || secret.length < 16 || secret === 'CLP_WEBHOOK_SECRET') throw new Error('Configure the private CLP_WEBHOOK_SECRET variable.');\nconst timestamp = String(headers['x-clp-timestamp'] || '');\nconst signature = String(headers['x-clp-signature'] || '');\nif (!/^\\d{10,11}$/.test(timestamp) || !/^sha256=[a-f0-9]{64}$/.test(signature)) throw new Error('Missing or malformed CLP signature.');\nif (!input.binary?.data) throw new Error('Keep Raw Body enabled on the Webhook node.');\nconst raw = await this.helpers.getBinaryDataBuffer(0, 'data');\nconst expected = 'sha256=' + crypto.createHmac('sha256', secret).update(timestamp + '.').update(raw).digest('hex');\nif (Math.abs(Date.now() / 1000 - Number(timestamp)) > 300 || !crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expected))) throw new Error('CLP signature or timestamp could not be verified.');\nconst event = JSON.parse(raw.toString('utf8'));\nif (event.object !== 'event' || event.version !== 1 || typeof event.id !== 'string' || !event.id || typeof event.type !== 'string' || typeof event.test !== 'boolean') throw new Error('Unexpected CLP event format.');\nif (event.test) return [];\nif (event.type !== 'lead.created') return [];\nconst lead = event.data?.object;\nif (lead?.object !== 'lead' || typeof lead.id !== 'string') throw new Error('Missing lead record.');\nconst spreadsheetId = $vars.CLP_REPORT_SPREADSHEET_ID;\nconst range = $vars.CLP_REPORT_RANGE || 'Sheet1!A:I';\nif (typeof spreadsheetId !== 'string' || !/^[A-Za-z0-9_-]{10,150}$/.test(spreadsheetId) || spreadsheetId.startsWith('REPLACE_')) throw new Error('Set CLP_REPORT_SPREADSHEET_ID.');\nif (typeof range !== 'string' || !range.trim() || range.length > 200) throw new Error('Set a valid CLP_REPORT_RANGE.');\nconst text = (value) => typeof value === 'string' ? value : '';\nreturn [{json:{eventId:event.id,spreadsheetId,range,values:[[event.id,text(event.occurredAt),lead.id,text(lead.firstName),text(lead.lastName),text(lead.email),text(lead.phone),text(lead.propertyAddress),text(lead.source)]]}}];"
      },
      "id": "verify",
      "name": "Verify and route CLP event",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        520,
        460
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ 'https://sheets.googleapis.com/v4/spreadsheets/' + encodeURIComponent($json.spreadsheetId) + '/values/' + encodeURIComponent($json.range) + ':append' }}",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "googleSheetsOAuth2Api",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "valueInputOption",
              "value": "RAW"
            },
            {
              "name": "insertDataOption",
              "value": "INSERT_ROWS"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ majorDimension: 'ROWS', values: $json.values }) }}",
        "options": {
          "timeout": 15000
        }
      },
      "id": "append-report",
      "name": "Append Google Sheets report",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        800,
        460
      ]
    }
  ],
  "connections": {
    "CLP event": {
      "main": [
        [
          {
            "node": "Verify and route CLP event",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verify and route CLP event": {
      "main": [
        [
          {
            "node": "Append Google Sheets report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "tags": []
}
