{
  "name": "08-B: Apollo Enrichment + DE Email Outreach",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 1
            }
          ]
        }
      },
      "id": "schedule-trigger",
      "name": "Hourly Check",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        100,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT id, company_name, country, website, email, phone, keyword\nFROM leads\nWHERE email IS NULL\n  AND country = 'DE'\n  AND stage = 'Lead'\n  AND created_at > NOW() - INTERVAL '7 days'\nLIMIT 10",
        "additionalFields": {}
      },
      "id": "fetch-unenriched",
      "name": "Fetch DE Leads (no email)",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        300,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "conditions": [
            {
              "leftValue": "={{ $json.length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ]
        }
      },
      "id": "has-leads",
      "name": "Any leads?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://api.apollo.io/api/v1/organizations/enrich",
        "method": "GET",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-api-key",
              "value": "={{ $vars.APOLLO_API_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "domain",
              "value": "={{ $json.website ? $json.website.replace(/https?:\\/\\//, '').split('/')[0] : '' }}"
            }
          ]
        },
        "options": {
          "timeout": 15000
        }
      },
      "id": "apollo-enrich",
      "name": "Apollo.io Enrichment",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        700,
        200
      ],
      "continueOnFail": true
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE leads\nSET email = '{{ $json.organization?.primary_email || null }}',\n    employees_range = '{{ $json.organization?.estimated_num_employees || null }}',\n    score = CASE WHEN '{{ $json.organization?.primary_email }}' IS NOT NULL THEN score + 20 ELSE score END,\n    updated_at = NOW()\nWHERE id = {{ $('Fetch DE Leads (no email)').item.json.id }}\nRETURNING id, company_name, email",
        "additionalFields": {}
      },
      "id": "update-lead-email",
      "name": "Update Lead Email",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        900,
        200
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      },
      "continueOnFail": true
    },
    {
      "parameters": {
        "conditions": {
          "conditions": [
            {
              "leftValue": "={{ $json.email }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty"
              }
            }
          ]
        }
      },
      "id": "has-email",
      "name": "Got email?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1100,
        200
      ]
    },
    {
      "parameters": {
        "fromEmail": "arda@ajanservis.com",
        "toEmail": "={{ $json.email }}",
        "subject": "=KI-Automatisierung f\u00fcr {{ $json.company_name }}?",
        "emailType": "html",
        "message": "=<html><body style=\"font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;\">\n<p>Hallo,</p>\n\n<p>ich bin Arda von <strong>Ajanservis.com</strong>. Wir helfen kleinen und mittelst\u00e4ndischen Unternehmen in Deutschland, ihren Kundenservice mit KI zu automatisieren \u2013 ohne teures Personal zu ben\u00f6tigen.</p>\n\n<p><strong>Owly</strong> \u2013 unser WhatsApp-KI-Agent \u2013 beantwortet Kundenanfragen in Sekunden, rund um die Uhr:</p>\n<ul>\n<li>\u2705 Automatische Antworten auf h\u00e4ufige Fragen</li>\n<li>\u2705 Termin- und Buchungsmanagement</li>\n<li>\u2705 Nahtlose \u00dcbergabe an menschliche Mitarbeiter</li>\n</ul>\n\n<p>F\u00fcr ein Unternehmen wie <strong>{{ $json.company_name }}</strong> k\u00f6nnte das 40% weniger Support-Aufwand bedeuten.</p>\n\n<p>H\u00e4tten Sie <strong>15 Minuten</strong> f\u00fcr eine kurze Demo?</p>\n\n<p>\ud83d\udc49 <a href=\"https://ajanservis.com/demo\">Demo buchen (kostenlos)</a></p>\n\n<p>Mit freundlichen Gr\u00fc\u00dfen,<br>\n<strong>Arda</strong> | Ajanservis.com<br>\n<a href=\"https://ajanservis.com\">ajanservis.com</a></p>\n</body></html>",
        "options": {}
      },
      "id": "send-email",
      "name": "Send DE Outreach Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        1300,
        200
      ],
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "continueOnFail": true
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE leads\nSET stage = 'Contacted',\n    last_contact_at = NOW(),\n    score = score + 10,\n    updated_at = NOW()\nWHERE id = {{ $('Update Lead Email').item.json.id }}\nRETURNING id, company_name, stage",
        "additionalFields": {}
      },
      "id": "update-stage",
      "name": "Mark as Contacted",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        1500,
        200
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "amount": 3,
        "unit": "days"
      },
      "id": "wait-3days",
      "name": "Wait 3 Days",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1700,
        200
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT id, company_name, email, last_contact_at\nFROM leads\nWHERE stage = 'Contacted'\n  AND country = 'DE'\n  AND last_contact_at < NOW() - INTERVAL '3 days'\n  AND last_contact_at > NOW() - INTERVAL '4 days'\nLIMIT 5",
        "additionalFields": {}
      },
      "id": "check-no-reply",
      "name": "Check No-Reply (3d)",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        1900,
        200
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "fromEmail": "arda@ajanservis.com",
        "toEmail": "={{ $json.email }}",
        "subject": "=Re: KI-Automatisierung f\u00fcr {{ $json.company_name }}",
        "emailType": "text",
        "message": "=Hallo,\n\nnur kurz nachfassen \u2013 haben Sie meine E-Mail von letzter Woche gesehen?\n\nIch w\u00fcrde gerne 15 Minuten mit Ihnen \u00fcber die M\u00f6glichkeiten der KI-Automatisierung f\u00fcr {{ $json.company_name }} sprechen.\n\n\ud83d\udc49 Demo buchen: https://ajanservis.com/demo\n\nMit freundlichen Gr\u00fc\u00dfen,\nArda | Ajanservis.com",
        "options": {}
      },
      "id": "followup-email",
      "name": "Follow-up Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        2100,
        200
      ],
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "continueOnFail": true
    }
  ],
  "connections": {
    "Hourly Check": {
      "main": [
        [
          {
            "node": "Fetch DE Leads (no email)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch DE Leads (no email)": {
      "main": [
        [
          {
            "node": "Any leads?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Any leads?": {
      "main": [
        [
          {
            "node": "Apollo.io Enrichment",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Apollo.io Enrichment": {
      "main": [
        [
          {
            "node": "Update Lead Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Lead Email": {
      "main": [
        [
          {
            "node": "Got email?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Got email?": {
      "main": [
        [
          {
            "node": "Send DE Outreach Email",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Send DE Outreach Email": {
      "main": [
        [
          {
            "node": "Mark as Contacted",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mark as Contacted": {
      "main": [
        [
          {
            "node": "Wait 3 Days",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 3 Days": {
      "main": [
        [
          {
            "node": "Check No-Reply (3d)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check No-Reply (3d)": {
      "main": [
        [
          {
            "node": "Follow-up Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveExecutionProgress": true
  },
  "tags": [
    "revenue",
    "lead-gen",
    "email-outreach",
    "phase-08"
  ],
  "notes": "ARMANUAL: 1) Add owly-postgres credential. 2) Add Gmail SMTP credential. 3) Set n8n variable APOLLO_API_KEY=your_key. Apollo free tier: 50 enrichments/month. Use $vars.APOLLO_API_KEY in n8n variables (Settings > Variables)."
}