Brownstone AI Access Control for Express
raw JSON →This `brownstone-middleware` package, currently at version 0.1.4, integrates with Express.js applications to manage and monetize AI agent access. It operates by detecting known AI crawlers and bots based on their user-agents. The middleware offers capabilities for logging AI hits (metering), enforcing access policies by blocking unauthorized agents with a 403 status, and injecting licensing metadata into HTML responses via a `<meta name="ai-usage">` tag. This allows AI systems to directly read usage terms. A key differentiator is its reliance on an external Brownstone API for comprehensive analytics, centralized licensing configuration, and robust bot detection, providing developers with granular control over how their content is accessed and used by AI models. The project is actively maintained, with ongoing updates to agent detection and API features.
Common errors
error TypeError: brownstone is not a function ↓
const brownstone = require('brownstone-middleware'); if your project is CommonJS. If using ESM, a bundler might be needed to handle the CJS package, or await official ESM support in future versions. error Brownstone API Error: API Key missing or invalid. ↓
apiKey string, obtained from brownstoneai.dev, to the middleware options object: brownstone({ apiKey: 'your_valid_key', metering: true }). Warnings
gotcha An `apiKey` is mandatory for both `metering` and `enforcement` features to function. Without a valid key, the middleware will operate in a passive detection-only mode or throw errors if an API call is attempted. ↓
gotcha The `<meta name="ai-usage">` tag injection only occurs for HTML responses. If your application primarily serves JSON or other content types, this specific feature will not be active for those routes. ↓
breaking As a pre-1.0 package (current version 0.1.4), internal APIs and configuration options are subject to change in minor or patch releases without strict adherence to SemVer for breaking changes. Developers should review release notes carefully. ↓
gotcha Brownstone middleware relies on user-agent strings for AI detection. While a comprehensive list is provided, custom or rapidly evolving AI agents might not be immediately recognized, potentially bypassing metering or enforcement. ↓
Install
npm install brownstone-middleware yarn add brownstone-middleware pnpm add brownstone-middleware Imports
- brownstone wrong
import brownstone from 'brownstone-middleware';correctconst brownstone = require('brownstone-middleware');
Quickstart
const express = require("express");
const brownstone = require("brownstone-middleware");
const app = express();
const BROWNSTONE_API_KEY = process.env.BROWNSTONE_API_KEY ?? "your-brownstone-api-key";
app.use(
brownstone({
apiKey: BROWNSTONE_API_KEY,
metering: true,
enforcement: true,
license: {
model: "paid",
rate: "$0.001-per-1000-tokens",
permission: "allowed",
},
}),
);
app.get("/", (req, res) => {
res.send("<html><head></head><body>Hello, AI world!</body></html>");
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Express server listening on port ${PORT}`);
console.log('Try visiting / with an AI user-agent (e.g., GPTBot) to see metering and enforcement in action.');
});