Skip to main content

Python client for the Data Legion API

Project description

Data Legion

Data Legion Python SDK

Tests   License: MIT   Python

The official Python client for the Data Legion API.

Installation

pip install datalegion

Quick Start

from datalegion import DataLegion

client = DataLegion(api_key="legion_...")

Or set the DATALEGION_API_KEY environment variable and omit the api_key argument:

client = DataLegion()

Person Enrichment

Look up a person by email, phone, social URL, name, or other identifiers. Returns a PersonResponse with full type hints and dot access.

person = client.person.enrich(email="john@example.com")
print(person.full_name)
print(person.job_title)
print(person.company_name)

# Multiple results
results = client.person.enrich(
    email="john@example.com",
    multiple_results=True,
    limit=5,
)
for match in results.matches:
    print(match.person.full_name, match.match_metadata.match_confidence)

# Enrich by name + company
person = client.person.enrich(
    first_name="John",
    last_name="Doe",
    company="Google",
)

# Enrich by LinkedIn URL
person = client.person.enrich(social_url="https://linkedin.com/in/johndoe")

Person Search

Search for people using SQL queries.

results = client.person.search(
    query="SELECT * FROM people WHERE company_name ILIKE '%google%' AND city = 'San Francisco'",
    limit=10,
)
for match in results.matches:
    print(match.person.full_name)

Person Discover

Search for people using natural language.

results = client.person.discover(
    query="engineers in San Francisco who worked at Google",
    limit=10,
)
for match in results.matches:
    print(match.person.full_name)

Company Enrichment

Look up a company by domain, name, LinkedIn ID, or ticker symbol. Returns a CompanyResponse with dot access.

# By domain
company = client.company.enrich(domain="google.com")
print(company.name.cleaned)
print(company.industry)
print(company.legion_employee_count)

# By name
company = client.company.enrich(name="Google")

# Multiple results
results = client.company.enrich(name="Apple", multiple_results=True, limit=3)
for match in results.matches:
    print(match.company.name.cleaned)

Company Search

Search for companies using SQL queries.

results = client.company.search(
    query="SELECT * FROM companies WHERE industry = 'software development'",
    limit=10,
)
for match in results.matches:
    print(match.company.name.cleaned)

Company Discover

Search for companies using natural language.

results = client.company.discover(
    query="AI companies with more than 100 employees",
    limit=10,
)
for match in results.matches:
    print(match.company.name.cleaned)

Utilities

Clean & Normalize Fields

cleaned = client.utility.clean(
    fields={
        "email": "John.Doe+tag@gmail.com",
        "phone": "(555) 123-4567",
        "domain": "https://www.Google.com/about",
    }
)
for field, result in cleaned.results.items():
    print(f"{field}: {result.original} -> {result.cleaned}")

Hash Email

hashed = client.utility.hash_email(email="john@example.com")
print(hashed.hashes["sha256"])
print(hashed.hashes["md5"])

Validate Data

validated = client.utility.validate(
    email="john@example.com",
    phone="+15551234567",
    first_name="John",
)
print(validated.valid)
for error in validated.errors:
    print(f"{error.field}: {error.error}")

Report a Data Issue

Report an inaccuracy in a person or company record against its legion_id. Free; see the docs for the issue_type/issue_level rules.

report = client.utility.report_person(
    legion_id="abc123",
    issue_type="incorrect",
    issue_level="field",
    field="job_title",
    observed_value="CEO",
    correct_value="CTO",
)
print(report.report_id, report.status)

Company records use client.utility.report_company() with the same parameters.

Health Check

health = client.health()
print(health.status)

Async Usage

import asyncio
from datalegion import AsyncDataLegion

async def main():
    client = AsyncDataLegion(api_key="legion_...")

    person = await client.person.enrich(email="john@example.com")
    print(person.full_name)

    company = await client.company.enrich(domain="google.com")
    print(company.name.cleaned)

    await client.close()

asyncio.run(main())

Or use as a context manager:

async with AsyncDataLegion(api_key="legion_...") as client:
    person = await client.person.enrich(email="john@example.com")

Response Types

All responses are Pydantic models with full type hints and autocomplete support. Import them directly:

from datalegion import PersonResponse, CompanyResponse, PersonMatchesResponse

Response Headers

After each request, these attributes are updated on the client:

person = client.person.enrich(email="john@example.com")
print(client.request_id)             # unique request ID
print(client.credits_used)           # credits consumed
print(client.credits_remaining)      # credits remaining
print(client.contract_id)            # contract ID
print(client.rate_limit_limit)       # requests quota in current window
print(client.rate_limit_remaining)   # remaining requests in current window
print(client.rate_limit_reset)       # unix timestamp when window resets
print(client.rate_limit_policy)      # rate limit policy (e.g. "100/min")
print(client.retry_after)            # seconds to wait (on 429 responses)
print(client.generated_query)        # SQL from discover endpoints
print(client.correlation_id)         # echoed Correlation-ID

Error Handling

from datalegion import (
    DataLegion,
    AuthenticationError,
    InsufficientCreditsError,
    RateLimitError,
    ValidationError,
    APIError,
)

client = DataLegion(api_key="legion_...")

try:
    person = client.person.enrich(email="john@example.com")
except AuthenticationError as e:
    print(f"Invalid API key: {e.message}")
except InsufficientCreditsError as e:
    print(f"Out of credits: {e.message}")
except RateLimitError as e:
    print(f"Rate limited: {e.message}")
except ValidationError as e:
    print(f"Invalid request: {e.message}, details: {e.details}")
except APIError as e:
    print(f"Server error ({e.status_code}): {e.message}")

All exceptions inherit from DataLegionError and include these attributes:

  • message - Human-readable error message
  • status_code - HTTP status code
  • error - Error type/code from the API
  • details - Additional error details (if any)

Configuration

Parameter Default Description
api_key DATALEGION_API_KEY env var Your API key
base_url https://api.datalegion.ai API base URL
timeout 60.0 Request timeout in seconds
httpx_client None Custom httpx.Client / httpx.AsyncClient

Documentation

For full API documentation, visit https://www.datalegion.ai/docs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datalegion-0.4.0.tar.gz (49.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

datalegion-0.4.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file datalegion-0.4.0.tar.gz.

File metadata

  • Download URL: datalegion-0.4.0.tar.gz
  • Upload date:
  • Size: 49.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for datalegion-0.4.0.tar.gz
Algorithm Hash digest
SHA256 ed488aee847a6e9298c6b313f9231eefce33dbe2a74baac2d8870befe447fc66
MD5 d6303a34de948110d8840f78f0069909
BLAKE2b-256 21acc5d2e1e736402e0a3b0c2f1a6b0e017b3818b61a1197dc997647dc528fe9

See more details on using hashes here.

File details

Details for the file datalegion-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: datalegion-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for datalegion-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2ef368d4f1656718a2024d51b79266acbbcb71a4e14738023e95f7da992c9249
MD5 a1b787b0ff199fda59599c122c6e3087
BLAKE2b-256 6f29372e4d1192fe11caf5aa6f14114b407c77b6c469360f6a85df9e6d0b2099

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page