Skip to content

Content moderation for Node.js

Arcjet content moderation detects harmful content in untrusted text before it is stored, displayed, or forwarded. It is a Guard rule – call it from guard() / Guard, not protect().

What is Arcjet? Arcjet is the AI agent runtime security platform. Discover the agents running in your organization, enforce policy across every action, prompt, and tool call, and keep the evidence to prove what happened. Detect prompt injection, authorize agent tool calls, redact PII, and block bots and abuse.

This example screens inbound text with content moderation before you store or display it.

Content moderation is a Guard rule, so this example uses @arcjet/guard inside your Node.js server – not protect().

We assume you already have a Node.js project set up. For helper options and denial behavior, see the Agent guards guide.

Install the dependencies:

Terminal window
# Export your Arcjet API key from https://console.arcjet.com
export ARCJET_KEY="ajkey_..."
npm install @arcjet/guard

Create the example:

server.ts
import { launchArcjet, moderateContent } from "@arcjet/guard";
import http from "node:http";
const arcjet = launchArcjet({ key: process.env.ARCJET_KEY! });
const moderate = moderateContent();
const server = http.createServer(async (req, res) => {
if (req.method !== "POST") {
res.writeHead(405);
res.end();
return;
}
const chunks: Buffer[] = [];
for await (const chunk of req) {
chunks.push(chunk);
}
const { message } = JSON.parse(Buffer.concat(chunks).toString()) as {
message: string;
};
const decision = await arcjet.guard({
label: "message.received",
rules: [moderate(message)],
});
if (decision.reason === "ERROR") {
res.writeHead(503, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Content check could not complete" }));
return;
}
if (decision.conclusion !== "ALLOW") {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
error: "Message blocked by policy",
}),
);
return;
}
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ ok: true }));
});
server.listen(8000);

Then send a test POST request to the server.

Requests appear in your Arcjet Console in real time.

Terminal window
npm install @arcjet/guard
import { launchArcjet, moderateContent } from "@arcjet/guard";
const arcjet = launchArcjet({ key: process.env.ARCJET_KEY! });
const moderate = moderateContent();
const decision = await arcjet.guard({
label: "tools.chat",
rules: [moderate(userMessage)],
});
if (decision.conclusion !== "ALLOW") {
throw new Error("Message did not pass content moderation");
}
const result = moderate.result(decision);
// `detected` is true when harmful content was found. Billing is undefined
// when the service does not report usage. Content moderation uses text_units.
console.log(result?.detected, result?.billing?.unit, result?.billing?.count);

Keep the response generic. Do not leak detector details or explain exactly what was flagged.

For production handlers that accept longer messages, see how much text moderation checks and what to do when a check fails.

Discussion