Express · TypeScript
Block disposable email in Express
Express middleware calls Piisend on POST /signup (or your auth route) before passport or custom user creation runs.
The code
// middleware/disposableEmail.ts
import type { Request, Response, NextFunction } from "express";
const CHECK_URL = "https://api.piisend.com/api/v1/check/email";
export async function blockDisposableEmail(req: Request, res: Response, next: NextFunction) {
const email = req.body?.email;
if (!email) return next();
try {
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') {
return res.status(400).json({ error: "Please use a permanent email address." });
}
} catch {
// fail open
}
next();
}
// app.ts
import express from "express";
import { blockDisposableEmail } from "./middleware/disposableEmail";
const app = express();
app.use(express.json());
app.post("/signup", blockDisposableEmail, signupHandler);API key
Use `dotenv` in development; inject `PIISEND_API_KEY` via your host's secret store in production.
Fail-open vs fail-closed
Middleware calls `next()` on errors — swap for 503 if you need fail-closed.
Pair with verification
Place middleware before passport-local or your ORM user-create handler.
FAQ
Where does the Piisend API key go in Express?
Use `dotenv` in development; inject `PIISEND_API_KEY` via your host's secret store in production.
What happens if Piisend is slow or down?
Middleware calls `next()` on errors — swap for 503 if you need fail-closed.
How does this fit with the rest of Express signup?
Place middleware before passport-local or your ORM user-create handler.
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.