{
  "id": "E4K85OV19sy8Jrly",
  "meta": {
    "instanceId": "f46651348590f9c7e3e7fe91218ed49590c553ab737d5cc247951397ff85fa93",
    "templateCredsSetupCompleted": true
  },
  "name": "Domain -> Email Extraction using Apollo API copy",
  "tags": [],
  "nodes": [
    {
      "id": "02646c75-dad3-4b21-8203-e86732374a2c",
      "name": "When clicking ‘Test workflow’",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -400,
        -60
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "69175622-b2fb-4a56-83bf-a55290f1c725",
      "name": "Loop Targets",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        40,
        -60
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "5e6af8ff-2bc1-44b4-9523-3e55a785ee4d",
      "name": "Pull Target Domains",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -180,
        -60
      ],
      "parameters": {
        "options": {},
        "filtersUI": {
          "values": [
            {
              "lookupColumn": "Status"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1qSlCPA-FqrQZUS7DIqYaZbhdhxZDxIM3xYrswJKEZ-o/edit#gid=0",
          "cachedResultName": "Target Domains"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1qSlCPA-FqrQZUS7DIqYaZbhdhxZDxIM3xYrswJKEZ-o",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1qSlCPA-FqrQZUS7DIqYaZbhdhxZDxIM3xYrswJKEZ-o/edit?usp=drivesdk",
          "cachedResultName": "Apollo Data"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "credential-id",
          "name": "googleSheetsOAuth2Api Credential"
        }
      },
      "typeVersion": 4.5
    },
    {
      "id": "884b3766-8d4b-43a3-8a14-e3eee1a3d678",
      "name": "Get People By Domain",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        300,
        -60
      ],
      "parameters": {
        "url": "https://api.apollo.io/v1/mixed_people/search",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"q_organization_domains_list\": [\"{{ $json['Domain To Enrich'] }}\"],\n  \"per_page\": 10,\n  \"page\": 1\n}\n",
        "sendBody": true,
        "sendHeaders": true,
        "specifyBody": "json",
        "headerParameters": {
          "parameters": [
            {
              "name": "x-api-key",
              "value": "Your  API KEY "
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "df77689b-ccd3-4638-a981-4cf13fbf8a70",
      "name": "Clean Up Results",
      "type": "n8n-nodes-base.code",
      "position": [
        480,
        -60
      ],
      "parameters": {
        "jsCode": "// Function node code\nconsole.log('Initial data:', JSON.stringify(items, null, 2));\n\n// Get the first item since n8n usually wraps data in an array\nconst data = items[0].json;\nconsole.log('Data structure:', JSON.stringify(data, null, 2));\n\n// Now process the people data\nconst people = data.people || [];\n\ntry {\n  return people.map(person => {\n    console.log('Processing person:', JSON.stringify(person, null, 2));\n    return {\n      json: {\n        firstName: person.first_name || '',\n        lastName: person.last_name || '',\n        email: person.email || '',\n        websiteUrl: person.website_url || '',\n        linkedinUrl: person.linkedin_url || '',\n        twitterUrl: person.twitter_url || '',\n        facebookUrl: person.facebook_url || '',\n        phone: person.primary_phone?.number || '',\n        company: {\n          name: person.company_name || '',\n          domain: person.primary_domain || '',\n          foundedYear: person.founded_year || '',\n          logo: person.logo_url || ''\n        },\n        professional: {\n          departments: person.departments || [],\n          seniority: person.seniority || '',\n          functions: person.functions || []\n        }\n      }\n    };\n  });\n} catch (error) {\n  console.log('Error occurred. Full items:', JSON.stringify(items, null, 2));\n  throw new Error(`Error processing data: ${error.message}`);\n}"
      },
      "typeVersion": 2
    },
    {
      "id": "bc0f01b0-3225-4023-9e21-0bcebab00808",
      "name": "Loop Over Results",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        680,
        -60
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "65533eb2-a738-4cf3-9cbc-480a911e4fb2",
      "name": "Get Person Info",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        920,
        -60
      ],
      "parameters": {
        "url": "=https://api.apollo.io/api/v1/people/match?first_name={{ $json.firstName }}&last_name={{ $json.lastName }}&domain={{ $('Get People By Domain').item.json.breadcrumbs[0].value[0] }}&reveal_personal_emails=false&reveal_phone_number=false ",
        "method": "POST",
        "options": {},
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-api-key",
              "value": "Your API Key"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "4c369c7e-e79b-4bf1-97d4-25abac4256db",
      "name": "Clean Up",
      "type": "n8n-nodes-base.code",
      "position": [
        1140,
        -60
      ],
      "parameters": {
        "jsCode": "// Function node to extract specific fields from Apollo response\ntry {\n    const results = items.map(item => {\n        const person = item.json.person; // Access the person object directly\n        \n        return {\n            json: {\n                firstName: person.first_name || '',\n                lastName: person.last_name || '',\n                email: person.email || '',\n                linkedinUrl: person.linkedin_url || '',\n                title: person.title || '',\n                twitter: person.twitter_url || '',\n                facebook: person.facebook_url || '',\n                organizationName: person.employment_history?.[0]?.organization_name || '', // Get current organization from employment history\n                headline: person.headline || '',\n                photoUrl: person.photo_url || ''\n            }\n        };\n    });\n\n    console.log(`Successfully processed ${results.length} records`);\n    return results;\n\n} catch (error) {\n    console.error('Error processing data:', error);\n    throw new Error(`Failed to process data: ${error.message}`);\n}"
      },
      "typeVersion": 2
    },
    {
      "id": "f0eaa1e7-299f-4ec8-ae26-90bbe1483a1e",
      "name": "Results To Results Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1340,
        -60
      ],
      "parameters": {
        "columns": {
          "value": {
            "Email": "={{ $json.email }}",
            "Title": "={{ $json.title }}",
            "Company": "={{ $('Loop Targets').item.json['Domain To Enrich'] }}",
            "Last Name": "={{ $json.lastName }}",
            "Linkedin ": "={{ $json.linkedinUrl }}",
            "First Name": "={{ $json.firstName }}"
          },
          "schema": [
            {
              "id": "Company",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Company",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "First Name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "First Name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Last Name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Last Name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Title",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Title",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Email",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Email",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Linkedin ",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Linkedin ",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 308352805,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1qSlCPA-FqrQZUS7DIqYaZbhdhxZDxIM3xYrswJKEZ-o/edit#gid=308352805",
          "cachedResultName": "Results"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1qSlCPA-FqrQZUS7DIqYaZbhdhxZDxIM3xYrswJKEZ-o",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1qSlCPA-FqrQZUS7DIqYaZbhdhxZDxIM3xYrswJKEZ-o/edit?usp=drivesdk",
          "cachedResultName": "Apollo Data"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "credential-id",
          "name": "googleSheetsOAuth2Api Credential"
        }
      },
      "typeVersion": 4.5
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "3fe93ef2-9ba0-474e-9b44-dbb9f9fa4fd8",
  "connections": {
    "Clean Up": {
      "main": [
        [
          {
            "node": "Results To Results Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Targets": {
      "main": [
        [],
        [
          {
            "node": "Get People By Domain",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Person Info": {
      "main": [
        [
          {
            "node": "Clean Up",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Clean Up Results": {
      "main": [
        [
          {
            "node": "Loop Over Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Results": {
      "main": [
        [
          {
            "node": "Loop Targets",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get Person Info",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pull Target Domains": {
      "main": [
        [
          {
            "node": "Loop Targets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get People By Domain": {
      "main": [
        [
          {
            "node": "Clean Up Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Results To Results Sheet": {
      "main": [
        [
          {
            "node": "Loop Over Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking ‘Test workflow’": {
      "main": [
        [
          {
            "node": "Pull Target Domains",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}