How to Automate Lead Generation and Scoring with AI

Introduction

Sales teams spend hours manually finding and qualifying leads from LinkedIn, Apollo.io, or email lists. This process is slow, inconsistent, and often biased by human judgment. AI can automate these repetitive steps by gathering prospects, enriching them with company data, and scoring them based on conversion likelihood before pushing them into your CRM.

In this guide, you will learn how to build a complete AI-assisted lead generation and scoring workflow using Python, Airflow (or Make.com), and APIs from tools such as Apollo.io, LinkedIn, and HubSpot.

What You Will Build

An automated pipeline that

  • Pulls or scrapes new leads from LinkedIn or Apollo.io
  • Enriches data with firmographic information such as company size, funding, and location
  • Scores each lead using an AI model that predicts conversion potential
  • Sends qualified leads to your CRM automatically
  • Continuously learns from new outcomes to improve scoring accuracy

Architecture Overview

Step 1: Collect Leads from Source

The first step is to pull raw leads from your chosen source. If you are using Apollo.io, you can export data through their API. For LinkedIn, you can use their official API or a compliant third-party data provider.

Example with Apollo.io API:

import requests
import pandas as pd

api_key = "YOUR_APOLLO_API_KEY"
url = "https://api.apollo.io/v1/mixed_people/search"

payload = {
    "api_key": api_key,
    "page": 1,
    "person_titles": ["CTO", "Head of Engineering"],
    "person_locations": ["United States"]
}

response = requests.post(url, json=payload)
data = response.json()

leads = []
for p in data["people"]:
    leads.append({
        "name": p["name"],
        "title": p["title"],
        "company": p["organization"]["name"],
        "email": p["email"],
        "linkedin": p["linkedin_url"]
    })

df = pd.DataFrame(leads)
df.to_csv("leads_raw.csv", index=False)

Step 2: Enrich Leads with Firmographic Data

To improve scoring accuracy, enrich each lead with attributes like industry, number of employees, and funding information. Use APIs such as Clearbit, Crunchbase, or Apollo’s own enrichment endpoint.

def enrich_lead(lead):
    response = requests.get(f"https://api.clearbit.com/v2/companies/find?domain={lead['company']}.com",
                            headers={"Authorization": "Bearer YOUR_CLEARBIT_KEY"})
    data = response.json()
    lead["industry"] = data.get("category", {}).get("sector", "")
    lead["employees"] = data.get("metrics", {}).get("employees", 0)
    lead["funding"] = data.get("metrics", {}).get("raised", 0)
    return lead

Enrichment ensures that leads with real growth potential are prioritized before AI scoring begins.

Step 3: Score Leads Using an AI Model

Once leads are enriched, use an AI model to predict conversion potential. This can be done via OpenAI’s GPT-4-turbo, Claude, or a local LLM hosted through Ollama.

Example using OpenAI API:

import openai, json, os

openai.api_key = os.getenv("OPENAI_API_KEY")

def score_lead(lead):
    prompt = f"""
    You are a B2B sales assistant. Evaluate this lead for potential conversion.
    Provide a score from 0 to 100 and reasoning.

    Lead:
    {json.dumps(lead, indent=2)}
    """
    response = openai.ChatCompletion.create(
        model="gpt-4-turbo",
        messages=[{"role": "user", "content": prompt}],
        temperature=0
    )
    content = response.choices[0].message["content"]
    return content

For a fully local setup, run a lightweight model like Mistral via Ollama or use a fine-tuned regression model trained on your historical CRM data.

Step 4: Push Qualified Leads to CRM Automatically

When a lead passes your score threshold, automatically add it to HubSpot or your preferred CRM.

def push_to_hubspot(lead, score):
    if score < 70:
        return
    payload = {
        "properties": {
            "firstname": lead["name"],
            "email": lead["email"],
            "company": lead["company"],
            "jobtitle": lead["title"],
            "lead_score": score
        }
    }
    headers = {
        "Authorization": "Bearer YOUR_HUBSPOT_API_KEY",
        "Content-Type": "application/json"
    }
    requests.post("https://api.hubapi.com/crm/v3/objects/contacts", json=payload, headers=headers)

Integrating with HubSpot ensures all high-value leads flow directly into your pipeline for follow-up.

Step 5: Automate the Pipeline

You can orchestrate this workflow using Airflow, cron jobs, or Make.com.
Example Airflow DAG snippet:

from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import datetime

def pipeline():
    # steps: collect → enrich → score → push
    pass

with DAG("ai_lead_scoring", start_date=datetime(2025, 1, 1), schedule="@daily", catchup=False) as dag:
    run_pipeline = PythonOperator(task_id="run_pipeline", python_callable=pipeline)

This automation runs daily or hourly, depending on your lead generation frequency.

Step 6: Continuous Improvement

Once your pipeline runs regularly, gather feedback from your CRM to see which leads converted.
Use that data to fine-tune your scoring model. You can retrain a simple regression model or prompt the LLM with success ratios for better accuracy over time.

  • Add labels such as “won,” “lost,” or “unresponsive.”
  • Analyze model outputs against actual outcomes.
  • Adjust scoring thresholds dynamically based on performance metrics.

Security and Compliance

  • Always comply with LinkedIn and Apollo’s data use policies.
  • Store lead data securely and anonymize sensitive fields when retraining models.
  • Encrypt API keys and environment variables.
  • Respect GDPR and data privacy laws for all European leads.

References and Resources

Conclusion

By automating lead generation and scoring with AI, your sales team gains time to focus on closing deals instead of filtering noise. This system ensures every prospect entering your CRM is pre-qualified with consistent criteria and scored based on real conversion potential. Over time, it continuously improves, helping your sales organization operate at scale with minimal manual input.

Ivan Dabić

A man with a beard and glasses, wearing an orange hoodie and a black cap with a Hard Rock Cafe logo, stands with his arms crossed against a plain white background.

Ivan Dabić

Co-founder and CEO of BlueGrid.io, with a background in cloud infrastructure, distributed systems, monitoring, and security operations. He works closely with engineering teams to build and operate reliable systems while documenting both technical and organizational aspects of modern engineering work.

Ivan is a metalhead, and big fan of cyberpunk move genre. If you are his secret Santa go with Star Wars Lego box!

Share this post

Share this link via

Or copy link