{
  "name": "Lead Gen Agent Perplexity Sonar API & Google Maps",
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "id": "10219833-1c98-4408-8e21-c67b56501a4a",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.1,
      "position": [
        80,
        60
      ],
      "webhookId": "0fb951a2-b676-41c5-8e7a-54d0a14a53ef"
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "c384a720-6d23-4fef-9579-c738ea4dfb2c",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1,
      "position": [
        280,
        300
      ],
      "credentials": {
        "openAiApi": {
          "id": "LSihAOI8NTU3m5ce",
          "name": "OpenAi account 3"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "7"
      },
      "id": "1b269548-31d0-42e8-95e3-4a1e43713ab0",
      "name": "Window Buffer Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.2,
      "position": [
        420,
        360
      ],
      "disabled": true
    },
    {
      "parameters": {
        "toolDescription": "This bot helps you extract contact information from Google Maps for lead generation.\n\nUse \"q\" for your search keyword.\n\nUse \"ll\" to input latitude and longitude in this format: @latitude,longitude,zoom (e.g. @34.052235,-118.243683,12z).\n\nUse \"page\" to specify the current page of results.",
        "url": "https://google.serper.dev/maps",
        "sendQuery": true,
        "parametersQuery": {
          "values": [
            {
              "name": "q"
            },
            {
              "name": "ll"
            },
            {
              "name": "page"
            }
          ]
        },
        "sendHeaders": true,
        "parametersHeaders": {
          "values": [
            {
              "name": "X-API-KEY",
              "valueProvider": "fieldValue"
            }
          ]
        }
      },
      "id": "564cd561-8832-47da-aa72-373a6240a750",
      "name": "Map Searcher",
      "type": "@n8n/n8n-nodes-langchain.toolHttpRequest",
      "typeVersion": 1.1,
      "position": [
        540,
        280
      ]
    },
    {
      "parameters": {},
      "id": "cd8290f1-1fcd-4482-bc65-cb43c6de0134",
      "name": "Execute Workflow Trigger",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1,
      "position": [
        -760,
        580
      ],
      "disabled": true
    },
    {
      "parameters": {
        "content": "SAVE DATA TO GOOGLE SHEETS WORKFLOW",
        "height": 80,
        "width": 453,
        "color": 3
      },
      "id": "0c57922d-521b-4848-970c-ae95645a2d22",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -560,
        460
      ]
    },
    {
      "parameters": {
        "name": "store_to_google_sheet",
        "description": "This is a tool to parse the input and store to google sheet, columns including:\n\n{\n    \"Name\": \"xxx\",\n    \"Address\": \"xxx\",\n    \"Website\": \"xxx\",\n    \"Rating\": \"xxx\",\n    \"Opening Hours\": \"xxx\",\n    \"Email\": \"xxx\",\n    \"Phone\": \"xxx\"\n}",
        "workflowId": {
          "__rl": true,
          "value": "xFMvp1hCsYzR6jh7",
          "mode": "id"
        }
      },
      "id": "eb87d242-2537-42b0-b434-0f40ef437c6e",
      "name": "Store to Google Sheet",
      "type": "@n8n/n8n-nodes-langchain.toolWorkflow",
      "typeVersion": 1.2,
      "position": [
        700,
        220
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1bTam6oAFCdwiCkzTqYOqk-0UNf_BNynsKqgNpM1oOaE",
          "mode": "list",
          "cachedResultName": "Tutorial 9.2 Perplexity <> Google Map",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1bTam6oAFCdwiCkzTqYOqk-0UNf_BNynsKqgNpM1oOaE/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1bTam6oAFCdwiCkzTqYOqk-0UNf_BNynsKqgNpM1oOaE/edit#gid=0"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [],
          "schema": [
            {
              "id": "UUID",
              "displayName": "UUID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Name",
              "displayName": "Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Address",
              "displayName": "Address",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Website",
              "displayName": "Website",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Rating",
              "displayName": "Rating",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Opening Hours",
              "displayName": "Opening Hours",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Phone",
              "displayName": "Phone",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Email",
              "displayName": "Email",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Background",
              "displayName": "Background",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {}
      },
      "id": "aa2f48e0-4f3d-4ba9-9416-f735eabf4e29",
      "name": "Google Sheets1",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        -360,
        580
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "TQHNRwbFTxYXwnxr",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "return { json: { response: \"ok\" } }"
      },
      "id": "4e88a0f2-41b4-4e96-aa66-0e72d1f29c51",
      "name": "Return response1",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -140,
        580
      ]
    },
    {
      "parameters": {
        "jsCode": "const v7 = () => {\n  return 'tttttttt-tttt-7xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {\n    const r = Math.trunc(Math.random() * 16);\n    const v = c == 'x' ? r : (r & 0x3 | 0x8);\n    return v.toString(16);\n  }).replace(/^[t]{8}-[t]{4}/, function() {\n    const unixtimestamp = Date.now().toString(16).padStart(12, '0');\n    return unixtimestamp.slice(0, 8) + '-' + unixtimestamp.slice(8);\n  });\n}\n\n\nconst data = JSON.parse($input.all()[0].json.query);\nconst dataWithId = data.map(i => {\n  const uuid = v7();\n  return { UUID: uuid, ... i }\n});\n\nreturn dataWithId;"
      },
      "id": "4680f57c-8689-4fd3-b7c7-94ebb5569743",
      "name": "Parse content1",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -560,
        580
      ]
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "documentId": {
          "__rl": true,
          "value": "1bTam6oAFCdwiCkzTqYOqk-0UNf_BNynsKqgNpM1oOaE",
          "mode": "list",
          "cachedResultName": "Tutorial 9.2 Perplexity <> Google Map",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1bTam6oAFCdwiCkzTqYOqk-0UNf_BNynsKqgNpM1oOaE/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/160mpEi2DqWmzU53jOyXCOti3is1fP16vkR-GBgbVycg/edit#gid=0"
        },
        "event": "rowAdded",
        "options": {}
      },
      "id": "4b794759-eec7-4451-8343-f25356ed7303",
      "name": "Google Sheets Trigger",
      "type": "n8n-nodes-base.googleSheetsTrigger",
      "typeVersion": 1,
      "position": [
        760,
        540
      ],
      "credentials": {
        "googleSheetsTriggerOAuth2Api": {
          "id": "5nUg6NoF70KAeRha",
          "name": "Google Sheets Trigger account"
        }
      }
    },
    {
      "parameters": {
        "options": {
          "reset": false
        }
      },
      "id": "ac87c08b-6f66-4202-9721-a68b33308916",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        1200,
        540
      ]
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all()[0].json.choices[0].message.content;\nconst cleaned = items.replace('```json', '').replace('```', '')\nconst parsed = JSON.parse(cleaned);\n\nreturn {data: parsed}"
      },
      "id": "1b34c41b-8b0a-4846-b56a-7a510ae47576",
      "name": "Convert to JSON Object1",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1620,
        540
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "781fc345-87ce-4f2a-84f4-059bd802fc17",
              "leftValue": "={{ $json.Name }}",
              "rightValue": "Name",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              }
            },
            {
              "id": "bbaeb84c-0c19-42f2-9e06-e826e69833fa",
              "leftValue": "={{ $json.Name }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "bf022e86-faa0-4f6c-875a-60afbc8e12ba",
      "name": "Filter",
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.2,
      "position": [
        980,
        540
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "1bTam6oAFCdwiCkzTqYOqk-0UNf_BNynsKqgNpM1oOaE",
          "mode": "list",
          "cachedResultName": "Tutorial 9.2 Perplexity <> Google Map",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1bTam6oAFCdwiCkzTqYOqk-0UNf_BNynsKqgNpM1oOaE/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1bTam6oAFCdwiCkzTqYOqk-0UNf_BNynsKqgNpM1oOaE/edit#gid=0"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "UUID": "={{ $('Loop Over Items').item.json.UUID }}",
            "Email": "={{ $json.data.Email }}",
            "Background": "={{ $json.data['Company Background'] }}"
          },
          "matchingColumns": [
            "UUID"
          ],
          "schema": [
            {
              "id": "UUID",
              "displayName": "UUID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Name",
              "displayName": "Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Address",
              "displayName": "Address",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Website",
              "displayName": "Website",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Rating",
              "displayName": "Rating",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Opening Hours",
              "displayName": "Opening Hours",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Phone",
              "displayName": "Phone",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Email",
              "displayName": "Email",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Background",
              "displayName": "Background",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "row_number",
              "displayName": "row_number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": true,
              "removed": true
            }
          ]
        },
        "options": {}
      },
      "id": "89048f05-4f8d-4bc0-8d65-b816233aaa0c",
      "name": "Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1820,
        540
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "TQHNRwbFTxYXwnxr",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "content": "Retrieve Company Emails & Insights via Perplexity",
        "height": 315.62164546431364,
        "width": 1340.354600213169,
        "color": 3
      },
      "id": "e9c18e99-34fc-422b-a771-95731927fea3",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        720,
        460
      ]
    },
    {
      "parameters": {
        "options": {
          "systemMessage": "You are a research agent helping user to find the contact information from Google Map \"Map Searcher\" or website. After searching, you should structure the output for human readable list.\n\nAfter searching, put your result to \"Store To Google Sheets\", when you call this tool, you must be in this json format in a single call:\n\n[\n{\n    \"Name\": \"xxx\",\n    \"Address\": \"xxx\",\n    \"Website\": \"xxx\",\n    \"Rating\": \"xxx\",\n    \"Opening Hours\": \"xxx\",\n    \"Email\": \"xxx\",\n    \"Phone\": \"xxx\"\n},\n{\n    \"Name\": \"xxx\",\n    \"Address\": \"xxx\",\n    \"Website\": \"xxx\",\n    \"Rating\": \"xxx\",\n    \"Opening Hours\": \"xxx\",\n    \"Email\": \"xxx\",\n    \"Phone\": \"xxx\"\n},...\n]\n\nIf you loop over multiple pages, you should call \"Store To Google Sheets\" each time after \"Map Searcher\" so that we can keep storing without aggregate.",
          "maxIterations": 10
        }
      },
      "id": "297c5d20-df02-4117-9119-33abcc4b6b5c",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.7,
      "position": [
        300,
        60
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.perplexity.ai/chat/completions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"model\": \"sonar\",\n  \"messages\": [\n    {\n      \"role\": \"system\",\n      \"content\": \"You are a research assistant. Retrieve data from a company and return it. If a field does not exist, return \\\"N/A\\\" for that field. Return JSON only, without any other information.\"\n    },\n    {\n      \"role\": \"user\",\n      \"content\": \"Get the Email and Company Background of {{ $json.Name }} in {{ $json.Address }}, reference website: {{ $json.Website }}\"\n    }\n  ],\n  \"response_format\": {\n    \"type\": \"json_schema\",\n    \"json_schema\": {\n      \"schema\": {\n        \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n        \"title\": \"CompanyDetails\",\n        \"type\": \"object\",\n        \"properties\": {\n          \"Email\": {\n            \"type\": \"string\"\n          },\n          \"Background\": {\n            \"type\": \"string\"\n          }\n      },\n      \"required\": [\n        \"Email\",\n        \"Background\"\n      ]\n    }\n    }\n  }\n}",
        "options": {}
      },
      "id": "61ee6958-d745-48db-8e47-89f3df243ece",
      "name": "Get Contact Details via Perplexity",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1420,
        540
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "r505SEKIJmugv4IH",
          "name": "Header Auth account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "When chat message received": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Window Buffer Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Map Searcher": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Execute Workflow Trigger": {
      "main": [
        [
          {
            "node": "Parse content1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Store to Google Sheet": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets1": {
      "main": [
        [
          {
            "node": "Return response1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse content1": {
      "main": [
        [
          {
            "node": "Google Sheets1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets Trigger": {
      "main": [
        [
          {
            "node": "Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Get Contact Details via Perplexity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to JSON Object1": {
      "main": [
        [
          {
            "node": "Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Contact Details via Perplexity": {
      "main": [
        [
          {
            "node": "Convert to JSON Object1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "eea6084b-00eb-427d-9e4d-2410ec0f3528",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "ae59e6b831344dba624c99cbe778a4bc47150f8e64de86a8011d15bac8e58c21"
  },
  "id": "RAl977fZAe0Bp1wC",
  "tags": []
}