OPEN SWAG GO
API Reference

Core Package

The root openswag package contains every type you need to define an OpenAPI spec and serve interactive documentation from your Go application.

Config

Top-level configuration passed to openswag.New().

type Config struct {
    Info     Info       // API metadata (title, version, description)
    Servers  []Server   // Server URLs shown in the docs UI
    Tags     []Tag      // Logical groupings for endpoints
    UI       UIConfig   // Scalar UI appearance settings
    Auth     AuthConfig // Security schemes exposed under components.securitySchemes
    DocsAuth *DocsAuth  // Optional password/API-key protection for the docs page
}

The mount path (e.g. /docs) is passed to docs.Mount(mux, "/docs"), not configured on Config.

Fields

FieldTypeDescription
InfoInfoAPI title, version, description, contact, and license
Servers[]ServerBase URLs displayed in the server dropdown
Tags[]TagGrouping tags that organise endpoints in the sidebar
UIUIConfigTheme, layout, and Scalar UI options
AuthAuthConfigSecurity schemes exposed under components.securitySchemes
DocsAuth*DocsAuthProtect the docs page with basic auth or an API key

Docs

Docs is the main handle returned by openswag.New(). It holds the compiled spec and serves the UI.

func New(cfg Config) *Docs
type Docs struct {
    // unexported fields
}
 
func (d *Docs) Add(endpoint Endpoint)                       // Register one endpoint
func (d *Docs) AddAll(endpoints ...Endpoint)                // Register multiple endpoints
func (d *Docs) Mount(mux *http.ServeMux, basePath string)   // Serve UI + spec on a ServeMux
func (d *Docs) Handler() http.HandlerFunc                   // Handler that serves the docs UI
func (d *Docs) SpecHandler() http.HandlerFunc               // Handler that serves the raw OpenAPI JSON
func (d *Docs) SpecJSON() ([]byte, error)                   // The OpenAPI spec as JSON bytes
func (d *Docs) BuildSpec() *spec.OpenAPI                    // The compiled spec object
func (d *Docs) GetUIConfig() (string, error)                // Serialized UI config (advanced)

Usage

docs := openswag.New(cfg)
docs.AddAll(endpoints...)
 
// Serve with net/http
mux := http.NewServeMux()
docs.Mount(mux, "/docs")
http.ListenAndServe(":8080", mux)

Endpoint

Describes a single API operation.

type Endpoint struct {
    Method      string            // HTTP method: GET, POST, PUT, PATCH, DELETE
    Path        string            // URL path, e.g. "/users/{id}"
    Summary     string            // Short one-line description
    Description string            // Detailed explanation (Markdown supported)
    Tags        []string          // Grouping tags for the sidebar
    Parameters  []Parameter       // Path, query, and header parameters (manual)
    QueryParams interface{}       // Struct whose fields become query params (form/description/example tags)
    PathParams  interface{}       // Struct whose fields become path params (param tag)
    RequestBody *RequestBody      // Request body (nil for GET/DELETE)
    Responses   Responses         // map[int]ResponseSpec keyed by HTTP status
    Security    []string          // Security scheme names applied to this endpoint
    Deprecated  bool              // Mark the endpoint as deprecated
}
 
// Responses is a convenience alias used in endpoint literals.
type Responses = map[int]ResponseSpec

Fields

FieldTypeDescription
MethodstringHTTP verb — GET, POST, PUT, PATCH, DELETE
PathstringRoute path with {param} placeholders
SummarystringOne-line summary shown in the sidebar
DescriptionstringLonger description, supports Markdown
Tags[]stringGroups the endpoint under one or more sidebar sections
Parameters[]ParameterManually declared path, query, and header parameters
QueryParamsinterface{}A struct whose fields (with form tags) become query parameters
PathParamsinterface{}A struct whose fields (with param tags) become path parameters
RequestBody*RequestBodyPayload definition for write operations — build with Body() / BodyWithDesc() / FormBody()
ResponsesResponsesStatus-code map — build entries with Response(desc, schema)
Security[]stringNames of security schemes this endpoint requires
DeprecatedboolFlags the endpoint as deprecated in the spec

Parameter

Describes a single path, query, or header parameter.

