1. Architecture: the API key stays on the server
The most common integration mistake is calling the verification API straight from the browser. Anyone can read an API key placed in frontend code and then use up your credits. The correct flow is:
- The browser sends the address to your backend (e.g. a Route Handler in Next.js).
- The backend, with the key in an environment variable, calls
POST /api/v1/verify. - The backend turns the result into a simple decision for the form: accept, block or suggest a correction.
This way the key never leaves the server, and you control who can use verification and how often.
Your endpoint needs protecting too
A public endpoint that verifies any address can be used to check other people's lists at your expense. Rate-limit requests per IP address and session, and add a CAPTCHA to sign-up (e.g. Cloudflare Turnstile).
2. Calling the API
The request takes a single email field, and the response contains the status, a reason code, flags and the details of each check:
curl -X POST https://api.mailspecto.com/api/v1/verify \
-H "Authorization: Bearer $MAILSPECTO_API_KEY" \
-H "Content-Type: application/json" \
-d '{"email": "[email protected]"}'The most important response fields:
| Field | What it is for |
|---|---|
status | The main decision: mailbox_exists, catch_all, domain_accepts_mail, risky, invalid, unknown |
reason_code | The specific reason, e.g. possible_typo, disposable_domain, mailbox_not_found |
flags | Quick flags: is_disposable, is_role_account, has_typo_suggestion and others |
suggested_correction | The suggested correct address, e.g. [email protected] |
mailbox_verified | Whether the recipient's server answered about this specific mailbox |
The full list of fields and codes is in the API documentation.
3. Example: a Next.js Route Handler
The handler below takes the address from the form, asks MailSpecto and returns a simplified decision. The key is in MAILSPECTO_API_KEY, set on the server only.
// app/api/check-email/route.ts
type Verdict = { ok: boolean; message?: string; suggestion?: string };
export async function POST(req: Request) {
const { email } = await req.json();
if (typeof email !== "string" || email.length < 3 || email.length > 254) {
return Response.json({ ok: false, message: "Please enter a valid email address." });
}
let res: Response;
try {
res = await fetch("https://api.mailspecto.com/api/v1/verify", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.MAILSPECTO_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ email }),
signal: AbortSignal.timeout(8000),
});
} catch {
return Response.json({ ok: true }); // timeout or network error: do not block the customer
}
if (!res.ok) {
const code = (await res.json().catch(() => null))?.error?.code;
console.error("mailspecto verify failed", res.status, code);
return Response.json({ ok: true }); // 402, 429, 503: accept the address, handle the alert
}
return Response.json(toVerdict(await res.json()));
}
function toVerdict(r: any): Verdict {
if (r.flags?.has_typo_suggestion && r.suggested_correction) {
return { ok: true, suggestion: r.suggested_correction };
}
if (r.status === "invalid") {
return { ok: false, message: "Mail cannot be delivered to this address." };
}
if (r.flags?.is_disposable) {
return { ok: false, message: "Please use a permanent email address, not a disposable one." };
}
return { ok: true };
}In the form, call this endpoint when the field loses focus (onBlur) and once more on submit:
// part of the form component ("use client")
async function check(email: string) {
const res = await fetch("/api/check-email", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email }),
});
const verdict = await res.json();
if (verdict.suggestion) setHint(`Did you mean ${verdict.suggestion}?`);
else setError(verdict.ok ? null : verdict.message);
}A few rules for good UX:
- Do not verify on every keystroke. It wastes credits and gives confusing messages for an unfinished address.
- Show a typo suggestion as a question with a "Fix it" button, not as an error. The user may be right.
- Remember the result for an address within the session, so you do not pay twice for the same check.
4. Mapping statuses to decisions
How strictly to treat each status depends on the form. An example setup for account sign-up:
| Status / flag | Account sign-up | Newsletter sign-up |
|---|---|---|
mailbox_exists | Accept | Accept |
catch_all, domain_accepts_mail | Accept | Accept, confirm with double opt-in |
risky + typo | Suggest a correction | Suggest a correction |
risky + disposable | Block | Block |
is_role_account flag (role address) | Accept (B2B) | Accept |
invalid | Block with a message | Block with a message |
unknown | Accept | Accept |
The "fail open" rule
If verification does not answer in time or returns an error, accept the address and log the event. An outage or exhausted credits must never block sign-ups or orders. You can monitor your balance with GET /api/v1/credits (the low_balance field).
5. Handling HTTP errors
| Code | Cause | Response |
|---|---|---|
401 | Wrong or revoked key, or a request from outside the allowed IPs | Alert the team, accept the address |
402 | No credits left | Alert, top up the balance, accept the address |
422 | Invalid input | Check what you send (only the email field) |
429 | Rate limit exceeded | Retry with increasing delay |
503 | Service temporarily unavailable | Retry shortly or accept the address |
Every error uses the same JSON envelope with code, message and request_id. Logging the request ID is worth it: it speeds up resolving an issue with support.
6. Whole lists: batch verification
To check an existing database (e.g. before a campaign), do not call /verify in a loop. Upload a CSV file as a job:
POST /api/v1/jobswith the file and the name of the column with the addresses; theIdempotency-Keyheader prevents a duplicate job when you retry.GET /api/v1/jobs/{job_id}every few seconds until the status changes tocompleted.GET /api/v1/jobs/{job_id}/exportto download a CSV with the results (optionally only a chosen status).
Addresses the recipient's server temporarily refused to check (greylisting) are retried by the job automatically. Their number is shown in progress.waiting_rows.
- Keep the API key on the server only, in an environment variable.
- Protect your own endpoint with rate limits and a CAPTCHA.
- Verify when the field loses focus and on submit, not on every character.
- Show typos as a suggestion; block disposable and invalid addresses.
- On errors and timeouts, accept the address (fail open) and log the
request_id. - Check existing lists with CSV jobs, not with a loop of single requests.
Keep your contact list clean
Verify email addresses before you send, and explore our upcoming SPF/DKIM/DMARC diagnostics module.
MailSpecto Engineering Team
We build tools for email address verification, deliverability monitoring and mail infrastructure security diagnostics for companies in Poland and Europe.