# SWAGENT - Complete Documentation > API documentation for the agent era. Transform OpenAPI specs into AI-readable, developer-friendly, and discoverable formats. Website: https://swagent.dev GitHub: https://github.com/X24Labs/SWAGENT npm: https://www.npmjs.com/package/swagent License: MIT --- ## Overview SWAGENT solves a growing problem: LLM agents consume API documentation, but traditional formats (Swagger UI, Redoc, raw OpenAPI JSON) waste thousands of tokens on navigation chrome, repeated schemas, and verbose formatting. SWAGENT takes any OpenAPI/Swagger specification and produces three outputs: 1. **llms.txt** - Token-optimized compact notation designed for AI agents. Uses ~75% fewer tokens than raw OpenAPI JSON while preserving all endpoint information. 2. **to-humans.md** - Full markdown reference with table of contents, parameter tables, response schemas, and authentication guides. 3. **index.html** - Zero-JavaScript semantic HTML landing page with dark theme, CSS-only interactions. ## Installation ```bash # Core library npm install @swagent/core # Framework adapters npm install @swagent/fastify npm install @swagent/express npm install @swagent/hono # CLI tool npm install -g swagent ``` ## Packages ### @swagent/core Pure generator functions with zero runtime dependencies. All other packages depend on this. Key function: `generate(spec, options)` orchestrates three generators: - `llms-txt.ts` - Token-optimized markdown - `human-docs.ts` - Full markdown with ToC and tables - `html-landing.ts` - Zero-JS semantic HTML ### @swagent/fastify Fastify plugin using `fastify-plugin`. Reads spec from `@fastify/swagger` automatically. Eager generation at startup. ```js import { swagentFastify } from '@swagent/fastify'; app.register(swagentFastify, { baseUrl: 'https://api.example.com' }); ``` ### @swagent/express Express Router middleware. Pass your OpenAPI spec, mount at any path. Lazy caching on first request. ```js import { swagentExpress } from '@swagent/express'; app.use(swagentExpress(spec, { baseUrl: 'https://api.example.com' })); ``` ### @swagent/hono Hono sub-app instance. Uses native `c.html()`, `c.text()`, `c.json()`. Lightweight and fast. ```js import { swagentHono } from '@swagent/hono'; app.route('/', swagentHono(spec, { baseUrl: 'https://api.example.com' })); ``` ### swagent (CLI) Generate docs from any OpenAPI spec file or URL. Output llms.txt, markdown, and HTML to disk. ```bash # From a local file npx swagent generate ./openapi.json -o ./docs # From a URL npx swagent generate https://api.example.com/openapi.json -o ./docs ``` ## Generated endpoints All adapters serve 4 endpoints (paths configurable via `routes` option): | Route | Content-Type | Description | |-------|-------------|-------------| | GET / | text/html | Semantic HTML landing page | | GET /llms.txt | text/plain | AI-optimized compact documentation | | GET /to-humans.md | text/plain | Full markdown reference | | GET /openapi.json | application/json | Raw OpenAPI specification | ## Compact notation conventions The llms.txt format uses these conventions to minimize token usage: - `*` after field name = required field - `:type` after field name = non-string type (e.g. `age:number`, `active:boolean`) - `{field1*, field2:number}` = object shape with field types - Auth shorthands: `JWT` (Bearer token), `KEY` (API key), `NONE` (no auth) - Common errors listed once as conventions, not repeated per endpoint - Schemas inlined at point of use, not referenced separately ### Example llms.txt output ``` # Pet Store API > A sample API for managing pets and orders. Base: https://api.petstore.io Docs: [HTML](/) | [OpenAPI JSON](/openapi.json) ## Auth Methods - JWT: `Authorization: Bearer ` via POST /auth/login - API Key: `X-API-Key: ` header ## Conventions - Auth: JWT = Bearer token, KEY = API Key, NONE = no auth - `*` after field name = required, all fields string unless noted - Common errors: 400/401/404 return `{success:false, error}` --- ## Auth ### POST /auth/login - Login | NONE Body: `{email*, password*}` 200: `{token, expiresIn:number}` ## Pets ### GET /pets - List pets | JWT Query: ?page:integer ?limit:integer ?species 200: `{data:[{id, name, species, age:number}], total:number}` ### POST /pets - Create pet | JWT Body: `{name*, species*, age:number, vaccinated:boolean}` 200: `{id, name}` ``` ## Configuration options ```typescript interface SwagentOptions { baseUrl: string; // Base URL shown in docs routes?: { // Customize or disable routes landing?: string | false; // default: '/' llmsTxt?: string | false; // default: '/llms.txt' humanDocs?: string | false; // default: '/to-humans.md' openapi?: string | false; // default: '/openapi.json' }; } ``` ## Token savings comparison | Format | Typical size | Tokens (~) | |--------|-------------|------------| | Raw OpenAPI JSON | 65 KB | ~16,000 | | SWAGENT llms.txt | 16 KB | ~4,000 | | Savings | ~75% | ~75% | ## Links - Website: https://swagent.dev - GitHub: https://github.com/X24Labs/SWAGENT - npm (core): https://www.npmjs.com/package/@swagent/core - npm (fastify): https://www.npmjs.com/package/@swagent/fastify - npm (express): https://www.npmjs.com/package/@swagent/express - npm (hono): https://www.npmjs.com/package/@swagent/hono - npm (cli): https://www.npmjs.com/package/swagent