Skip to main content

🧾 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

Use AddEndpoint 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

This generates markdown or HTML documentation in the specified folder (e.g., ./docs/gitlab_issues.md or .html depending on config).

🧠 Tips

  • PathParams can be defined explicitly or inferred from {} in path strings.
  • Headers are centralized via KarmaHeaders constants.
  • Use FieldOverride.Exclude to hide internal struct fields (e.g., TableName, Id, CreatedAt).
  • Combine multiple APIDefinition blocks 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}/issues
  • POST /projects/{project_id}/issues
  • GET /projects/{project_id}/issues/{issue_id}
  • POST /auth/login
  • POST /auth/verify_otp
  • POST /auth/register
All with automatic type inference, examples, field descriptions, and header control.