Skip to content

Python (httpx / requests)

View as Markdown

No Syvel package needed — only httpx or requests.

With httpx

import os
import httpx
def check_email(email: str) -> dict | None:
"""
Returns the Syvel check result, or None if the API is unavailable.
Always fail open: return None and let the user through.
"""
try:
response = httpx.get(
f"https://api.syvel.io/v1/check/{email}",
headers={"Authorization": f"Bearer {os.environ['SYVEL_API_KEY']}"},
timeout=3.0, # 3 s max — never block the user
)
if not response.is_success:
return None # quota exceeded or server error → fail open
return response.json()
except Exception:
return None # network error or timeout → fail open
# In a Django / FastAPI / Flask view:
result = check_email(email)
if result and result.get("is_risky"):
raise ValidationError("Please use a professional email address.")
# result is None → Syvel unavailable, let the user through

Django REST Framework

from rest_framework import serializers
import os
import httpx
class RegisterSerializer(serializers.Serializer):
email = serializers.EmailField()
def validate_email(self, value):
try:
res = httpx.get(
f"https://api.syvel.io/v1/check/{value}",
headers={"Authorization": f"Bearer {os.environ['SYVEL_API_KEY']}"},
timeout=3.0,
)
if res.is_success and res.json().get("is_risky"):
raise serializers.ValidationError(
"Please use a professional email address."
)
except httpx.RequestError:
pass # fail open — Syvel unavailable, let the user through
return value

FastAPI (async)

from fastapi import FastAPI, HTTPException
import os
import httpx
app = FastAPI()
async def check_email(email: str) -> dict | None:
try:
async with httpx.AsyncClient() as client:
res = await client.get(
f"https://api.syvel.io/v1/check/{email}",
headers={"Authorization": f"Bearer {os.environ['SYVEL_API_KEY']}"},
timeout=3.0,
)
if not res.is_success:
return None
return res.json()
except Exception:
return None
@app.post("/register")
async def register(email: str):
result = await check_email(email)
if result and result.get("is_risky"):
raise HTTPException(
status_code=422,
detail="Please use a professional email address.",
)
# continue with registration…

With requests

import os
import requests
def check_email(email: str) -> dict | None:
try:
response = requests.get(
f"https://api.syvel.io/v1/check/{email}",
headers={"Authorization": f"Bearer {os.environ['SYVEL_API_KEY']}"},
timeout=3,
)
if not response.ok:
return None
return response.json()
except Exception:
return None

Resources

Last updated: