Integrations
Validate phone numbers and emails in Express
Add an Express middleware that checks emails, phone numbers and IPs with the official SDK, rejects bad input with a 422 and fails open on API errors.
Updated 2026-11-23
This recipe adds a validation middleware to an Express app. It checks the email and phone in the request body, looks up the client IP, and either rejects the request or lets it through with a verdict attached.
What you need
- Express 4 or 5 and Node.js 20 or later.
- A Qelka API key in the
API_KEYenvironment variable. Ask_test_key returns simulated results and never spends credits: see Test mode. - The API called from the server only: see Authentication.
Steps
1. Install
npm install express @qelka/sdk
2. The middleware
import express from 'express'
import { ApiConnectionError, ApiError, Client } from '@qelka/sdk'
const client = new Client({ apiKey: process.env.API_KEY, timeoutMs: 8000 })
export async function validateSignup(req, res, next) {
const errors = {}
const verdict = { checked: true, e164: null, review: false }
const settle = async (task) => {
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')
}
}
try {
if (req.body.email) {
await settle(async () => {
const result = await client.email.verify({ email: req.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
}
})
}
if (req.body.phone) {
await settle(async () => {
const result = await client.phone.validate({ phone: req.body.phone, country: req.body.country })
if (result.status === 'valid') verdict.e164 = result.e164
if (result.status === 'invalid') errors.phone = 'This phone number is not valid.'
})
}
if (req.ip) {
await settle(async () => {
const result = await client.ip.lookup({ ip: req.ip })
if (result.risk.level === 'high' || result.flags.tor) verdict.review = true
})
}
} catch (error) {
return next(error)
}
if (Object.keys(errors).length > 0) return res.status(422).json({ errors })
req.validation = verdict
next()
}
3. Use it on a route
const app = express()
app.set('trust proxy', 1)
app.use(express.json())
app.post('/signup', validateSignup, async (req, res) => {
const { checked, e164, review } = req.validation
const user = await createUser({
email: req.body.email,
phone: e164 ?? req.body.phone,
needsReview: review,
needsRecheck: !checked,
})
res.status(201).json({ id: user.id })
})
app.listen(3000)
createUser is your own function. Store e164 instead of what was typed, and keep needsReview and needsRecheck on the account: a background job can re-run the checks for accounts where the API was unavailable.
4. 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 |
An IP check on 127.0.0.1 returns 422 unprocessable_input. That is normal in development, and the middleware treats it as no signal.
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 your balance is empty: alert yourself and keep failing open. No error is billed. See Errors and Rate limits.
Protect the route itself with your own rate limiter, such as express-rate-limit, so a bot cannot spend your credits through your signup endpoint.
Related
Frequently asked questions
Should validation be middleware or inside the route?
How do I get the real client IP in Express?
What status code should I return for a bad email?
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