{
  "name": "Meta Lead Ads → Light AI",
  "nodes": [
    {
      "parameters": {
        "content": "## Meta Lead Ads → Light AI\n1. Connect the Facebook Lead Ads Trigger to your page and form.\n2. Create a Header Auth credential named \"Light AI API key\": Name = Authorization, Value = Bearer lai_live_… (a key with leads:write).\n3. Adjust custom questions in \"Map to Light AI\".\nDocs: https://docs.light-ai.app/inbound/meta-and-lsa",
        "height": 260,
        "width": 380
      },
      "name": "About",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -300
      ]
    },
    {
      "parameters": {
        "event": "newLead",
        "options": {}
      },
      "name": "Facebook Lead Ads Trigger",
      "type": "n8n-nodes-base.facebookLeadAdsTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "webhookId": "light-ai-meta-leads"
    },
    {
      "parameters": {
        "jsCode": "// Facebook Lead Ads → Light AI lead. Handles both the simplified trigger\n// output and the raw Graph API shape (field_data: [{ name, values }]).\nreturn $input.all().map((item) => {\n  const j = item.json;\n  const f = {};\n  for (const field of j.field_data ?? []) f[field.name] = (field.values ?? [])[0];\n  Object.assign(f, j.data ?? {}, j.fields ?? {});\n  const leadId = String(j.id ?? j.leadgen_id ?? f.leadgen_id ?? '');\n  const consent = {};\n  for (const answer of j.custom_disclaimer_responses ?? []) {\n    consent[answer.checkbox_key ?? 'disclaimer'] = answer.is_checked === '1' || answer.is_checked === true;\n  }\n  return { json: { lead: {\n    source: 'meta_lead_ad',\n    source_detail: j.form_name ?? j.form?.name ?? 'Meta lead ad',\n    external_ref: { system: 'meta_lead_ads', namespace: String(j.page_id ?? j.page?.id ?? ''), id: leadId },\n    contact: {\n      first_name: f.first_name ?? ((f.full_name ?? '').split(' ')[0] || null),\n      last_name: f.last_name ?? ((f.full_name ?? '').split(' ').slice(1).join(' ') || null),\n      email: f.email ?? null,\n      phone: f.phone_number ?? f.phone ?? null,\n    },\n    address: f.street_address ? {\n      line1: f.street_address, city: f.city ?? null, region: f.state ?? null,\n      postal_code: f.zip_code ?? f.post_code ?? null, country: f.country ?? 'US',\n    } : null,\n    service_interest: f.service ?? 'christmas_lights',\n    message: f.message ?? null,\n    attribution: { utm_source: 'facebook', campaign_id: j.campaign?.id ?? j.campaign_id ?? null, ad_id: j.ad?.id ?? j.ad_id ?? null, form_id: j.form?.id ?? j.form_id ?? null },\n    consent: Object.keys(consent).length ? { ...consent, method: 'meta_lead_ad' } : null,\n    submitted_at: j.created_time ? new Date(j.created_time).toISOString() : null,\n  } } };\n});"
      },
      "name": "Map to Light AI",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.light-ai.app/v1/leads",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Idempotency-Key",
              "value": "={{ 'meta-' + $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": {
    "Facebook Lead Ads Trigger": {
      "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
  }
}
