Skip to content

Integrations

Validate phone numbers and emails in Deno

Check emails, phone numbers and IPs from Deno and Deno Deploy with the SDK from npm. A Deno.serve signup endpoint that rejects bad input and fails open.

Updated 2026-11-23

This recipe adds a signup endpoint to a Deno server. It checks the email, the phone number and the client IP with the SDK, answers 422 for clear negatives and lets everything else through.

What you need

  • Deno 1.40 or later, or Deno Deploy.
  • A Qelka API key in the API_KEY environment variable. A sk_test_ key returns simulated results and never spends credits: see Test mode.
  • Server-side code only: see Authentication.

Steps

1. The server

Save this as server.ts:

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

const client = new Client({ apiKey: Deno.env.get('API_KEY') ?? '', timeoutMs: 8000 })

type Verdict = { errors: Record<string, string>; checked: boolean; e164: string | null; review: boolean }

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! })
        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!, 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! })
        if (result.risk.level === 'high' || result.flags.tor) verdict.review = true
      }),
  ])
  return verdict
}

Deno.serve(async (request, info) => {
  const url = new URL(request.url)
  if (request.method !== 'POST' || url.pathname !== '/signup') return new Response('Not found', { status: 404 })

  const body = await request.json().catch(() => null)
  if (!body) return Response.json({ errors: { body: 'Send a JSON body.' } }, { status: 400 })

  const ip = info.remoteAddr.transport === 'tcp' ? info.remoteAddr.hostname : undefined
  const verdict = await validateSignup({ email: body.email, phone: body.phone, country: body.country, ip })

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

2. Run it

bash
deno run --allow-net --allow-env=API_KEY server.ts
bash
curl -X POST http://localhost:8000/signup \
  -H "Content-Type: application/json" \
  -d '{"email": "jane@example.com", "phone": "+14155550123"}'

On a local run the client address is 127.0.0.1, so the IP check answers 422 unprocessable_input. The code treats that as no signal.

3. 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

Keep needsReview and needsRecheck on the account: a background job can re-run the checks where the API was unavailable.

Handle errors and rate limits

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. No error is billed. See Errors and Rate limits.

On Deno Deploy, set API_KEY in the project's environment variables, not in the code.

Frequently asked questions

Does the SDK work in Deno?
Yes. The SDK uses only the platform fetch and Web Crypto and has no runtime dependencies, so Deno runs it through the npm: specifier. You can also skip it and call the API with fetch, since the API is plain HTTPS and JSON with a single Authorization header.
Which permissions does the script need?
Network access to the API host and read access to the API_KEY environment variable: --allow-net and --allow-env=API_KEY. Granting only those keeps a bug or a bad dependency from reading the rest of your environment or the file system.
How do I get the client IP in Deno.serve?
The handler receives a second argument with info.remoteAddr, which holds the hostname of the connection. Behind a proxy that is the proxy, so read the header your platform sets, such as the forwarded address, and trust only proxies you control. On Deno Deploy use the header the platform documents.

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