Next.js Integration

Block disposable emails in your Next.js app using server actions, API routes, or middleware.

Server Action

The simplest approach. Add a check in your signup server action:

app/actions/signup.tstypescript
'use server';

import { isDisposable } from '@isdisposable/js';

export async function signup(formData: FormData) {
  const email = formData.get('email') as string;

  if (isDisposable(email)) {
    return { error: 'Please use a real email address.' };
  }

  // Continue with signup...
  // await createUser(email, ...)
}
app/signup/page.tsxtypescript
'use client';

import { signup } from '@/app/actions/signup';

export function SignupForm() {
  async function handleSubmit(formData: FormData) {
    const result = await signup(formData);
    if (result?.error) {
      alert(result.error);
    }
  }

  return (
    <form action={handleSubmit}>
      <input name="email" type="email" placeholder="Email" required />
      <button type="submit">Sign Up</button>
    </form>
  );
}

API Route

If you prefer API routes:

app/api/signup/route.tstypescript
import { NextRequest, NextResponse } from 'next/server';
import { isDisposable } from '@isdisposable/js';

export async function POST(request: NextRequest) {
  const { email } = await request.json();

  if (isDisposable(email)) {
    return NextResponse.json(
      { error: 'Disposable emails are not allowed.' },
      { status: 400 }
    );
  }

  // Create user...
  return NextResponse.json({ success: true });
}

Middleware

Protect all form submissions at the edge. This runs before your route handlers.

middleware.tstypescript
import { NextRequest, NextResponse } from 'next/server';
import { isDisposable } from '@isdisposable/js';

export async function middleware(request: NextRequest) {
  // Only check POST requests to signup routes
  if (request.method === 'POST' && request.nextUrl.pathname === '/api/signup') {
    const body = await request.clone().json();

    if (body.email && isDisposable(body.email)) {
      return NextResponse.json(
        { error: 'Please use a real email address.' },
        { status: 400 }
      );
    }
  }

  return NextResponse.next();
}

export const config = {
  matcher: '/api/signup',
};

Edge runtime

The isDisposable package works in Next.js middleware (edge runtime). The bundled domain list is loaded at build time, so there are no filesystem reads at runtime.

With Pro API

For real-time detection of new disposable services, use the API client in a server action:

app/actions/signup.tstypescript
'use server';

import { createIsDisposable } from '@isdisposable/js';

const checker = createIsDisposable({
  apiKey: process.env.ISDISPOSABLE_API_KEY!,
});

export async function signup(formData: FormData) {
  const email = formData.get('email') as string;

  const result = await checker.check(email);

  if (result.disposable) {
    return {
      error: 'Please use a real email address.',
      score: result.score,
      reason: result.reason,
    };
  }

  // Safe to create user
}

Fallback strategy

The API client automatically falls back to offline detection if the API is unreachable. Your signup flow will never break even if the API is down. See Error Handling for details.