🧾 apigen – API Documentation Generator
The karma/apigen package helps you auto-generate OpenAPI-style REST documentation directly from Go structs. It supports dynamic path params, field overrides, global variables, reusable request/response bodies, and inline headers.
It’s ideal for documenting internal or external APIs with zero OpenAPI spec learning curve.
🚀 Quick Start
📦 Installation
✅ Create a New API Definition
🌐 Add Global Variables
These are available as$variables in your documentation templates:
🔁 Request and Response Bodies
🧱 Generate Request Body from Struct
Use
FieldOverride to provide examples, override descriptions, or exclude specific fields.Example override:
📦 Generate Response from Struct
🧩 Define Endpoints
UseAddEndpoint to declare a REST endpoint with method, path, headers, params, and expected responses.
Example – POST /projects/{project_id}/issues
🔐 Authentication Example: Auth APIs
Example – POST /auth/login
📄 Export Documentation
./docs/gitlab_issues.md or .html depending on config).
🧠 Tips
PathParamscan be defined explicitly or inferred from{}in path strings.- Headers are centralized via
KarmaHeadersconstants. - Use
FieldOverride.Excludeto hide internal struct fields (e.g.,TableName,Id,CreatedAt). - Combine multiple
APIDefinitionblocks for different modules (e.g. Auth, Admin, Public APIs).
📦 Constants Reference
apigen.ContentTypeJSON:"application/json"apigen.HeaderAccept:"Accept"apigen.HeaderContentType:"Content-Type"apigen.HeaderPrivateToken:"PRIVATE-TOKEN"
🧪 Real Example Output
The example generates documentation for:GET /projects/{project_id}/issuesPOST /projects/{project_id}/issuesGET /projects/{project_id}/issues/{issue_id}POST /auth/loginPOST /auth/verify_otpPOST /auth/register