FastAPI · Python

Block disposable email in FastAPI

Piisend's own stack. Add a reusable dependency or service call before your signup endpoint creates a user.

The code

# services/email_check.py
import os
import httpx

CHECK_URL = "https://api.piisend.com/api/v1/check/email"

async def should_block_email(email: str) -> bool:
    try:
        async with httpx.AsyncClient(timeout=5.0) as client:
            r = await client.get(
                f"{CHECK_URL}/{email}",
                headers={
                    "Authorization": f"Bearer {os.environ['PIISEND_API_KEY']}",
                },
            )
        if not r.is_success:
            return False
        # reject on 'block' — catches disposable, spam, no-MX and typo'd addresses
        return r.json().get("recommendation") == "block"
    except httpx.HTTPError:
        return False

# routes/auth.py
from fastapi import APIRouter, HTTPException
from .services.email_check import should_block_email

router = APIRouter()

@router.post("/signup")
async def signup(email: str, password: str):
    if await should_block_email(email):
        raise HTTPException(400, "Please use a permanent email address.")
    # ...create user

API key

Load `PIISEND_API_KEY` from settings, not hard-coded strings.

Fail-open vs fail-closed

httpx errors return False — log if you need observability.

Pair with verification

Piisend itself uses a vendored blocklist locally; this pattern is for your separate app calling Piisend intelligence.

FAQ

Where does the Piisend API key go in FastAPI?

Load `PIISEND_API_KEY` from settings, not hard-coded strings.

What happens if Piisend is slow or down?

httpx errors return False — log if you need observability.

How does this fit with the rest of FastAPI signup?

Piisend itself uses a vendored blocklist locally; this pattern is for your separate app calling Piisend intelligence.

What does this snippet block?

Addresses on disposable domains: about 10,900 listed domains, plus new ones whose mail servers belong to a known throwaway service. Free providers such as Gmail and forwarding aliases such as duck.com pass.

Can I get a full allow / review / block verdict instead?

Yes, and we recommend it. Call GET /api/v1/check/email/{email} instead. Both endpoints need an API key with the check:read scope and use one check from your monthly allowance per call, but the check endpoint also returns forwarding relays, phishing lists, missing MX records, new domains, typo suggestions and an allow / review / block verdict.

Send verification email with Piisend

After blocking disposable addresses, send OTP and verification mail through the same Piisend account. One API key for intelligence checks and transactional sends.