Express bot detection and email validation
A factory-style middleware with an explicit timeout and fail-open behaviour, mountable per route.
1. Install
npm install express
2. Add the file
middleware/conure.js
const CONURE = "https://uat.conureapi.com";
/**
* @param {{ key: string, timeoutMs?: number }} options
*/
export function blockBots({ key, timeoutMs = 3000 }) {
if (!key) throw new Error("blockBots: missing API key");
return async function (request, response, next) {
const ip = request.ip;
if (!ip) return next();
try {
const upstream = await fetch(CONURE + "/v1/bot-check", {
method: "POST",
headers: { Authorization: "Bearer " + key, "Content-Type": "application/json" },
body: JSON.stringify({ ip, user_agent: request.get("user-agent") || "" }),
signal: AbortSignal.timeout(timeoutMs),
});
// A 4xx or 5xx from us is not a verdict.
if (!upstream.ok) return next();
const verdict = await upstream.json();
if (verdict.is_bot === true) {
return response.status(403).json({
error: "Automated traffic is not permitted.",
reasons: verdict.reasons ?? [],
});
}
} catch (error) {
request.log?.warn?.({ error }, "conure unreachable");
}
return next();
};
}3. Wire it up
import express from "express";
import { blockBots } from "./middleware/conure.js";
const app = express();
app.set("trust proxy", 1); // required for request.ip to be the real client
app.use(express.json());
app.post("/signup", blockBots({ key: process.env.CONURE_API_KEY }), signupHandler);4. Verify
Confirm the API answers before you debug your Express
wiring. The sample address sits inside a published AWS range, so a correct setup
returns is_bot: true.
curl -X POST https://uat.conureapi.com/v1/bot-check \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"ip":"52.1.2.3","user_agent":"curl/8.4.0"}'
import requests
HEADERS = {"Authorization": "Bearer YOUR_API_KEY"}
response = requests.post(
"https://uat.conureapi.com/v1/bot-check",
headers=HEADERS,
json={"ip": "52.1.2.3", "user_agent": "curl/8.4.0"},
timeout=5,
)
response.raise_for_status()
print(response.json())
const response = await fetch("https://uat.conureapi.com/v1/bot-check", {
method: "POST",
headers: {
Authorization: "Bearer YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({"ip":"52.1.2.3","user_agent":"curl/8.4.0"}),
signal: AbortSignal.timeout(5000),
});
if (!response.ok) throw new Error("conure: " + response.status);
console.log(await response.json());
$curl = curl_init("https://uat.conureapi.com/v1/bot-check");
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => '{"ip":"52.1.2.3","user_agent":"curl/8.4.0"}',
CURLOPT_HTTPHEADER => [
"Authorization: Bearer YOUR_API_KEY",
"Content-Type: application/json",
],
]);
$body = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
curl_close($curl);
$verdict = $status === 200 ? json_decode($body, true) : null;
var_dump($verdict);
require "net/http"
require "json"
uri = URI("https://uat.conureapi.com/v1/bot-check")
request = Net::HTTP::Post.new(uri)
request["Authorization"] = "Bearer YOUR_API_KEY"
request["Content-Type"] = "application/json"
request.body = { ip: "52.1.2.3", user_agent: "curl/8.4.0" }.to_json
response = Net::HTTP.start(uri.hostname, uri.port,
use_ssl: uri.scheme == "https",
open_timeout: 5, read_timeout: 5) do |http|
http.request(request)
end
puts JSON.parse(response.body)
Notes
- Without app.set("trust proxy"), request.ip is your load balancer and every check returns the same datacenter verdict.
- Node 18+ has global fetch and AbortSignal.timeout. On Node 16, add undici.