Clay logo

LeadMagic has made more than one billion API calls with Clay and won The Unstoppable Wheel award.

Recap
LeadMagicDocs

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:

.env.local

Never name it NEXT_PUBLIC_LEADMAGIC_API_KEY. Next.js puts NEXT_PUBLIC_ variables in the browser bundle. The SDK refuses to run with that name outside production.

2. Add the route

app/api/leadmagic/validate-email/route.ts

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 unknown when 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 unknown and spends nothing. Change it with rateLimit.

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:

app/signup-form.tsx

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:

app/providers.tsx

Without a route

Pass queryFn to call a server action or your own API instead of the route:

app/actions.ts

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:

app/actions.ts

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

OptionDefault
allowedOrigins[]Origins allowed besides the route's own
protectnone(request, { email }) => boolean | Response
timeoutMs8000Longest 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
skipnone(email) => boolean. true answers unknown without calling LeadMagic
failOpentruefalse answers 503 instead of unknown when LeadMagic cannot answer
verificationPolicy"legacy""strict" reports policy-only rejections as unknown, which is free
clientfrom envA createClient() client or its options
onResultnoneCalled after each answer, for logging

The example app is a shadcn/ui signup form with this setup.

API key for REST · OAuth for LeadMagic CLI and MCP

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.