Skip to content

AdanRott/browseragent-detector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

browseragent-detector

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

Install

npm install browseragent-detector

Quick Start

const 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' });
    }
});

API Reference

detectAgent(ctx)

Detects if the request is from an AI agent.

Parameters:

  • ctx (Object): An object with a get() method that can retrieve HTTP headers
    • Must have: get(headerName) method that returns the header value as a string
    • Examples: Express.js req object, Next.js req object

Returns:

  • boolean: true if the request is from a detected AI agent, false otherwise

Currently supported Agents

  • OpenAI Operator: Detected via Signature-Agent: https://chatgpt.com header

Examples

Express.js Middleware

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);

Next.js API Route

// 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!' });
}

Testing

Run the included tests:

npm test

License

MIT

About

"Detect AI agents and browser operators in Node.js applications. Works with Express, Next.js, and other frameworks.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors