{
  "id": "geEVmbXLYDSA8UGM",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Instagram Lead Generation",
  "tags": [],
  "nodes": [
    {
      "id": "5f49d3d3-5e81-4b9d-9bc4-687ee97e2e8f",
      "name": "When clicking \u2018Test workflow\u2019",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -256,
        0
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "9990229b-1098-4ff5-8906-c57a260f4d15",
      "name": "Edit Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        -32,
        0
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "8b66c6fb-0773-4c66-b088-e35fbb27fd08",
              "name": "site",
              "type": "string",
              "value": "instagram"
            },
            {
              "id": "9dddc30f-b90d-4759-81be-96161bc5dd48",
              "name": "field_of_interest",
              "type": "string",
              "value": "beauty & hair"
            },
            {
              "id": "9fba044b-013f-4ff0-8687-6a6c8954011a",
              "name": "target_country",
              "type": "string",
              "value": "USA"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "a40f44bc-efee-4c89-8fbc-77cd0d09ff3e",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        224,
        0
      ],
      "parameters": {
        "text": "=field of interest: {{ $json.field_of_interest }}\ntarget country: {{ $json.target_country }}",
        "options": {
          "systemMessage": "You are an assistant designed to generate Google search queries based on user instructions. Your sole task is to create queries following a specific format like the one below:\n\nsite:instagram.com/*/ (inurl:\"/beauty\" OR inurl:\"/hair\" OR intitle:\"beauty\" OR intitle:\"hair\") (\"England\" OR \"UK\" OR \"London\" OR \"Manchester\" OR \"Birmingham\" OR \"Liverpool\" OR \"Leeds\") (\"@gmail.com\" OR \"@yahoo.com\" OR \"@hotmail.com\" OR \"@outlook.com\" OR \"@aol.com\" OR \"@icloud.com\") -inurl:\"/p/\" -inurl:\"/reel/\" -inurl:\"/tv/\"\n\nYour output must:\n\n1. Only return the Google search query, no explanation or extra text.\n\n2. Replace the niche-related keywords (e.g., \"beauty\" or \"hair\") with the field of interest specified by the user.\n\n3. Replace the location filters (e.g., \"England\", \"UK\", \"London\") with the target country provided by the user. Add cities related to the target country as well.\n\n4. Always include the email provider filters:\n(\"@gmail.com\" OR \"@yahoo.com\" OR \"@hotmail.com\" OR \"@outlook.com\" OR \"@aol.com\" OR \"@icloud.com\")\n\n5. Always exclude media content using:\n-inurl:\"/p/\" -inurl:\"/reel/\" -inurl:\"/tv/\"\n\n6. Ensure the query remains **clean, optimized, and structurally consistent** with the original format. Do **not explain** the query \u2014 return only the **raw search string**.\n"
        },
        "promptType": "define"
      },
      "typeVersion": 1.9
    },
    {
      "id": "79b4c5f6-7fe9-46eb-932b-4d98593c2607",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        224,
        208
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "d2eecb84-b111-401c-9fb2-91722625cb66",
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        736,
        32
      ],
      "parameters": {
        "url": "https://api.apify.com/v2/acts/apify~google-search-scraper/run-sync-get-dataset-items?token=YOUR_TOKEN_HERE",
        "options": {},
        "jsonBody": "={\n    \"focusOnPaidAds\": false,\n    \"forceExactMatch\": false,\n    \"includeIcons\": false,\n    \"includeUnfilteredResults\": false,\n    \"maxPagesPerQuery\": 20,\n    \"mobileResults\": false,\n    \"queries\": \"{{ $json.escapedQuery }}\",\n    \"resultsPerPage\": 100,\n    \"saveHtml\": false,\n    \"saveHtmlToKeyValueStore\": true\n} ",
        "sendBody": true,
        "specifyBody": "json"
      },
      "typeVersion": 4.2
    },
    {
      "id": "ff3a6ff0-f66f-499d-b56f-2ae8f22534ba",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "position": [
        496,
        0
      ],
      "parameters": {
        "jsCode": "// n8n Function node code\nconst inputQuery = $input.first().json.output;\n\n// Replace all \" with \\\"\nconst escapedQuery = inputQuery.replace(/\"/g, '\\\\\"');\n\n// Return as output\nreturn [\n  {\n    json: {\n      escapedQuery\n    }\n  }\n];"
      },
      "typeVersion": 2
    },
    {
      "id": "dd29f519-c629-4302-ad17-e1a5b0555e46",
      "name": "Split Out",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        896,
        32
      ],
      "parameters": {
        "options": {},
        "fieldToSplitOut": "organicResults"
      },
      "typeVersion": 1
    },
    {
      "id": "32b57f66-c3cd-4a04-a787-3f7662af13c2",
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1456,
        80
      ],
      "parameters": {
        "url": "https://api.apify.com/v2/acts/apify~instagram-scraper/run-sync-get-dataset-items?token=YOUR_TOKEN_HERE",
        "options": {},
        "jsonBody": "={\n    \"addParentData\": false,\n    \"directUrls\": [\n        \"{{ $json.cleanedUrl }}\"\n    ],\n    \"enhanceUserSearchWithFacebookPage\": false,\n    \"isUserReelFeedURL\": false,\n    \"isUserTaggedFeedURL\": false,\n    \"resultsLimit\": 2,\n    \"resultsType\": \"details\",\n    \"searchLimit\": 1,\n    \"searchType\": \"hashtag\"\n} ",
        "sendBody": true,
        "specifyBody": "json"
      },
      "typeVersion": 4.2
    },
    {
      "id": "6e52cddd-1cf2-433f-8f78-cd1f0f2d89ad",
      "name": "Code1",
      "type": "n8n-nodes-base.code",
      "position": [
        1312,
        80
      ],
      "parameters": {
        "jsCode": "// n8n Function node code\nconst inputUrl = $input.first().json.url;\n\n// Remove trailing slash if present\nconst cleanedUrl = inputUrl.replace(/\\/$/, \"\");\n\nreturn [\n  {\n    json: {\n      cleanedUrl\n    }\n  }\n];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "28aaf56d-ee27-4a81-9cd7-b0e43d13aeb9",
      "name": "Information Extractor",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "position": [
        1600,
        80
      ],
      "parameters": {
        "text": "={{ $json.biography }}",
        "options": {
          "systemPromptTemplate": "You are an expert extraction algorithm. Extract relevant information from the given web scraped text data and present it in JSON format with the attribute Email . If email attribute is not found in the text, assign them the value \"N/A\". Always extract email address in small hand."
        },
        "schemaType": "manual",
        "inputSchema": "{\n\t\"type\": \"array\",\n\t\"properties\": {\n\t\t\"Email\": {\n\t\t\t\"type\": \"string\",\n\t\t\t\"format\": \"email\"\n\t\t}\n\t}\n}"
      },
      "typeVersion": 1
    },
    {
      "id": "f2a03fb3-9171-482e-b078-05a5dff93e6a",
      "name": "OpenAI Chat Model1",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1600,
        304
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-3.5-turbo",
          "cachedResultName": "gpt-3.5-turbo"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "9d1fbc5b-538a-4d82-a897-00a03ce4d5cc",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        1152,
        32
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "067b1f22-eb31-4573-80e1-47fac5bddb7b",
      "name": "Postgres",
      "type": "n8n-nodes-base.postgres",
      "position": [
        2032,
        80
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "Instagram_Leads_Beauty&Hair",
          "cachedResultName": "Instagram_Leads_Beauty&Hair"
        },
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "columns": {
          "value": {
            "email": "={{ $('Filter').item.json.output[0].Email }}",
            "user_name": "={{ $('HTTP Request1').item.json.username }}",
            "account_link": "={{ $('HTTP Request1').item.json.inputUrl }}",
            "follower_count": "={{ $('HTTP Request1').item.json.followsCount }}",
            "target_country": "={{ $('Edit Fields').item.json.target_country }}",
            "field_of_interest": "={{ $('Edit Fields').item.json.field_of_interest }}"
          },
          "schema": [
            {
              "id": "user_name",
              "type": "string",
              "display": true,
              "removed": false,
              "required": true,
              "displayName": "user_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "follower_count",
              "type": "number",
              "display": true,
              "required": true,
              "displayName": "follower_count",
              "defaultMatch": false,
              "canBeUsedToMatch": false
            },
            {
              "id": "account_link",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "account_link",
              "defaultMatch": false,
              "canBeUsedToMatch": false
            },
            {
              "id": "email",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "email",
              "defaultMatch": false,
              "canBeUsedToMatch": false
            },
            {
              "id": "target_country",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "target_country",
              "defaultMatch": false,
              "canBeUsedToMatch": false
            },
            {
              "id": "field_of_interest",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "field_of_interest",
              "defaultMatch": false,
              "canBeUsedToMatch": false
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "user_name"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "upsert"
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "c8d53921-24d1-4f79-ab40-2f5794f0f057",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -112,
        -128
      ],
      "parameters": {
        "color": 5,
        "width": 256,
        "height": 304,
        "content": "## Specifying Our Target Niche \n"
      },
      "typeVersion": 1
    },
    {
      "id": "a466c89c-8766-4927-871c-474a67a42265",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        176,
        -128
      ],
      "parameters": {
        "width": 464,
        "height": 464,
        "content": "## Generating Custom Optimized Search Engine Query for finding Instagram Accounts\n"
      },
      "typeVersion": 1
    },
    {
      "id": "8d2a5c18-c22b-4773-8890-12fa1e0b7913",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        672,
        -128
      ],
      "parameters": {
        "color": 3,
        "width": 368,
        "height": 336,
        "content": "## Extracting Search Results Using Apify Api"
      },
      "typeVersion": 1
    },
    {
      "id": "6d5711e0-b5d3-43ee-b1f4-b8db6c853d7d",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1072,
        -128
      ],
      "parameters": {
        "color": 4,
        "width": 848,
        "height": 560,
        "content": "## Going Through Each of the Accounts & Extracting Leads From User's Bio"
      },
      "typeVersion": 1
    },
    {
      "id": "0c298e44-251f-4718-9f58-ba8904cb2708",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1952,
        -32
      ],
      "parameters": {
        "color": 6,
        "width": 256,
        "height": 272,
        "content": "## Storing Leads In PostgreSQL"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "cd76a0c4-c864-4bb3-906f-57bfb43d2a9e",
  "connections": {
    "Code": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code1": {
      "main": [
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Split Out",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request1": {
      "main": [
        [
          {
            "node": "Information Extractor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "Information Extractor",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Information Extractor": {
      "main": [
        [
          {
            "node": "Postgres",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking \u2018Test workflow\u2019": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}