Integrations
Validate phone numbers and emails in Nuxt
Validate emails and phone numbers in a Nuxt 3 or 4 server route with the official SDK. The API key stays in private runtime config, never in the browser.
Updated 2026-11-23
This recipe adds a Nitro server route to a Nuxt app that checks an email and a phone number, so your signup form can show errors before it submits.
What you need
- Nuxt 3 or 4 and Node.js 20 or later.
- A Qelka API key. A
sk_test_key returns simulated results and never spends credits: see Test mode. - The key stored as private runtime config. Anything under
runtimeConfig.publicis sent to the browser, so it must not go there. See Authentication.
Steps
1. Install the SDK
npm install @qelka/sdk
2. Declare the private config
In nuxt.config.ts:
export default defineNuxtConfig({
runtimeConfig: {
apiKey: '',
},
})
Set NUXT_API_KEY in your .env file and in your host's environment. Nuxt maps it onto runtimeConfig.apiKey at runtime.
3. The server route
Save as server/api/validate.post.ts:
import { ApiConnectionError, ApiError, Client } from '@qelka/sdk'
let client: Client | undefined
function getClient() {
client ??= new Client({ apiKey: useRuntimeConfig().apiKey, timeoutMs: 8000 })
return client
}
export default defineEventHandler(async (event) => {
const body = await readBody<{ email?: string; phone?: string; country?: string }>(event)
const ip = getRequestIP(event, { xForwardedFor: true })
const errors: Record<string, string> = {}
let checked = true
let e164: string | null = null
let review = false
const settle = async (task: () => Promise<void>) => {
try {
await task()
} catch (error) {
if (error instanceof ApiError && error.type === 'unprocessable_input') return
if (!(error instanceof ApiError) && !(error instanceof ApiConnectionError)) throw error
checked = false
console.error('validation skipped', error instanceof ApiError ? `${error.type} ${error.requestId}` : 'unreachable')
}
}
if (body.email) {
await settle(async () => {
const result = await getClient().email.verify({ email: body.email as string })
if (result.status === 'undeliverable') {
errors.email = result.did_you_mean ? `Did you mean ${result.did_you_mean}?` : 'This email address cannot receive mail.'
} else if (result.status === 'risky' && result.reason === 'disposable') {
review = true
}
})
}
if (body.phone) {
await settle(async () => {
const result = await getClient().phone.validate({ phone: body.phone as string, country: body.country })
if (result.status === 'valid') e164 = result.e164
if (result.status === 'invalid') errors.phone = 'This phone number is not valid.'
})
}
if (ip) {
await settle(async () => {
const result = await getClient().ip.lookup({ ip })
if (result.risk.level === 'high' || result.flags.tor) review = true
})
}
return { ok: Object.keys(errors).length === 0, errors, checked, e164, review }
})
The SDK retries 429, 502 and 503 for you. When a call fails for good, checked becomes false and no error is shown to the person: the signup goes through and you re-check later. Return only what the form needs; do not forward the raw API result.
4. Call it from the form
<script setup lang="ts">
const form = reactive({ email: '', phone: '', country: 'US' })
const errors = ref<Record<string, string>>({})
async function submit() {
const verdict = await $fetch('/api/validate', { method: 'POST', body: form })
errors.value = verdict.errors
if (!verdict.ok) return
await $fetch('/api/signup', { method: 'POST', body: { ...form, phone: verdict.e164 ?? form.phone, review: verdict.review } })
}
</script>
Store e164, the normalized number, rather than what was typed. Save the review flag with the account so a person can look at flagged signups later.
5. Read the result
| Result | Action |
|---|---|
Email undeliverable |
Reject and show did_you_mean |
Email risky, reason disposable |
Flag, or reject if you block disposable inboxes |
Email unknown |
Accept and confirm by email |
Phone valid |
Store e164 |
Phone unknown |
No + and no country: send the country |
IP high risk or Tor |
Flag for review |
Handle errors and rate limits
Authentication errors (401, 403) mean a wrong or restricted key: fix it, do not retry. 402 insufficient_credits means an empty balance. 429 rate_limited happens above 200 requests per 10 seconds for a key and is retried by the SDK after Retry-After. A 422 on the IP is a private address, normal on localhost. Errors are never billed. See Errors.
Add your own rate limit on /api/validate, otherwise a script can spend your credits through your own form.
Related
Frequently asked questions
Where do I put the API key in Nuxt?
Why a server route and not a composable?
Does it work on Cloudflare or other edge presets?
Put this check in your product
Get an API key in under a minute. 100 free credits every month, no card required.
- 100 free credits a month
- No card
- Test keys spend nothing
- Cancel any time