DEV Community

Habib Benabdeslam
Habib Benabdeslam

Posted on

Find work emails for a CSV of leads with Python

You have a CSV with names and companies, and you need work emails. Standard email finders usually stop when their own lookup fails. A waterfall tries several lookup services in sequence and only stops when one finds the email.

This tutorial reads your CSV, sends the records to Datablist's Waterfall Email Finder API, waits for the task to finish, and writes the emails back to a new CSV. About 60 lines of Python, using only requests.

What you need

first_name,last_name,company
Jane,Doe,example.com
Satya,Nadella,Microsoft
Enter fullscreen mode Exit fullscreen mode

A company domain gives better results than a company name, but both work.

Cost: 25 credits ($0.025) per email found. Rows where no email is found cost nothing.

1. Submit the rows as one task

The API is asynchronous. You send up to 10,000 people in one task and get a task id back.

import csv, os, time, uuid
import requests

API = "https://api.datablist.com/v1/enrichments/tasks"
HEADERS = {"X-API-Key": os.environ["DATABLIST_API_KEY"]}

def submit(rows):
    body = {
        "enrichmentId": "waterfall-email-finder",
        "uniqueId": f"csv-{uuid.uuid4()}",  # new id for every task
        "settings": {"sourceKind": "name_company"},
        "inputs": [
            {
                "inputId": f"row-{i}",  # lets us match results to rows
                "data": {
                    "firstName": row["first_name"],
                    "lastName": row["last_name"],
                    "companyNameOrDomain": row["company"],
                },
            }
            for i, row in enumerate(rows)
        ],
    }
    r = requests.post(API, json=body, headers=HEADERS, timeout=30)
    r.raise_for_status()
    return r.json()["@id"]
Enter fullscreen mode Exit fullscreen mode

The inputId matters: two people can share a name, so match results by id, not by name.

2. Wait for the task to finish

def wait(task_id, every=5):
    while True:
        r = requests.get(f"{API}/{task_id}", headers=HEADERS, timeout=30)
        r.raise_for_status()
        status = r.json()["status"]
        if status in ("completed", "failed", "stopped"):
            return status
        time.sleep(every)
Enter fullscreen mode Exit fullscreen mode

3. Read every page of results

Results come back in pages. Follow nextCursor until it is null.

def results(task_id):
    cursor = None
    while True:
        params = {"cursor": cursor} if cursor else {}
        r = requests.get(f"{API}/{task_id}/results", headers=HEADERS,
                         params=params, timeout=30)
        r.raise_for_status()
        page = r.json()
        yield from page["results"]
        cursor = page.get("nextCursor")
        if not cursor:
            return
Enter fullscreen mode Exit fullscreen mode

4. Put it together

def main(src="leads.csv", dst="leads_with_emails.csv"):
    with open(src, newline="", encoding="utf-8") as f:
        rows = list(csv.DictReader(f))

    task_id = submit(rows)
    print("task", task_id, wait(task_id))

    by_id = {res["inputId"]: res for res in results(task_id)}
    for i, row in enumerate(rows):
        out = (by_id.get(f"row-{i}") or {}).get("outputs") or {}
        row["email"] = out.get("emailFound", "")
        row["email_status"] = out.get("emailStatus", "")

    with open(dst, "w", newline="", encoding="utf-8") as f:
        w = csv.DictWriter(f, fieldnames=list(rows[0].keys()))
        w.writeheader()
        w.writerows(rows)

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

Reading the results

Field What it tells you
emailFound The work email
emailStatus verified, unverified, not_found, missing_source_data or invalid_data
mxProvider Who hosts the mailbox, for example google or microsoft

Only send to verified addresses if you care about bounce rates. Treat unverified as risky (often catch-all domains).

Variations

  • LinkedIn URLs instead of names: set sourceKind to "linkedin" and send {"linkedInProfile": "https://www.linkedin.com/in/..."}.
  • Full names in one column: add "useFullName": true to settings and send fullName.
  • Your own provider keys: set useCustomAPIKeys and a customSequence. The result then also tells you which provider found each email.

Complete Python and TypeScript examples: github.com/datablist/waterfall-email-finder-api. API docs: datablist.com/apis/waterfall-email-finder.

Top comments (0)