AGENTS.md template for Express
A ready AGENTS.md for an Express API. It sets out the route, service and data layers, how errors are handled, and the security basics agents skip when they generate endpoints quickly.
# AGENTS.md An HTTP API built on Express and TypeScript. ## Commands - `npm run dev`: start the API with reload on http://localhost:3000 - `npm run build`: compile TypeScript - `npm test`: run unit and API tests - `npm run lint`: ESLint ## Project structure - `src/app.ts`: builds the Express app: middleware, routers, error handler - `src/routes/`: routers: parse the request, call a service, send the response - `src/services/`: business logic; no `req` or `res` in here - `src/db/`: queries and data access - `src/middleware/`: auth, validation, error handling ## Rules for agents - Route handlers parse the request, call a service and send the response. Business logic goes in `src/services/`. - Every async handler must pass failures to the error middleware. No request may hang on a thrown error. - Validate `req.body`, `req.query` and `req.params` at the route before using them. - Queries are parameterised. Never build SQL from strings containing input. ## Conventions ### Express - Async route handlers must bubble errors to an error middleware — use `express-async-errors` or wrap with a helper. - Validate every request body/query/param (Zod, Joi, express-validator). Never trust inputs. - Security middleware baseline: `helmet`, `cors` (explicit origins), rate limiting, body size limits. - Keep routes thin — delegate business logic to services, persistence to repos. ### Node.js - Node 20+ features are fair game (top-level await, fetch, test runner). - ESM only for new code. If the repo is CJS, match it — don't mix. - Never block the event loop. Long work goes to workers or a queue. - Env vars via `process.env.X` — validate at startup (Zod or `envsafe`). Fail fast on missing config. ### TypeScript - Prefer `type` for object shapes, `interface` only when extending third-party types. - Never use `any` — use `unknown` and narrow, or write a proper type. - No non-null assertions (`!`) except in tests or when a type guard just ran. - Use `readonly` for props, return types, and array params that don't mutate. - Prefer discriminated unions over enums. Enums only for stable public APIs. - Import types with `import type { ... }` to avoid runtime import cost. ## Avoid - Declaration-merging global namespaces. - Using `Function`, `Object`, or `{}` as types. ## Guardrails - Never commit secrets, API keys, or credentials. If you spot one in a diff, stop and flag it. - Do not invent dependencies. If a package is not already in the manifest, confirm before adding it. - When a command fails, read the actual error. Do not retry blindly or bypass with `--force`. - Prefer editing existing files over creating new ones. - Run the tests and the linter before saying a task is done, and say what you ran.
The commands and folders are the common defaults for Express. Change them to match your project before you commit the file: an agent will run exactly what is written here.
How to use it
Save the file as AGENTS.md in the root of your repository and commit it. Then check each tool you use:
| Tool | What to do |
|---|---|
| Claude Code | Reads AGENTS.md only when no CLAUDE.md exists in or above the folder. If you have a CLAUDE.md, put @AGENTS.md on its first line. |
| Cursor | Reads AGENTS.md from the project root, in the desktop app and the terminal. In the app, rules in .cursor/rules must be .mdc files. The terminal reads only plain .md files there. |
| Codex | Reads AGENTS.md from the root down to the folder you are in, with a shared 32 KiB limit across all of them. |
| GitHub Copilot | Reads AGENTS.md on some surfaces only. Keep a .github/copilot-instructions.md as well if your team relies on Copilot. |
Details and sources: the AGENTS.md compatibility matrix.
What agents get wrong in Express projects
These are the reasons the “Rules for agents” section says what it says.
Business logic in route handlers
Agents write the whole feature inside router.post(...). It works, and it cannot be tested or reused. The structure section gives them a place for each layer.
Unhandled async errors
An async handler that throws without reaching next(err) hangs the request on Express 4. Say which version you run and how errors reach the error middleware.
Trusting request bodies
Generated endpoints often pass req.body straight to the database. The template requires validation at the edge.
String-built SQL
Template literals with user input still appear in generated queries. Parameterised queries are in the Guardrails.
Questions
Where does AGENTS.md go in a Express project?
In the root of the repository, next to your manifest file. Tools read it from the folder you start them in and, for most tools, from parent folders too.
Does Claude Code read AGENTS.md?
Only when there is no CLAUDE.md in or above the working folder. If you already have a CLAUDE.md, keep it and make its first line @AGENTS.md so both tools read the same rules.
How long should the file be?
Short enough that every line is something the agent would get wrong without it. Codex shares a 32 KiB budget across all the AGENTS.md files it loads, and long files get followed less closely by every tool.
Does this fit Fastify, Hono or NestJS?
The layering advice carries over, but the framework rules differ. Use the generator and pick your framework to get the right ones.