{
  "name": "Lection scrape to Google Sheets (dedupe on unique column)",
  "nodes": [
    {
      "parameters": {
        "content": "## Lection to Google Sheets (dedupe)\n1. Create a Google Sheet with a header row. Column names must match the scraped column names exactly.\n2. In this workflow, set the **Sheet ID** in the *Append or update rows* node and the **unique column** in its *Matching columns* field.\n3. Set `REPLACE_WITH_LECTION_SECRET` in the *Secret matches?* node to the same value you enter as the webhook secret in Lection (or delete that node if you do not use a secret).\n4. Activate the workflow, then copy the production webhook URL into Lection.",
        "height": 300,
        "width": 620,
        "color": 4
      },
      "id": "ec1006ea-e9e4-4309-a6ba-ae54b2c66674",
      "name": "Setup notes",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        -260
      ]
    },
    {
      "parameters": {
        "content": "### In Lection\nScheduled scrape → Delivery channel: **Webhook** → paste this workflow's production URL. Optional: set the same secret.\nTest sends (`scheduled_scrape.test`) are acknowledged but NOT written, so sample rows never land in your sheet. Use a real run (Run now) to test.",
        "height": 150,
        "width": 620,
        "color": 5
      },
      "id": "7f24d19e-f64f-464e-8400-8ed690caf908",
      "name": "Lection schedule note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        60
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "REPLACE-ME-lection-path",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "bddfb2d7-b2e3-4308-a9e3-35fc2e4f8ffc",
      "name": "Lection webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        300
      ],
      "webhookId": "9d329dfc-1be9-4ec1-b446-0a4ba737defc"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "6d8c2692-c7df-4e31-a7bf-f7e91f161c0b",
              "leftValue": "={{ $json.headers['x-lection-secret'] }}",
              "rightValue": "REPLACE_WITH_LECTION_SECRET",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "89d59154-fc91-4d50-992a-13baaf455831",
      "name": "Secret matches?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "text",
        "responseBody": "Unauthorized",
        "options": {
          "responseCode": 401
        }
      },
      "id": "d172aff5-debf-4bf7-94f0-87e8a8c15c64",
      "name": "Respond 401",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        480,
        480
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "3412dc1d-e7dd-44c2-baae-9a2c1bc727a4",
              "leftValue": "={{ $json.body.event }}",
              "rightValue": "scheduled_scrape.completed",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            },
            {
              "id": "2a6fbf4f-19c1-4218-aa9f-9ce222f28c01",
              "leftValue": "={{ $json.body.dataset.rowCount }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "5b83fa88-96e4-45f1-b70a-42c3bbab49db",
      "name": "Completed run with rows?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        480,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\"ok\": true, \"rowsWritten\": 0, \"note\": \"Not a completed run with rows (test send or empty run); nothing written.\"}",
        "options": {}
      },
      "id": "4ce20732-f9ba-4291-9ebc-b35da287efb6",
      "name": "Respond skipped",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        720,
        480
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "body.dataset.records",
        "options": {}
      },
      "id": "21a3dd32-7bfd-4eda-90fd-51462b392070",
      "name": "Split records",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        720,
        300
      ]
    },
    {
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "REPLACE_SPREADSHEET_ID"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Sheet1"
        },
        "options": {},
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [
            "REPLACE_UNIQUE_COLUMN"
          ],
          "schema": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        }
      },
      "id": "21b69713-ddc3-418a-9bfb-a3d76a5d116d",
      "name": "Append or update rows",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        960,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "REPLACE_CREDENTIAL_ID",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "rows",
        "options": {}
      },
      "id": "22cb3806-3978-4b8a-a563-176cd74f0424",
      "name": "Summarize",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        1200,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { \"ok\": true, \"rowsWritten\": $json.rows.length } }}",
        "options": {}
      },
      "id": "f856ffbe-a5d6-4b6f-9071-1d9863b4d005",
      "name": "Respond 200",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1440,
        300
      ]
    }
  ],
  "pinData": {},
  "connections": {
    "Lection webhook": {
      "main": [
        [
          {
            "node": "Secret matches?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Secret matches?": {
      "main": [
        [
          {
            "node": "Completed run with rows?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond 401",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Completed run with rows?": {
      "main": [
        [
          {
            "node": "Split records",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond skipped",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split records": {
      "main": [
        [
          {
            "node": "Append or update rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append or update rows": {
      "main": [
        [
          {
            "node": "Summarize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Summarize": {
      "main": [
        [
          {
            "node": "Respond 200",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "dbec039d-b1d3-4fec-b847-f89ea377c356",
  "meta": {
    "templateCredsSetupCompleted": false,
    "description": "Receives Lection scheduled-scrape webhooks and upserts each row into Google Sheets keyed on a unique column."
  },
  "tags": []
}
