Integrations
Validate phone numbers and emails in SvelteKit
Check emails, phone numbers and IPs in a SvelteKit form action with the SDK. Return field errors with fail, flag risky signups and fail open on API errors.
Updated 2026-11-23
This recipe validates a signup in a SvelteKit form action. The email, phone number and client IP are checked on the server, field errors go back through fail and an API outage never blocks a signup.
What you need
- SvelteKit 2 on Node.js 20 or later, or any adapter that runs server code.
- A Qelka API key in the
API_KEYenvironment variable. Ask_test_key returns simulated results and never spends credits: see Test mode. - The key in a private environment variable, used only in server files: see Authentication.
Steps
1. Install the SDK
npm install @qelka/sdk
2. The validation module
Save as src/lib/server/validate-signup.ts. Files under $lib/server cannot be imported from client code.
import { env } from '$env/dynamic/private'
import { ApiConnectionError, ApiError, Client } from '@qelka/sdk'
const client = new Client({ apiKey: env.API_KEY, timeoutMs: 8000 })
export type Verdict = {
errors: Record<string, string>
checked: boolean
e164: string | null
review: boolean
}
export async function validateSignup(input: {
email?: string
phone?: string
country?: string
ip?: string
}): Promise<Verdict> {
const verdict: Verdict = { errors: {}, checked: true, e164: null, 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
verdict.checked = false
console.error('validation skipped', error instanceof ApiError ? `${error.type} ${error.requestId}` : 'unreachable')
}
}
await Promise.all([
input.email &&
settle(async () => {
const result = await client.email.verify({ email: input.email as string })
if (result.status === 'undeliverable') {
verdict.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') {
verdict.review = true
}
}),
input.phone &&
settle(async () => {
const result = await client.phone.validate({ phone: input.phone as string, country: input.country })
if (result.status === 'valid') verdict.e164 = result.e164
if (result.status === 'invalid') verdict.errors.phone = 'This phone number is not valid.'
}),
input.ip &&
settle(async () => {
const result = await client.ip.lookup({ ip: input.ip as string })
if (result.risk.level === 'high' || result.flags.tor) verdict.review = true
}),
])
return verdict
}
3. The form action
Save as src/routes/signup/+page.server.ts:
import { fail } from '@sveltejs/kit'
import type { Actions } from './$types'
import { validateSignup } from '$lib/server/validate-signup'
export const actions: Actions = {
default: async ({ request, getClientAddress }) => {
const form = await request.formData()
const email = String(form.get('email') ?? '')
const phone = String(form.get('phone') ?? '')
const verdict = await validateSignup({
email,
phone,
country: String(form.get('country') ?? '') || undefined,
ip: getClientAddress(),
})
if (Object.keys(verdict.errors).length > 0) return fail(422, { email, phone, errors: verdict.errors })
await createUser({ email, phone: verdict.e164 ?? phone, needsReview: verdict.review, needsRecheck: !verdict.checked })
return { success: true }
},
}
4. The page
Save as src/routes/signup/+page.svelte:
<script lang="ts">
import { enhance } from '$app/forms'
let { form } = $props()
</script>
<form method="POST" use:enhance>
<input name="email" type="email" value={form?.email ?? ''} required />
{#if form?.errors?.email}<p role="alert">{form.errors.email}</p>{/if}
<input name="phone" type="tel" value={form?.phone ?? ''} />
{#if form?.errors?.phone}<p role="alert">{form.errors.phone}</p>{/if}
<button type="submit">Sign up</button>
</form>
createUser is your own function. The page uses the Svelte 5 $props rune: on Svelte 4 write export let form instead. Store e164 instead of what was typed.
Read the result
| Result | Action |
|---|---|
Email undeliverable |
Reject with 422, 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, add friction |
Handle errors and rate limits
No error is billed. The SDK retries 429, 502 and 503 responses, waiting Retry-After when present. After 200 requests per 10 seconds on one key you get 429 rate_limited. 401 invalid_api_key and 403 forbidden mean a wrong or restricted key. 402 insufficient_credits means an empty balance: alert yourself and keep failing open. Each email check costs 1 credit. See Errors and Rate limits.
Related
Frequently asked questions
How do I read the API key in SvelteKit?
How do I get the client IP?
What happens if the API is down?
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