Skip to content

Node.js

A complete integration with the built-in fetch. One helper that calls the API with a timeout and fails open, and one function that turns the answer into a decision.

This guide needs Node.js 18 or later for the built-in fetch and AbortSignal.timeout. It has no dependencies.

checkSignup returns Heimdal’s answer, or null when there is none: a timeout, a network error, or an error status. null means “no opinion”, and the caller lets the signup through.

heimdal.js
import { isIP } from "node:net";
const CHECK_URL = "https://heimdal.codebrewerz.com/api/v1/check";
/** Heimdal's answer, or `null` when the check could not be made. */
export async function checkSignup({ ip, email }) {
try {
const response = await fetch(CHECK_URL, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.HEIMDAL_API_KEY}`,
"Content-Type": "application/json",
},
// An address that is not valid would fail the whole request: leave it out.
body: JSON.stringify({ ip: isIP(ip ?? "") ? ip : null, email }),
signal: AbortSignal.timeout(2000),
});
if (!response.ok) {
const { error } = await response.json();
console.error(
`Heimdal ${response.status} ${error.code}: ${error.message}`,
);
return null;
}
return await response.json();
} catch (cause) {
console.error("Heimdal check failed, signup allowed:", cause);
return null;
}
}

Keep the policy in one function, apart from the HTTP call, so you can change it without touching the plumbing. This one refuses disposable email, Tor, open proxies and custom-list hits. VPN and hosting networks pass.

heimdal.js
/** The message that refuses this signup, or `null` to let it through. */
export function refusalFor(result) {
if (result === null) return null;
if (result.email?.disposable) {
return "Disposable email addresses can't be used to sign up.";
}
if (result.ip?.tor || result.ip?.proxy) {
return "Sign-ups from this network aren't allowed. Contact support if this is a mistake.";
}
if (result.custom.matched.length > 0) {
return "This sign-up can't be completed. Contact support if this is a mistake.";
}
return null;
}

To use the strict policy instead, replace the body with return result?.block ? "…" : null. Best practices explains the trade-off.

The example uses Express. The two calls are the same in any framework.

server.js
import express from "express";
import { checkSignup, refusalFor } from "./heimdal.js";
const app = express();
app.use(express.json());
// Behind one reverse proxy. Without this, `req.ip` is the proxy's address.
app.set("trust proxy", 1);
app.post("/signup", async (req, res) => {
const { email } = req.body;
const result = await checkSignup({ ip: req.ip, email });
const refusal = refusalFor(result);
if (refusal !== null) {
return res.status(400).json({ error: refusal });
}
if (result !== null && result.reasons.length > 0) {
console.info("signup allowed with signals", result.reasons, result.ip?.asn);
}
// Create the user as you do today.
res.status(201).end();
});
app.listen(3000);
  • It fails open. No answer from Heimdal never blocks a signup.
  • It has a timeout. The visitor waits two seconds at most.
  • It sends the visitor’s address. trust proxy makes req.ip the client address that your proxy reports. Set it to match your own setup.
  • It keeps the key on the server, in HEIMDAL_API_KEY.
  • It logs signals it did not act on, so you can review the policy later.

With the server running, a signup with a disposable address is refused:

Terminal window
curl -X POST "http://localhost:3000/signup" \
-H "Content-Type: application/json" \
-d '{"email": "test@mailinator.com", "password": "correct horse battery"}'
{ "error": "Disposable email addresses can't be used to sign up." }