{
  "name": "Public Website Contacts: one batch to CSV",
  "nodes": [
    {
      "parameters": {},
      "id": "manual-start",
      "name": "Run manually",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        240
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// Replace these public examples with your own public business website URLs.\nreturn [\n  { json: { url: 'https://www.iana.org/' } },\n  { json: { url: 'https://www.w3.org/' } },\n  { json: { url: 'https://example.com/' } },\n];"
      },
      "id": "sample-websites",
      "name": "Sample website URLs",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        240,
        240
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length === 0) throw new Error('Provide at least one public website URL.');\nif (items.length > 1000) throw new Error('One batch supports at most 1,000 URL rows.');\nconst urls = items.map((item, index) => {\n  if (typeof item.json.url !== 'string' || !item.json.url.trim()) {\n    throw new Error('Row ' + (index + 1) + ' needs a URL string in the url field.');\n  }\n  const url = item.json.url.trim();\n  if (!/^https?:\\/\\//i.test(url)) throw new Error('Use a public HTTP or HTTPS website URL.');\n  return url;\n});\n// The Actor validates public destinations and deduplicates apex/www aliases.\n// Use integer ten-thousandths: $0.05 start + $0.0005 per possible found site.\nconst maxTotalChargeUsd = ((500 + urls.length * 5) / 10000).toFixed(4);\nreturn [{ json: {\n  inputUrlCount: urls.length,\n  maxTotalChargeUsd,\n  body: {\n    startUrls: urls,\n    maxWebsites: urls.length,\n    maxPagesPerWebsite: 3,\n    maxConcurrency: 3,\n  },\n} }];"
      },
      "id": "one-batch",
      "name": "Prepare one batch",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        480,
        240
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.apify.com/v2/acts/kamnev.apps~public-website-contacts/run-sync-get-dataset-items",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "timeout",
              "value": "180"
            },
            {
              "name": "memory",
              "value": "256"
            },
            {
              "name": "maxTotalChargeUsd",
              "value": "={{ $json.maxTotalChargeUsd }}"
            },
            {
              "name": "restartOnError",
              "value": "false"
            },
            {
              "name": "format",
              "value": "json"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ $json.body }}",
        "options": {
          "timeout": 360000,
          "redirect": {
            "redirect": {
              "followRedirects": false
            }
          },
          "response": {
            "response": {
              "responseFormat": "json",
              "fullResponse": false,
              "neverError": false
            }
          }
        }
      },
      "id": "one-actor-request",
      "name": "Run one Actor batch",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        720,
        240
      ],
      "executeOnce": true,
      "alwaysOutputData": true,
      "retryOnFail": false,
      "continueOnFail": false,
      "notesInFlow": true,
      "notes": "Choose your own HTTP Header Auth credential: Authorization = Bearer <your Apify token>. One paid run for the whole batch; automatic retry is disabled."
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const statuses = ['found', 'no_public_contact', 'blocked', 'needs_javascript', 'failed'];\nconst states = ['complete', 'partial', 'not_started'];\nconst contactTypes = ['email', 'phone', 'contact_form', 'social'];\nconst output = [];\nfor (const [index, item] of $input.all().entries()) {\n  const row = item.json;\n  if (!row || typeof row.websiteKey !== 'string' || typeof row.status !== 'string') {\n    throw new Error('Expected an Actor website outcome; inspect the HTTP response before exporting.');\n  }\n  if (!statuses.includes(row.status)) throw new Error('Unsupported website status: ' + row.status);\n  if (!Array.isArray(row.contacts) || !states.includes(row.completeness?.state)) {\n    throw new Error('Website outcome is missing contacts or completeness evidence.');\n  }\n  if (row.status === 'found' && row.contacts.length === 0) {\n    throw new Error('Actor returned found without contact evidence.');\n  }\n  const base = {\n    domain: row.websiteKey,\n    status: row.status,\n    reason: row.reasonCode ?? '',\n    completeness: row.completeness.state,\n    completeness_reason: row.completeness.reasonCode ?? '',\n    pages_selected: row.completeness.selectedPages ?? '',\n    pages_attempted: row.completeness.attemptedPages ?? '',\n    pages_successful: row.completeness.successfulPages ?? '',\n    homepage_url: row.homepageUrl ?? '',\n    canonical_url: row.canonicalUrl ?? '',\n  };\n  // Keep one outcome row even when no contact was returned.\n  for (const contact of row.contacts.length ? row.contacts : [null]) {\n    if (contact && (!contactTypes.includes(contact.type) || typeof contact.value !== 'string' || typeof contact.sourceUrl !== 'string')) {\n      throw new Error('Contact evidence does not match the supported Actor schema.');\n    }\n    output.push({ json: {\n      ...base,\n      contact_type: contact?.type ?? '',\n      contact_value: contact?.value ?? '',\n      source_url: contact?.sourceUrl ?? '',\n    }, pairedItem: { item: index } });\n  }\n}\nreturn output;"
      },
      "id": "flatten-outcomes",
      "name": "Flatten contacts and outcomes",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        960,
        240
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// Protect CSV downloads opened in spreadsheet apps; keep the prior JSON node unchanged.\nreturn $input.all().map((item, index) => ({\n  json: Object.fromEntries(Object.entries(item.json).map(([key, value]) => [\n    key,\n    typeof value === 'string' && (/^\\s*[=+\\-@]/.test(value) || /^[\\t\\r\\n]/.test(value))\n      ? \"'\" + value\n      : value,\n  ])),\n  pairedItem: { item: index },\n}));"
      },
      "id": "safe-csv-cells",
      "name": "Prepare spreadsheet-safe cells",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1200,
        240
      ]
    },
    {
      "parameters": {
        "operation": "csv",
        "binaryPropertyName": "data",
        "options": {
          "fileName": "website-contacts.csv",
          "headerRow": true
        }
      },
      "id": "csv-download",
      "name": "Download contacts CSV",
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.1,
      "position": [
        1440,
        240
      ]
    },
    {
      "parameters": {
        "content": "## Before running\nSelect your own HTTP Header Auth credential on Run one Actor batch.\nHeader name: Authorization\nHeader value: Bearer followed by your Apify token.\n\nActor fees: $0.05 per started valid run + $0.0005 per unique found website. The three-URL sample caps Actor charges at $0.0515. A run with no found contacts can still cost the $0.05 start fee.\n\nEdit Sample website URLs. Prepare one batch combines 1–1,000 rows into one request. Keep automatic retry off; a broken connection can leave an Actor run in progress.",
        "height": 340,
        "width": 680
      },
      "id": "setup-note",
      "name": "Setup and fees",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        -180
      ]
    },
    {
      "parameters": {
        "content": "## Download and interpret\nOpen Download contacts CSV after execution, then download binary field data.\n\nEach contact gets a row with its source URL. Websites with no contacts keep one status row, including blocked, failed and not_started outcomes. These outcomes are not proof that a website has no public contacts.\n\nThe Actor collects published email addresses, tel: phone links, contact-form URLs and supported social links. It excludes LinkedIn and does not execute JavaScript.\n\nIf the HTTP request fails, inspect the run in Apify Console before executing again. Re-execution starts a new paid run.",
        "height": 340,
        "width": 680
      },
      "id": "results-note",
      "name": "Download and outcomes",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        800,
        -180
      ]
    }
  ],
  "connections": {
    "Run manually": {
      "main": [
        [
          {
            "node": "Sample website URLs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample website URLs": {
      "main": [
        [
          {
            "node": "Prepare one batch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare one batch": {
      "main": [
        [
          {
            "node": "Run one Actor batch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Run one Actor batch": {
      "main": [
        [
          {
            "node": "Flatten contacts and outcomes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Flatten contacts and outcomes": {
      "main": [
        [
          {
            "node": "Prepare spreadsheet-safe cells",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare spreadsheet-safe cells": {
      "main": [
        [
          {
            "node": "Download contacts CSV",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "tags": []
}
