Sheets & data
Enrich Segment identify events with email status
A Segment destination insert function adds email_status, email_reason and email_disposable traits to identify calls, with the API key in a secret setting.
Updated 2026-11-26
This recipe adds email verification results to the identify calls Segment sends to a destination. A destination insert function reads traits.email, asks the API about it, and adds email_status, email_reason, email_disposable and, when there is one, email_suggestion to the event's traits, so your CRM, email tool or ad platform receives them with the profile. There is no Qelka destination in the Segment catalog: the function is a few lines of JavaScript that runs inside Segment.
What you need
- A Segment workspace where you can create functions (the Functions Admin role or Workspace Owner) and a cloud-mode destination to enrich. Insert functions do not work with device-mode or storage destinations, and Functions availability depends on your Segment plan: check yours.
- A Qelka API key. A
sk_test_key returns simulated results and never spends credits: see Test mode. Use a key restricted toemailin the dashboard: see Restricted keys.
Steps
1. Create the function
In Segment, go to Connections, Catalog, Functions, and select New Function. Choose Insert as the type, then Next: Build Function.
2. Add the API key setting
In the code editor's Settings tab, select Add Setting:
- Label:
API key. Segment generates the nameapiKey, which the code reads assettings.apiKey. - Type: string. Turn on Required and Secret, so the value is never shown in the Segment interface.
You enter the actual key later, when you connect the function to a destination, and a test value in the Test tab.
3. The code
Replace the template with:
const CACHE_MS = 24 * 60 * 60 * 1000
async function verifyEmail(email, apiKey) {
const controller = new AbortController()
const timer = setTimeout(() => controller.abort(), 3000)
try {
const response = await fetch('https://api.qelka.com/v1/email', {
method: 'POST',
headers: { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ email, mode: 'fast' }),
signal: controller.signal,
})
const body = await response.json()
if (!response.ok) {
const type = body && body.error ? body.error.type : `http_${response.status}`
throw new Error(`${type} request ${response.headers.get('X-Request-Id')}`)
}
return {
status: body.status,
reason: body.reason,
disposable: body.checks.disposable,
suggestion: body.did_you_mean,
}
} finally {
clearTimeout(timer)
}
}
async function onIdentify(event, settings) {
const traits = event.traits || {}
const email = typeof traits.email === 'string' ? traits.email.trim().toLowerCase() : ''
if (!email || traits.email_status) return event
let result = null
try {
result = await cache.load(`email:${email}`, CACHE_MS, () => verifyEmail(email, settings.apiKey))
} catch (error) {
console.log(`validation skipped: ${error.name === 'AbortError' ? 'timeout' : error.message}`)
}
if (result) {
event.traits = {
...traits,
email_status: result.status,
email_reason: result.reason,
email_disposable: result.disposable,
...(result.suggestion ? { email_suggestion: result.suggestion } : {}),
}
}
return event
}
async function onTrack(event) {
return event
}
async function onPage(event) {
return event
}
async function onScreen(event) {
return event
}
async function onGroup(event) {
return event
}
async function onAlias(event) {
return event
}
What each choice is for:
- Every handler returns the event. Segment blocks an event type whose handler is missing, with an Unsupported Event Type error, so the function passes track, page, screen, group and alias calls through untouched.
mode: 'fast'and a three-second timeout. Segment documents a five-second limit for a function run, after which it retries the run for up to four hours. Fast mode runs the syntax, DNS, disposable and role checks without contacting the mail server, so it answers quickly; it cannot returndeliverable, and a clean address comes backunknownwith the reasonsmtp_not_checked. See the email reference.cache.load. Segment's function cache keeps a result for a day under the address, so repeated identify calls for the same person do not call the API again. WhenverifyEmailthrows, nothing is cached and the next event tries again. The cache lives in each function process and can be emptied at any time, so treat it as a saving, not a guarantee.- The log line names the error type and the request id, never the email. Segment warns that function logs can be visible to other workspace members, so personal data stays out of them.
- The event is never dropped or retried for an API problem. It flows on without the email traits.
4. Test it
In the Test tab, enter a test key as the apiKey value, customize the event and run it with:
{
"type": "identify",
"userId": "user_123",
"traits": { "email": "disposable@example.com", "name": "Test User" }
}
The output shows the traits with email_status set to risky, email_reason set to disposable and email_disposable set to true. Then try nomx@example.com, which gives undeliverable with domain_no_mx in fast mode, and an event without an email, which must come back unchanged. Function tests start with an empty cache, so every test run calls the API.
5. Connect it to a destination
Select Next: Configure & Create, name the function and create it. From the function's side panel, choose Connect a destination and pick the destination to enrich, then enter the live key in its settings. Finally, open the destination, go to its Functions tab and turn on Enable Function. One destination accepts one insert function, and one function can serve several destinations.
Segment applies destination filters before the insert function, and the enriched traits reach only the destinations the function is connected to: they do not flow back into your other destinations or your Segment profiles. If every tool should receive them, look at source insert functions, which Segment offers in public beta: the same onIdentify handler runs before events reach any destination.
Use the traits
email_status |
email_reason examples |
What a destination can do |
|---|---|---|
undeliverable |
invalid_syntax, domain_not_found, domain_null_mx |
Suppress from email sends, flag for sales |
risky |
disposable (email_disposable is true) |
Exclude from trials, rewards and ad audiences |
unknown |
smtp_not_checked |
Normal: fast mode passed every check it runs |
| Absent | Not checked: no email, or the API did not answer |
With fast mode, a deliverable status never appears: an address that passes every check is unknown with smtp_not_checked, which you can treat as acceptable. email_suggestion holds a corrected address such as jane@gmail.com for jane@gmial.com.
Handle errors and rate limits
Nothing is billed on errors. 401 invalid_api_key, 403 forbidden and 402 insufficient_credits leave events without traits; the function's logs show the error type, so check them when the traits stop appearing, and fix the key or the balance. A 429 or a timeout also leaves the event untouched, with no retry inside the function, since the five-second budget leaves no room for one. A key allows 200 requests per 10 seconds. A busy source can exceed that during a spike; the cache absorbs repeats, and new addresses beyond the limit simply pass unenriched. See Errors and Rate limits.
Each check costs 1 credit, and results served from the API's own 7-day cache cost the same, so the Segment cache is what saves credits. For high traffic, the cheapest design is to verify once in your backend when the person signs up, as in Validate signup forms, and send the traits in your own identify call.
Related
Frequently asked questions
Insert function or source function?
Will every identify call spend a credit?
What happens when the API is slow or down?
More in Sheets & data
All integrationsPut 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