Validate emails in Next.js forms
Check addresses on signup and newsletter forms with a Route Handler, a React hook and a server action. Your API key never reaches the browser.
The browser never calls LeadMagic. It calls a route on your site, and that route calls LeadMagic with your key.
1. Add the key
Create a key in Settings → API Keys. Limit it to Email Validation, then add it to your server environment:
2. Add the route
The route:
- Answers same-origin JSON requests only. List other domains in
allowedOrigins. - Returns
{ verdict, roleBased }and nothing else from the API. - Remembers each answer for 10 minutes, so checking an address twice is charged once.
- Answers
unknownwhen LeadMagic is slow, rate limited or out of credits, so signups keep working. - Waits at most 8 seconds. Change it with
timeoutMs. - Checks at most 10 new addresses a minute per visitor and 120 a minute in total, on each server instance. Past that it answers
unknownand spends nothing. Change it withrateLimit.
Anyone who loads your page can call this route, and the built-in caps are counted per server instance. Add your bot check or a shared rate limiter with protect:
3. Use the field
Install the ready-made field. It is built on shadcn/ui and copied into your project, so you can change it:
To show your server action's message, pass it as error. The field shows it in place of its own hint, so a blocked signup shows one message.
Or use the hook with your own input:
status is idle, checking, deliverable, undeliverable or unknown. The hook checks when the field loses focus. Pass validateOn: "change" to check while typing, with a 600 ms debounce. validate(value) checks on demand and returns the verdict. It works with React Hook Form and TanStack Form.
Customize the hook
The hook takes options in the style of TanStack Query. All of them are in the reference.
Set options once for the whole app with the provider. Fields under it share answers:
Without a route
Pass queryFn to call a server action or your own API instead of the route:
A server action is as public as a route, and it does not get the route's same-origin check, cache or rate limit. Add your own limits before you ship it.
With TanStack Query
If your app already uses TanStack Query, let it own the request:
4. Check again on submit
Browser checks can be skipped, so check again where the form is handled:
Only block on undeliverable. An unknown result is common for catch-all company domains and is not a sign of a bad address.
Route options
| Option | Default | |
|---|---|---|
allowedOrigins | [] | Origins allowed besides the route's own |
protect | none | (request, { email }) => boolean | Response |
timeoutMs | 8000 | Longest the form waits |
cache | { ttlMs: 600000, max: 1000 } | false to turn off |
rateLimit | { perIp: 10, total: 120, windowMs: 60000 } | Caps on checks that reach LeadMagic. false to turn off |
skip | none | (email) => boolean. true answers unknown without calling LeadMagic |
failOpen | true | false answers 503 instead of unknown when LeadMagic cannot answer |
verificationPolicy | "legacy" | "strict" reports policy-only rejections as unknown, which is free |
client | from env | A createClient() client or its options |
onResult | none | Called after each answer, for logging |
The example app is a shadcn/ui signup form with this setup.
Get your LeadMagic API key and test endpoints live.
Use your API key in the interactive REST playground. For the LeadMagic CLI, install the lm command and run lm login; hosted MCP clients also sign in with OAuth.