Clerk · TypeScript

Block disposable email in Clerk

Use a Clerk webhook on user.created or validate in your custom signup API before calling Clerk. Two patterns depending on when you want to block.

The code

// app/api/webhooks/clerk/route.ts
import { Webhook } from "svix";
import { headers } from "next/headers";
import { NextResponse } from "next/server";

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

export async function POST(req: Request) {
  const payload = await req.text();
  const wh = new Webhook(process.env.CLERK_WEBHOOK_SECRET!);
  const evt = wh.verify(payload, Object.fromEntries(await headers())) as any;

  if (evt.type === "user.created" && evt.data.email_addresses?.[0]?.email_address) {
    const email = evt.data.email_addresses[0].email_address;
    const r = await fetch(`${CHECK_URL}/${encodeURIComponent(email)}`, {
      headers: {
        Authorization: `Bearer ${process.env.PIISEND_API_KEY!}`,
      },
    });
    // reject on 'block' — catches disposable, spam, no-MX and typo'd addresses
    if (r.ok && (await r.json()).recommendation === 'block') {
      // Delete user or flag — prefer blocking before create when possible
      await fetch(`https://api.clerk.com/v1/users/${evt.data.id}`, {
        method: "DELETE",
        headers: { Authorization: `Bearer ${process.env.CLERK_SECRET_KEY!}` },
      });
    }
  }

  return NextResponse.json({ ok: true });
}

API key

Use separate secrets for Clerk webhook verification and Piisend API key.

Fail-open vs fail-closed

Webhook pattern is async — user may exist briefly. Prefer server-side check before `clerkClient.users.createUser` when you control signup.

Pair with verification

For custom flows, call Piisend in your signup Route Handler before any Clerk API call.

FAQ

Where does the Piisend API key go in Clerk?

Use separate secrets for Clerk webhook verification and Piisend API key.

What happens if Piisend is slow or down?

Webhook pattern is async — user may exist briefly. Prefer server-side check before `clerkClient.users.createUser` when you control signup.

How does this fit with the rest of Clerk signup?

For custom flows, call Piisend in your signup Route Handler before any Clerk API call.

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.