SDK reference
Functions, options and TypeScript types in the leadmagic SDK.
createClient(options?)
| Option | Default | |
|---|---|---|
apiKey | LEADMAGIC_API_KEY | Server-side only |
sandbox | LEADMAGIC_SANDBOX=1 | Synthetic data, no key, no credits |
baseUrl | https://api.leadmagic.io | Or LEADMAGIC_BASE_URL. Must be https |
timeoutMs | 10000 | Per call, retries included |
maxRetries | 2 | For 429, 502, 503, 504 and network errors |
fetch | global fetch | For tests or proxies |
headers | {} | Sent with every request |
Throws a config error when the key is missing or exposed. The top-level validateEmail, findCompany and getCredits use the same defaults.
validateEmail(email, options?)
POST /v1/people/email-validation. Returns Result<EmailValidation>.
| Option | |
|---|---|
verificationPolicy | "legacy" (default) or "strict". Strict reports policy-only rejections as unknown, which is free |
includeExtendedInfo | Adds extended_info |
includeVerificationDetails | Adds verification |
EmailValidation is the API response plus verdict:
email_status | verdict | Credits |
|---|---|---|
| valid | deliverable | 0.25 |
| invalid | undeliverable | 0.25 |
| unknown | unknown | 0 |
Input that is not an email address returns an invalid_input error without a request.
findCompany(input, options?)
POST /v3/companies/search. Pass domain, name or linkedinUrl; domain is the most precise. Returns Result<CompanyLookup>:
Company types the common fields and keeps every other field the API returns.
getCredits(options?)
GET /v1/credits. Free. Returns { available, frozen, raw }.
Per-call options
Every method accepts:
| Option | |
|---|---|
timeoutMs | Overrides the client's timeout |
maxRetries | Overrides the client's retries |
signal | An AbortSignal |
idempotencyKey | Reused across retries; generated when omitted |
sandboxOutcome | Sandbox only: found, empty, 400, 401, 402, 404, 429, 500 |
Result<T>
leadmagic/next
createValidateEmailHandler(options?) returns a POST Route Handler. Options are on Validate emails in Next.js forms.
leadmagic/react
useEmailValidation(options?)
Every option is optional.
| Option | Default | |
|---|---|---|
endpoint | /api/leadmagic/validate-email | Your route, wherever you mounted it |
validateOn | "blur" | "blur", "change" (debounced) or "manual" (only validate()) |
debounceMs | 600 | Wait after the last keystroke with "change" |
enabled | true | false stops the field checking by itself; validate() still works |
staleTime | 600000 | How long an answer is reused for the same address |
retry | 0 | Extra attempts after a failed check |
retryDelay | doubles from 1000 to 8000 | A number of ms, or (attempt) => ms |
timeoutMs | none | Give up on the request after this long |
headers | none | An object, or a function that returns one for each request |
fetch | global fetch | Custom fetch for the built-in request |
queryFn | none | (email, { signal }) => Promise<{ verdict, roleBased? }>. Replaces the request |
shouldValidate | none | (email) => boolean. false skips the check; it reads as unknown |
onSuccess | none | (result) => void |
onError | none | (error, email) => void |
onSettled | none | (result, error, email) => void |
Returns:
| Field | |
|---|---|
status | idle, checking, deliverable, undeliverable or unknown |
data | { email, verdict, roleBased }, or undefined before an answer |
error | Why the last check failed, or null |
isChecking, isDeliverable, isUndeliverable, isUnknown | Booleans for status |
roleBased | The address is a shared mailbox such as info@ |
validate(value) | Checks now and resolves to the verdict. Never rejects |
reset() | Back to idle |
inputProps | Spread onto your <input type="email"> |
A failed check never blocks the form: status becomes unknown and error says why. A refusal from your route, such as 403, is not retried.
LeadMagicProvider
Sets options once for every useEmailValidation below it: endpoint, headers, fetch, queryFn, staleTime, retry, retryDelay, timeoutMs, validateOn and debounceMs. Options passed to a hook override it. Fields under one provider share answers, so an address is asked about once even when two fields check it together.
checkEmail(email, options?)
The request the hook makes, as a plain function for any framework. Takes endpoint, headers, fetch, timeoutMs and signal. Resolves to { email, verdict, roleBased } and rejects with an EmailCheckError (with the route's status) when there is no verdict.
emailValidationQueryOptions(email, options?)
Returns { queryKey, queryFn, enabled, staleTime } for TanStack Query. enabled is false until the text looks like an address.
Types
The package ships its own type declarations. Import types with import type:
| Import | Types |
|---|---|
leadmagic | Results: ValidateEmailResult, FindCompanyResult, GetCreditsResult, Result<T>, ResponseMeta, RateLimitInfo |
Data: EmailValidation, EmailStatus, EmailVerdict, EmailCompanyLocation, CompanyLookup, Company, Credits | |
Inputs: LeadMagicOptions, LeadMagicClient, RequestOptions, ValidateEmailOptions, FindCompanyInput, SandboxOutcome | |
Errors: LeadMagicError (a class), LeadMagicErrorKind, LeadMagicErrorCode, LeadMagicApiErrorCode, LeadMagicClientErrorCode | |
leadmagic/next | ValidateEmailHandlerOptions, ValidateEmailResponse, ValidateEmailEvent, ProtectResult |
leadmagic/react | UseEmailValidation, UseEmailValidationOptions, EmailFieldStatus, EmailInputProps, EmailVerdict, EmailValidationResult, EmailCheckError (a class), CheckEmailOptions, EmailValidationQueryOptions, LeadMagicProviderProps, EmailValidationDefaults, RequestHeaders |
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.
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.
SDK errors
How the leadmagic SDK reports failures: six kinds to branch on, stable codes from the API, and what to do about each.
Was this page helpful?
We value your feedback.