{
  "name": "Monkey D. Scraping | Affiliate website research to CSV",
  "nodes": [
    {
      "id": "9043d2ac-9f6a-436d-83ec-176d8dc68085",
      "name": "Manual start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        300
      ],
      "parameters": {}
    },
    {
      "id": "a47abaa3-5372-4b57-bed9-d48cfb9cb4b9",
      "name": "Website list",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        300
      ],
      "parameters": {
        "jsCode": "const websites = ['https://www.hubspot.com/partners/affiliates','https://www.semrush.com/'];\nif (!Array.isArray(websites) || websites.length < 1 || websites.length > 10 || websites.some(u => typeof u !== 'string' || !/^https?:\\/\\//i.test(u))) throw new Error('Use 1\u201310 public website URLs.');\nreturn [{json:{websites}}];"
      }
    },
    {
      "id": "20d7b47c-9fb5-4dce-9e25-d6b23d7f4af3",
      "name": "Start Affiliate",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        300
      ],
      "parameters": {
        "url": "https://api.apify.com/v2/acts/monkey.d.scraping~affiliate-program-finder/runs",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "timeout": 30000
        },
        "method": "POST",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "memory",
              "value": "512"
            },
            {
              "name": "timeout",
              "value": "180"
            },
            {
              "name": "maxTotalChargeUsd",
              "value": "0.04"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json) }}"
      }
    },
    {
      "id": "5e9f78e4-9a85-4374-a319-686c287eb511",
      "name": "Read run",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        660,
        300
      ],
      "parameters": {
        "url": "={{ 'https://api.apify.com/v2/actor-runs/' + $('Start Affiliate').first().json.data.id + '?waitForFinish=5' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "timeout": 30000
        }
      }
    },
    {
      "id": "6a4e3342-3f39-41f3-be13-c08ab81fd8ec",
      "name": "Check run",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        300
      ],
      "parameters": {
        "jsCode": "const run = $input.first().json.data;\nif (!run?.id || !run.status) throw new Error('Invalid run response. Check Apify Console before starting again.');\nif (['FAILED', 'ABORTED', 'TIMED-OUT'].includes(run.status)) throw new Error(`Run ${run.id}: ${run.status}. Inspect its log and partial dataset in Apify Console. No successful CSV exported.`);\nif (!['READY', 'RUNNING', 'TIMING-OUT', 'ABORTING', 'SUCCEEDED'].includes(run.status)) throw new Error(`Unexpected run status: ${run.status}. Inspect run ${run.id}.`);\nif (run.status !== 'SUCCEEDED' && $runIndex >= 29) throw new Error(`Polling limit reached for ${run.id}. Check the existing run before retrying; do not start a duplicate.`);\nreturn [{json: run}];\n"
      }
    },
    {
      "id": "aff92698-45d4-45a7-93db-a9bacb7664fc",
      "name": "Finished",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1100,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "1f691130-2c30-4fa8-a812-266d43c9ef21",
              "leftValue": "={{ $json.status }}",
              "rightValue": "SUCCEEDED",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "2c9ef3a0-4816-4836-b217-c41721581cf9",
      "name": "Wait before polling",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1320,
        300
      ],
      "parameters": {
        "amount": 5,
        "unit": "seconds"
      }
    },
    {
      "id": "e9e9ff8a-0858-44e0-9c87-3cd121ba32d6",
      "name": "Read summary",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1540,
        300
      ],
      "parameters": {
        "url": "={{ 'https://api.apify.com/v2/key-value-stores/' + $('Start Affiliate').first().json.data.defaultKeyValueStoreId + '/records/SUMMARY' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "timeout": 30000
        }
      }
    },
    {
      "id": "837782c4-ef7a-4bab-818a-1fb96f383991",
      "name": "Read dataset",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1760,
        300
      ],
      "parameters": {
        "url": "={{ 'https://api.apify.com/v2/datasets/' + $('Start Affiliate').first().json.data.defaultDatasetId + '/items?format=json&clean=true&limit=100' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "timeout": 30000,
          "response": {
            "response": {
              "fullResponse": true,
              "responseFormat": "json"
            }
          }
        }
      }
    },
    {
      "id": "3c34c011-b961-46da-8101-f15a9425bf37",
      "name": "Download research files",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1980,
        300
      ],
      "parameters": {
        "jsCode": "const response = $input.first().json;\nconst rows = response.body;\nif (!Array.isArray(rows)) throw new Error('Invalid dataset response. No CSV exported.');\nconst total = Number(response.headers?.['x-apify-pagination-total']);\nif (!Number.isInteger(total) || total !== rows.length) throw new Error('Incomplete dataset export. Inspect and export the full dataset in Apify Console.');\nconst run = $('Start Affiliate').first().json.data;\nconst summary = $('Read summary').first().json;\nconst groups = {found: [], not_found: [], review: []};\nfor (const row of rows) {\n  if (!['found','not_found','inconclusive','error'].includes(row.status)) throw new Error('Unknown research status. Review the dataset.');\n  if (!/^https?:\\/\\//i.test(row.inputUrl || '')) throw new Error('Missing input URL. Review the dataset.');\n  if (row.status === 'found' && !/^https?:\\/\\//i.test(row.programUrl || '')) throw new Error('Found program has no source URL. Review the dataset.');\n  groups[['inconclusive','error'].includes(row.status) ? 'review' : row.status].push(row);\n}\nconst fields = ['inputUrl','status','hasProgram','programUrl','commissionText','recurringCommission','cookieDays','cookieEvidence','programAvailability','coverage','errorCode','checkedAt'];\nlet protectedCells = 0;\nconst cell = value => {\n  let s = String(value ?? '');\n  if (/^[\\s\\u0000-\\u001f]*[=+@-]/u.test(s)) {s = \"'\" + s; protectedCells++;}\n  return '\"' + s.replaceAll('\"','\"\"') + '\"';\n};\nconst binary = {};\nfor (const [key, items] of Object.entries(groups)) {\n  const csv = '\\uFEFF' + [fields.map(cell).join(','), ...items.map(row => fields.map(f => cell(row[f])).join(','))].join('\\r\\n') + '\\r\\n';\n  binary[key] = {data:Buffer.from(csv).toString('base64'),mimeType:'text/csv',fileName:'affiliate-'+key+'.csv'};\n}\nconst warnings = [];\nif (summary.unprocessed > 0) warnings.push('INPUTS_UNPROCESSED');\nif (summary.budgetReached) warnings.push('BUDGET_REACHED');\nif (groups.review.length) warnings.push('MANUAL_REVIEW_REQUIRED');\nconst report = {publisher:'Monkey D. Scraping',runId:run.id,rows:rows.length,counts:Object.fromEntries(Object.entries(groups).map(([k,v])=>[k,v.length])),warnings,spreadsheetProtectedCells:protectedCells,note:'Blank terms are unknown, not zero. not_found is limited to checked coverage. Inconclusive/error records are in review.csv. Formula-like CSV cells are prefixed with an apostrophe; original data stays in Apify.',summary};\nbinary.report={data:Buffer.from(JSON.stringify(report,null,2)).toString('base64'),mimeType:'application/json',fileName:'affiliate-report.json'};\nreturn [{json:report,binary}];\n"
      }
    },
    {
      "id": "d28b37e8-1e27-4ab3-a8d8-2b1eb05eed0e",
      "name": "Setup \u2014 Monkey D. Scraping",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        -180
      ],
      "parameters": {
        "content": "## Monkey D. Scraping \u2014 Affiliate research to CSV\n1. Assign one HTTP Header Auth credential to all four HTTP nodes: Authorization = Bearer YOUR_APIFY_TOKEN. Never paste the token into this workflow JSON.\n2. Edit Website list (1\u201310 public URLs). Default: two brands, $0.04 run charge cap, 180 seconds. More inputs can exceed this cap; inspect the report for unprocessed inputs.\n3. Execute manually. Download found, not_found and review CSV files plus the JSON report from the final node Binary tab. Empty groups still have headers.\n4. found may have missing terms; blank does not mean zero. not_found is not proof of no program. Review inconclusive/errors separately and keep source URLs.\nNo schedule, outreach or spreadsheet connection. No automatic retry of the paid start request: inspect the existing run after a network error before starting again. Export limit 100 rows; this starter allows at most 10 websites.",
        "height": 420,
        "width": 620
      }
    }
  ],
  "connections": {
    "Manual start": {
      "main": [
        [
          {
            "node": "Website list",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Website list": {
      "main": [
        [
          {
            "node": "Start Affiliate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start Affiliate": {
      "main": [
        [
          {
            "node": "Read run",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read run": {
      "main": [
        [
          {
            "node": "Check run",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check run": {
      "main": [
        [
          {
            "node": "Finished",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Finished": {
      "main": [
        [
          {
            "node": "Read summary",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Wait before polling",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait before polling": {
      "main": [
        [
          {
            "node": "Read run",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read summary": {
      "main": [
        [
          {
            "node": "Read dataset",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read dataset": {
      "main": [
        [
          {
            "node": "Download research files",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "tags": []
}