type Parameter struct {
    Name        string       // Parameter name, e.g. "id"
    In          string       // Location: "path", "query", "header", or "cookie"
    Description string       // Human-readable description
    Required    bool         // Whether the parameter is mandatory
    Schema      *spec.Schema // Parameter schema (e.g. spec.NewSchema("string"))
    Example     interface{}  // Optional example value
}

RequestBody

Describes the payload for POST, PUT, and PATCH operations.

type RequestBody struct {
    Description string      // What the body represents
    ContentType string      // MIME type, e.g. "application/json", "multipart/form-data"
    Required    bool        // Whether the body is mandatory
    Schema      interface{} // Go struct for schema generation
}

ContentType is honored end-to-end — set it to multipart/form-data for file uploads and the generated spec will list that media type instead of application/json. Prefer the Body() / FormBody() / FormURLEncodedBody() helpers above when you don't need custom fields.

In v1.1.0+, request body schemas automatically include a generated example field built from struct field tags and type defaults. Example values are type-coerced (e.g. example:"42" on an int64 field produces the integer 42, not the string "42").

ResponseSpec & Response()

ResponseSpec is the struct stored in Endpoint.Responses. Build entries with the Response() helper for the concise, decorator-style form documented in the README:

type ResponseSpec struct {
    Description string      // What this response means
    Schema      interface{} // Go struct for schema generation (optional)
}
 
// Response builds a ResponseSpec. Pass the schema as the second argument,
// or omit it for empty responses (e.g. 204 No Content).
func Response(description string, schema ...interface{}) ResponseSpec

Responses are keyed by status code in the Endpoint.Responses map:

Responses: openswag.Responses{
    200: openswag.Response("A list of pets", []Pet{}),
    204: openswag.Response("Deleted"),
    404: openswag.Response("Not found", ErrorResponse{}),
},

The struct used to be named Response. v1.2.0 renamed it to ResponseSpec so that openswag.Response(...) could become a constructor function. Field literals still work — just replace the type name.

Request body helpers

*RequestBody literals are verbose. Use these constructors:

// JSON body (Required: true, ContentType: "application/json")
openswag.Body(CreateUserRequest{})
 
// JSON body with a description
openswag.BodyWithDesc("Pet object to create", Pet{})
 
// multipart/form-data — for file uploads / HTML forms
openswag.FormBody(UploadRequest{})
 
// application/x-www-form-urlencoded
openswag.FormURLEncodedBody(LoginForm{})

All four return *RequestBody, so they slot directly into Endpoint.RequestBody.

Parameter helpers

Decorator-style helpers that return a Parameter ready to drop into Endpoint.Parameters:

openswag.PathParam("id", "User ID")                          // required path string
openswag.QueryParam("include", "Related resources")           // optional query string
openswag.RequiredQueryParam("filter", "Required filter")      // required query string
openswag.HeaderParam("X-Request-ID", "Request tracking ID")   // optional header
openswag.CookieParam("session", "Session cookie")             // optional cookie

All produce a string-typed schema. For typed query/path params, prefer the struct-based QueryParams / PathParams form below.

AuthConfig

Top-level container for the security schemes your spec advertises.

type AuthConfig struct {
    PersistCredentials bool         // Keep try-it credentials between page reloads
    Schemes            []AuthScheme // Schemes exposed under components.securitySchemes
}
 
type AuthScheme struct {
    Name         string // ID referenced from Endpoint.Security
    Type         string // "http", "apiKey", "oauth2", "openIdConnect"
    Scheme       string // "bearer" / "basic" when Type == "http"
    BearerFormat string // Hint for http+bearer (e.g. "JWT")
    In           string // "header" / "query" / "cookie" when Type == "apiKey"
    ParamName    string // Header/query/cookie name carrying the credential
    Description  string
}

Build entries with the helpers:

Auth: openswag.AuthConfig{
    PersistCredentials: true,
    Schemes: []openswag.AuthScheme{
        openswag.BearerAuth(openswag.SecurityBearerAuth),
        openswag.APIKeyAuth("apiKey", "X-API-Key"),
        openswag.BasicAuth(openswag.SecurityBasicAuth),
        openswag.CookieAuth(openswag.SecurityCookieAuth, "session_id"),
    },
},

Custom scheme names referenced from Endpoint.Security that are not one of the Security* predefined constants and not registered in AuthConfig.Schemes are intentionally left out of the spec — prior versions silently materialised them as http-bearer, which masked typos. Register the scheme explicitly to keep the previous behavior.

