-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathenrichr.py
More file actions
52 lines (41 loc) · 1.53 KB
/
enrichr.py
File metadata and controls
52 lines (41 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Enrichr — email validation utility
Blocks disposable addresses before they hit your database.
Setup: add ENRICHR_API_KEY to your .env
Get a free key at https://enrichrapi.dev (1,000 calls/month free)
"""
import os
from typing import Any
import httpx
_BASE = os.getenv("ENRICHR_BASE_URL", "https://enrichrapi.dev")
async def validate_email(email: str) -> dict[str, Any] | None:
"""
Validate an email address via Enrichr.
Returns None if ENRICHR_API_KEY is not set (graceful degradation).
Returns None on any network error so signup is never blocked by a failed API call.
"""
key = os.getenv("ENRICHR_API_KEY")
if not key:
return None
try:
async with httpx.AsyncClient(timeout=5) as client:
resp = await client.post(
f"{_BASE}/v1/enrich/email",
headers={"X-Api-Key": key},
json={"email": email},
)
if not resp.is_success:
return None
return resp.json().get("data")
except Exception:
return None
async def is_disposable_email(email: str) -> bool:
"""
Returns True if the email is from a known disposable/throwaway provider.
Safe to call in API routes — returns False on any network error.
Usage:
if await is_disposable_email(form_data.username):
raise HTTPException(status_code=422, detail="Disposable email addresses are not allowed.")
"""
result = await validate_email(email)
return result.get("disposable", False) if result else False