{
  "name": "Google LSA → Light AI",
  "nodes": [
    {
      "parameters": {
        "content": "## Google LSA → Light AI\nNeeds Google Ads API access (developer token) and a Google Ads OAuth2 credential.\n1. Set customer_id, login_customer_id and developer_token in \"Settings\".\n2. Check the API version in the request URL.\n3. Create a Header Auth credential named \"Light AI API key\": Name = Authorization, Value = Bearer lai_live_… (a key with leads:write).\nDocs: https://docs.light-ai.app/inbound/meta-and-lsa",
        "height": 280,
        "width": 380
      },
      "name": "About",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -320
      ]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 15
            }
          ]
        }
      },
      "name": "Every 15 minutes",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "customer",
              "name": "customer_id",
              "value": "1234567890",
              "type": "string"
            },
            {
              "id": "login",
              "name": "login_customer_id",
              "value": "1234567890",
              "type": "string"
            },
            {
              "id": "token",
              "name": "developer_token",
              "value": "YOUR_DEVELOPER_TOKEN",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://googleads.googleapis.com/v21/customers/{{ $json.customer_id }}/googleAds:search",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "googleAdsOAuth2Api",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "developer-token",
              "value": "={{ $json.developer_token }}"
            },
            {
              "name": "login-customer-id",
              "value": "={{ $json.login_customer_id }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ query: \"SELECT local_services_lead.id, local_services_lead.lead_type, local_services_lead.category_id, local_services_lead.contact_details, local_services_lead.creation_date_time FROM local_services_lead WHERE local_services_lead.creation_date_time > '\" + $now.minus({ hours: 2 }).toUTC().toFormat('yyyy-MM-dd HH:mm:ss') + \"'\" }) }}",
        "options": {}
      },
      "name": "Search LSA leads",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// Google Ads API search results → Light AI leads. Field names follow the\n// local_services_lead resource; check them against your API version.\nconst rows = $input.all().flatMap((item) => item.json.results ?? []);\nreturn rows.map((row) => {\n  const lead = row.localServicesLead ?? {};\n  const contact = lead.contactDetails ?? {};\n  const [first, ...rest] = String(contact.consumerName ?? '').trim().split(/\\s+/);\n  return { json: { lead: {\n    source: 'google_lsa',\n    source_detail: `LSA ${String(lead.leadType ?? 'lead').toLowerCase()} lead`,\n    external_ref: { system: 'google_lsa', namespace: String($('Settings').first().json.customer_id), id: String(lead.id) },\n    contact: { first_name: first || 'Google', last_name: rest.join(' ') || 'LSA lead', email: contact.email ?? null, phone: contact.phoneNumber ?? null },\n    service_interest: 'christmas_lights',\n    message: lead.categoryId ? `LSA category ${lead.categoryId}` : null,\n    attribution: { utm_source: 'google', utm_medium: 'lsa' },\n    submitted_at: lead.creationDateTime ? new Date(lead.creationDateTime.replace(' ', 'T') + 'Z').toISOString() : null,\n  } } };\n});"
      },
      "name": "Map to Light AI",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.light-ai.app/v1/leads",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Idempotency-Key",
              "value": "={{ 'lsa-' + $json.lead.external_ref.id }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.lead) }}",
        "options": {}
      },
      "name": "Send lead to Light AI",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        880,
        0
      ]
    }
  ],
  "connections": {
    "Every 15 minutes": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Search LSA leads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search LSA leads": {
      "main": [
        [
          {
            "node": "Map to Light AI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Map to Light AI": {
      "main": [
        [
          {
            "node": "Send lead to Light AI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  }
}
