Skip to content

Integrations

Validate phone numbers and emails in Vercel Functions

Check emails, phone numbers and IPs from a Vercel Function with the SDK. Key in project environment variables, visitor IP from the platform header, fail open on errors.

Updated 2026-11-23

This recipe adds a plain Vercel Function that validates a signup: the email, phone number and visitor IP are checked on the server, clear negatives answer 422 and an API outage never blocks a signup. If your project uses Next.js, the Next.js integration covers route handlers and Server Actions instead.

What you need

  • A Vercel project and Node.js 20 or later.
  • A Qelka API key as the API_KEY project environment variable. A sk_test_ key returns simulated results and never spends credits: see Test mode.
  • Server-side code only: see Authentication.

Steps

1. Install the SDK

bash
npm install @qelka/sdk

2. The function

Save as api/signup.ts at the root of the project. Vercel serves it at /api/signup.

typescript
import { ApiConnectionError, ApiError, Client } from '@qelka/sdk'

const client = new Client({ apiKey: process.env.API_KEY as string, timeoutMs: 8000 })

export async function POST(request: Request): Promise<Response> {
  const body = await request.json().catch(() => null)
  if (!body) return Response.json({ errors: { body: 'Send a JSON body.' } }, { status: 400 })

  const ip = request.headers.get('x-forwarded-for')?.split(',')[0]?.trim()
  const errors: Record<string, string> = {}
  const verdict = { checked: true, e164: null as string | 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([
    body.email &&
      settle(async () => {
        const result = await client.email.verify({ email: body.email })
        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') {
          verdict.review = true
        }
      }),
    body.phone &&
      settle(async () => {
        const result = await client.phone.validate({ phone: body.phone, country: body.country })
        if (result.status === 'valid') verdict.e164 = result.e164
        if (result.status === 'invalid') errors.phone = 'This phone number is not valid.'
      }),
    ip &&
      settle(async () => {
        const result = await client.ip.lookup({ ip })
        if (result.risk.level === 'high' || result.flags.tor) verdict.review = true
      }),
  ])

  if (Object.keys(errors).length > 0) return Response.json({ errors }, { status: 422 })
  return Response.json({ phone: verdict.e164 ?? body.phone, needsReview: verdict.review, needsRecheck: !verdict.checked }, { status: 201 })
}

3. Call it

bash
curl -X POST https://your-project.vercel.app/api/signup \
  -H "Content-Type: application/json" \
  -d '{"email": "jane@example.com", "phone": "+14155550123"}'

Functions have a maximum duration that depends on your plan and configuration: check the current limit, and keep the SDK timeout below it. An email check in fast mode answers sooner than one in full mode. Add your own rate limit on the endpoint so a bot cannot spend your credits through it. 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.

Frequently asked questions

Where do I set the API key on Vercel?
In the project settings under Environment Variables, scoped to Production, Preview and Development as you need. Use a sk_test_ key for Preview and Development. Do not give the variable a NEXT_PUBLIC_ prefix, and do not commit it. Redeploy after changing it, since existing deployments keep the old value.
How do I get the visitor IP on Vercel?
Vercel sets the x-forwarded-for header with the client address on requests that reach your function, and the request object of the helper package exposes it too. Read the first value and ignore headers the client could have sent. Preview deployments behind protection may show a different address.
What happens if the API is down?
The signup goes through. A failed call never produces a form error: only a clear negative answer, such as an undeliverable email or an invalid phone number, rejects the request. The verdict records that the check did not run, so a background job can re-check the account later.

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