{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "00bc1391-6349-4fa4-98f8-206a753b7ab9",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        19088,
        9312
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes"
            }
          ]
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "bbc9b701-77ab-45ce-82f9-9a28ba900d5f",
      "name": "Read Search Requests",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        19312,
        9312
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "your-google-sheet-id-here",
          "cachedResultName": "your-google-sheet-name-here"
        },
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "your-google-sheet-id-here"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "a750ac2f-4c88-4654-ade1-ab09a5f04dab",
      "name": "Search Businesses API",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        19552,
        9312
      ],
      "parameters": {
        "url": "https://local-business-data.p.rapidapi.com/search",
        "options": {},
        "sendQuery": true,
        "sendHeaders": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "query",
              "value": "={{ $json.Keyword + ' in ' + $json.Location }}"
            },
            {
              "name": "limit",
              "value": "25"
            },
            {
              "name": "extract_emails_and_contacts",
              "value": "true"
            }
          ]
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "5d1ff122-878c-4b23-91bf-dc56238f3732",
      "name": "Format Business Results",
      "type": "n8n-nodes-base.code",
      "position": [
        19808,
        9312
      ],
      "parameters": {
        "jsCode": "const results = [];\nconst businesses = $input.first().json.data || [];\nconst searchId = $input.first().json.ID;\n\nfor (const b of businesses) {\n  results.push({\n    json: {\n      'Search ID': searchId,\n      'Business Name': b.name || 'd',\n      'Phone': b.phone_number || 'a',\n      'Email': (b.emails_and_contacts && b.emails_and_contacts.emails && b.emails_and_contacts.emails[0]) || 'm',\n      'Address': b.full_address || 'n',\n      'Website': b.website || 'n'\n    }\n  });\n}\n\nreturn results;\n"
      },
      "typeVersion": 2
    },
    {
      "id": "d0a64565-dcd7-4de9-9143-f44c53ea3ba3",
      "name": "Write to Business Results",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        20432,
        9312
      ],
      "parameters": {
        "columns": {
          "value": {
            "Email": "={{ $json.Email }}",
            "Phone": "={{ $json.Phone }}",
            "Address": "={{ $json.Address }}",
            "Website": "={{ $json.Website }}",
            "Category": "={{$json.Category}}",
            "LeadScore": "={{$json.LeadScore}}",
            "Search ID": "={{ $('Read Search Requests').item.json.ID }}",
            "Confidence": "={{$json.Confidence}}",
            "Description": "={{$json.Description}}",
            "OutreachLine": "={{$json.OutreachLine}}",
            "Business Name": "={{ $json['Business Name'] }}"
          },
          "schema": [
            {
              "id": "Search ID",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Search ID",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Business Name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Business Name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Phone",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Phone",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Email",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Email",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Address",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Address",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Website",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Website",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Category",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Category",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Description",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Description",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "LeadScore",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "LeadScore",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Confidence",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Confidence",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "OutreachLine",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "OutreachLine",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "Search ID"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "appendOrUpdate",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 72104887,
          "cachedResultUrl": "your-google-sheet-id-here",
          "cachedResultName": "your-google-sheet-name-here"
        },
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "your-google-sheet-id-here"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "d96b476e-1368-4b56-9544-7caa55c3a57c",
      "name": "Message a model",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        20064,
        9312
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini",
          "cachedResultName": "GPT-4.1-MINI"
        },
        "options": {},
        "responses": {
          "values": [
            {
              "role": "system",
              "content": "You are an expert B2B lead generation analyst.\n\nYour job is to analyze business data and return structured, factual, and concise insights useful for sales outreach.\n\nRules:\n- Be realistic and critical (do not assume missing data)\n- Do NOT hallucinate unknown details\n- Keep descriptions short (max 2 sentences)\n- Classify businesses into clear categories (e.g., Real Estate, Hospitality, Healthcare, Fitness, Marketing, Retail, etc.)\n- Lead score must be based on data quality and outreach potential\n- Return ONLY valid JSON (no extra text, no explanation)\n- If data is missing or unclear, reduce confidence and lead_score accordingly.\n- Prioritize businesses that:\n    - Have a website\n    - Have direct contact info\n    - Appear to be actively operating"
            },
            {
              "content": "=Analyze the following business for lead generation:\n\nName: {{$json[\"Business Name\"]}}\nWebsite: {{$json[\"Website\"]}}\nAddress: {{$json[\"Address\"]}}\nPhone: {{$json[\"Phone\"]}}\nEmail: {{$json[\"Email\"]}}\n\nEvaluate:\n1. What type of business is this?\n2. Is it a good potential client for outreach?\n3. How strong is the available contact data?\n\nReturn JSON in this exact format:\n{\n  \"category\": \"\",\n  \"description\": \"\",\n  \"lead_score\": \"\",\n  \"confidence\": \"\",\n  \"outreach_line\": \"\"\n}\n\nGuidelines:\n- description: what the business does (max 2 lines)\n- lead_score:\n  - \"Hot\" \u2192 has email + website + clear business type\n  - \"Warm\" \u2192 partial info (phone/website but no email)\n  - \"Cold\" \u2192 very little data or unclear business\n- confidence: High / Medium / Low (based on data quality)\n- outreach_line: short personalized opener for cold outreach (1 line only)"
            }
          ]
        },
        "builtInTools": {}
      },
      "typeVersion": 2.1
    },
    {
      "id": "c1ff54fe-9e80-4c1b-bbe8-5e0a7643d1f4",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        20368,
        8944
      ],
      "parameters": {
        "color": 7,
        "width": 288,
        "height": 768,
        "content": "**Step 6 \u2013 Write to Business Results**\nStores enriched leads in Google Sheets for outreach."
      },
      "typeVersion": 1
    },
    {
      "id": "d1009b80-0b12-4dd1-839d-fcb67a68d621",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        18576,
        8944
      ],
      "parameters": {
        "width": 384,
        "height": 768,
        "content": "### Automated local business lead generation and AI-powered lead scoring\n\nThis workflow helps you automatically discover, enrich, and qualify local business leads using Google Sheets, RapidAPI, and OpenAI.\n\n**What it does:**\n\n* Reads keywords and locations from Google Sheets.\n* Finds local businesses with contact details via API.\n* Extracts email, phone, website, and address.\n* Uses AI to classify businesses and assign lead scores.\n* Generates short outreach messages.\n* Stores enriched leads back in Google Sheets.\n\n**How it works:**\n\n1. Schedule Trigger runs automatically.\n2. Reads search inputs from Google Sheets.\n3. Calls API to fetch business data.\n4. Formats results into structured fields.\n5. OpenAI analyzes and scores each lead.\n6. Saves results to a Google Sheets database.\n\n**Setup notes:**\n\n* Add your RapidAPI key in the HTTP Request node.\n* Connect Google Sheets and OpenAI credentials.\n* Ensure input sheet includes Keyword, Location, and ID.\n* Avoid hardcoding credentials.\n* Use a Set node for easy customization.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "2c143079-9e8a-4bf2-a693-93db65c8c853",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        18992,
        8944
      ],
      "parameters": {
        "color": 7,
        "height": 768,
        "content": "**Step 1 \u2013 Schedule Trigger**\nRuns the workflow automatically at set intervals.\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "cccd712c-56a4-4dee-a27d-e8da7eee3924",
      "name": "Sticky Note9",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        19248,
        8944
      ],
      "parameters": {
        "color": 7,
        "height": 768,
        "content": "**Step 2 \u2013 Read Search Requests**\nFetches keywords and locations from Google Sheets.\n\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "e4d1e73a-6954-48dd-b910-ab9d9db50f10",
      "name": "Sticky Note10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        19504,
        8944
      ],
      "parameters": {
        "color": 7,
        "height": 768,
        "content": "**Step 3 \u2013 Search Businesses API**\nRetrieves local business data with contact details.\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "970ee2ce-3ad1-424f-908c-963e7ce1533e",
      "name": "Sticky Note11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        19760,
        8944
      ],
      "parameters": {
        "color": 7,
        "height": 768,
        "content": "**Step 4 \u2013 Format Business Results**\nCleans and structures API response into usable fields.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "a781bf7a-a2e0-4bf7-8902-d5cd1cde928f",
      "name": "Sticky Note12",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        20032,
        8944
      ],
      "parameters": {
        "color": 7,
        "width": 304,
        "height": 768,
        "content": "**Step 5 \u2013 Message a model (OpenAI)**\nClassifies leads, assigns scores, and generates outreach lines."
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Message a model": {
      "main": [
        [
          {
            "node": "Write to Business Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Read Search Requests",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Search Requests": {
      "main": [
        [
          {
            "node": "Search Businesses API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Businesses API": {
      "main": [
        [
          {
            "node": "Format Business Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Business Results": {
      "main": [
        [
          {
            "node": "Message a model",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}