Detects if a request is from an AI agent / browser operator.
Works in Node.js apps like Express, Next.js, or anything with req.get().
Currently supporting detection coming from:
- OpenAI Operator, through headers signature
npm install browseragent-detectorconst detectAgent = require('browseragent-detector');
// In Express.js
app.get('/api/data', (req, res) => {
if (detectAgent(req)) {
res.status(403).json({ error: 'AI agents not allowed' });
} else {
res.json({ data: 'your data here' });
}
});Detects if the request is from an AI agent.
Parameters:
ctx(Object): An object with aget()method that can retrieve HTTP headers- Must have:
get(headerName)method that returns the header value as a string - Examples: Express.js
reqobject, Next.jsreqobject
- Must have:
Returns:
boolean:trueif the request is from a detected AI agent,falseotherwise
- OpenAI Operator: Detected via
Signature-Agent: https://chatgpt.comheader
const express = require('express');
const detectAgent = require('browseragent-detector');
const app = express();
// Middleware to block AI agents
const blockAgents = (req, res, next) => {
if (detectAgent(req)) {
return res.status(403).json({
error: 'Access denied for AI agents'
});
}
next();
};
app.use('/api', blockAgents);// pages/api/protected.js
import detectAgent from 'browseragent-detector';
export default function handler(req, res) {
if (detectAgent(req)) {
return res.status(403).json({
error: 'AI agents not allowed'
});
}
// Your protected API logic here
res.json({ message: 'Hello human!' });
}Run the included tests:
npm testMIT