/* * Domailfix for HubSpot: a workflow "Custom code" step that verifies a contact's email. * Needs HubSpot Data Hub (formerly Operations Hub) Professional or Enterprise. * * SETUP (once): * 1. Settings > Properties > Contact properties: create "Email status" (single-line text). * 2. Automation > Workflows > Contact-based workflow, trigger "Contact is created". * 3. Add action "Custom code": Node.js 20.x. Secrets: add DOMAILFIX_API_KEY = your Domailfix * API key (dmf_...). Property to include: email -> Email. Paste this whole file. * Data outputs: status (String), score (Number), accept_all_score (Number), did_you_mean (String). * 4. Add action "Edit record": set "Email status" to the code step's status output. * A slow answer gives "unknown" (free) instead of failing. A wrong key or no credits * fails the step, so it shows in the workflow history. */ exports.main = async (event, callback) => { const email = (event.inputFields["email"] || "").trim(); if (!email) { return callback({ outputFields: { status: "no_email", score: 0, accept_all_score: -1, did_you_mean: "" } }); } const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), 18000); // HubSpot stops code steps after 20 seconds try { const res = await fetch("https://domailfix.achivix.com/api/verify?email=" + encodeURIComponent(email), { headers: { "X-API-Key": process.env.DOMAILFIX_API_KEY }, signal: controller.signal, }); const d = await res.json(); if (res.status === 401 || res.status === 402) { // Wrong key or no credits: fail the step so it shows up in the workflow history. clearTimeout(timer); throw Object.assign(new Error(d.error || "Domailfix error " + res.status), { fatal: true }); } if (!res.ok) throw new Error(d.error || "Domailfix error " + res.status); callback({ outputFields: { status: d.status, score: d.score, accept_all_score: d.accept_all_score == null ? -1 : d.accept_all_score, did_you_mean: d.did_you_mean || "", } }); } catch (e) { if (e.fatal) throw e; // A slow answer: report "unknown" (free) instead of failing the workflow. callback({ outputFields: { status: "unknown", score: 30, accept_all_score: -1, did_you_mean: "" } }); } finally { clearTimeout(timer); } };