Predefined security names

Use these constants in Endpoint.Security to pick up the built-in spec entries without supplying an AuthScheme:

ConstantResolves to
SecurityBearerAuthhttp + bearer + JWT
SecurityBasicAuthhttp + basic
SecurityApiKeyapiKey in X-API-Key header
SecurityApiKeyQueryapiKey in ?api_key= query
SecurityCookieAuthapiKey in session_id cookie
SecurityOAuth2oauth2 authorization code

UIConfig

Controls the Scalar-based documentation UI appearance.

type UIConfig struct {
    Theme              string // Predefined theme: "purple", "blue", "green", "light"
    DarkMode           bool   // Enable dark mode by default
    Layout             string // Layout style, e.g. "modern"
    ShowSidebar        bool   // Show the navigation sidebar
    SidebarSearch      bool   // Enable the sidebar search box
    TagGrouping        bool   // Group endpoints by tag
    CollapsibleSchemas bool   // Render schemas as collapsible sections
    CustomCSS          string // Raw CSS injected into the docs page
}

DocsAuth

Protects the documentation page with credentials.

type DocsAuth struct {
    Enabled  bool   // Turn protection on
    Username string // Basic auth username
    Password string // Basic auth password
    Realm    string // Basic auth realm (optional)
    APIKey   string // Alternative: protect with an API key (?key=...)
}

When both Username/Password and APIKey are set, basic auth takes precedence.

Info

API metadata rendered in the docs header.

type Info struct {
    Title          string   // API title
    Version        string   // Semantic version, e.g. "1.0.0"
    Description    string   // Markdown description
    TermsOfService string   // URL to terms of service
    Contact        *Contact // Maintainer contact info
    License        *License // License details
}

Server

A base URL entry shown in the server dropdown.

type Server struct {
    URL         string // Base URL, e.g. "https://api.example.com"
    Description string // Label for this server
}

Tag

A logical grouping for endpoints.

type Tag struct {
    Name        string // Tag name referenced by Endpoint.Tags
    Description string // Displayed in the sidebar
}

Contact

Maintainer contact information embedded in Info.

type Contact struct {
    Name  string // Contact name
    URL   string // Contact URL
    Email string // Contact email
}

License

License details embedded in Info.

type License struct {
    Name string // License name, e.g. "MIT"
    URL  string // URL to the full license text
}

Parameters from structs

Instead of declaring Parameters by hand, set Endpoint.QueryParams or Endpoint.PathParams to a struct value. Query-param fields use the form tag; path-param fields use the param tag. The description and example tags annotate each parameter.

type ListProductsQuery struct {
    Page     int    `form:"page" description:"Page number" example:"1"`
    PerPage  int    `form:"per_page" description:"Items per page" example:"20"`
    Category string `form:"category" description:"Filter by category ID"`
}
 
type ProductPathParams struct {
    ID string `param:"id" description:"Product ID" example:"prod_123"`
}
 
openswag.Endpoint{
    Method:      "GET",
    Path:        "/products/{id}",
    PathParams:  ProductPathParams{},
    QueryParams: ListProductsQuery{},
    // ...
}

For full control you can still declare Parameters manually with spec.NewSchema:

import "github.com/gopackx/open-swag-go/pkg/spec"
 
Parameters: []openswag.Parameter{
    {Name: "id", In: "path", Description: "User ID", Required: true, Schema: spec.NewSchema("string")},
},

Quick Example

package main
 
import (
    "net/http"
    openswag "github.com/gopackx/open-swag-go"
)
 
func main() {
    cfg := openswag.Config{
        Info: openswag.Info{
            Title:   "Pet Store",
            Version: "1.0.0",
        },
        Servers: []openswag.Server{
            {URL: "http://localhost:8080", Description: "Local"},
        },
    }
 
    endpoints := []openswag.Endpoint{
        {
            Method:  "GET",
            Path:    "/pets",
            Summary: "List all pets",
            Tags:    []string{"Pets"},
            Responses: openswag.Responses{
                200: openswag.Response("A list of pets"),
            },
        },
    }
 
    docs := openswag.New(cfg)
 
 
    docs.AddAll(endpoints...)
    http.Handle("/docs/", docs.Handler())
    http.ListenAndServe(":8080", nil)
}