Next.js · TypeScript

Block disposable email in Next.js

Drop a single Route Handler into your Next.js 15 / App Router project. Calls Piisend server-side so the API key never reaches the browser. Works with any auth library.

The code

// app/api/signup/route.ts
import { NextResponse } from 'next/server';

const CHECK_URL = 'https://api.piisend.com/api/v1/check/email';

async function shouldBlock(email: string): Promise<boolean> {
  try {
    const r = await fetch(`${CHECK_URL}/${encodeURIComponent(email)}`, {
      headers: {
        Authorization: `Bearer ${process.env.PIISEND_API_KEY!}`,
      },
      cache: 'no-store',
    });
    if (!r.ok) return false; // fail open
    const data = await r.json();
    // reject on 'block' — catches disposable, spam, no-MX and typo'd addresses
    return data.recommendation === 'block';
  } catch {
    return false; // fail open on network / timeout
  }
}

export async function POST(req: Request) {
  const { email, password } = await req.json();

  if (await shouldBlock(email)) {
    return NextResponse.json(
      { error: 'Please use a permanent email address.' },
      { status: 400 },
    );
  }

  // ...your existing signup logic
  return NextResponse.json({ ok: true });
}

API key

Add `PIISEND_API_KEY=pii_live_...` to `.env.local` and `.env.example`. Never expose the key in client components — keep checks in Route Handlers or Server Actions only.

Fail-open vs fail-closed

The example fails open (allows signup when Piisend is unreachable). Right for most SaaS. For high-fraud verticals, fail closed and show a temporary error instead.

Pair with verification

Disposable detection runs before you send a verification email via Piisend. Combine with double opt-in so anything that slips through still cannot activate without inbox access.

FAQ

Where does the Piisend API key go in Next.js?

Add `PIISEND_API_KEY=pii_live_...` to `.env.local` and `.env.example`. Never expose the key in client components — keep checks in Route Handlers or Server Actions only.

What happens if Piisend is slow or down?

The example fails open (allows signup when Piisend is unreachable). Right for most SaaS. For high-fraud verticals, fail closed and show a temporary error instead.

How does this fit with the rest of Next.js signup?

Disposable detection runs before you send a verification email via Piisend. Combine with double opt-in so anything that slips through still cannot activate without inbox access.

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.