Serves a RapiDoc API reference from a Go web app: an interactive page for your OpenAPI document, with a console to try requests.
go get github.com/rapi-doc/rapidoc-gopackage main
import (
"net/http"
rapidoc "github.com/rapi-doc/rapidoc-go"
)
func main() {
mux := http.NewServeMux()
docs := rapidoc.Handler(rapidoc.Options{SpecURL: "/openapi.json"})
mux.Handle("/docs", docs)
mux.Handle("/docs/", docs)
http.ListenAndServe(":8080", mux)
}Open /docs. Nothing is loaded from another site: the package carries the RapiDoc bundle, pinned to one release.
It is a plain net/http handler with no dependencies, so it works wherever one does.
/openapi.json in the example is a URL of your own app. Go has no built-in OpenAPI support, and this package shows a document; it does not create one. There are three usual sources:
A framework that generates it from your code. Huma builds the document from your typed handlers and serves it at /openapi.json; Fuego works the same way. Point SpecURL at the framework's document (Huma example below).
A tool that generates it from comments. swaggo/swag writes docs/swagger.json from comments above your handlers when you run swag init. Serve or embed that file as in the next case; RapiDoc reads Swagger 2.0 as well as OpenAPI 3.
A file you write (spec-first), often the input of a code generator such as oapi-codegen or ogen. Embed a JSON file and hand it over; it is written into the page, so no URL is needed:
//go:embed openapi.json
var spec []byte
docs := rapidoc.Handler(rapidoc.Options{Spec: spec})Spec takes JSON. For a YAML file, serve it and use SpecURL; RapiDoc reads YAML in the browser:
//go:embed openapi.yaml
var spec []byte
mux.HandleFunc("GET /openapi.yaml", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/yaml")
w.Write(spec)
})
docs := rapidoc.Handler(rapidoc.Options{SpecURL: "/openapi.yaml"})rapidoc.Handler(rapidoc.Options{
UIPath: "/reference",
SpecURL: "/openapi.json",
Title: "Pets API",
Attributes: map[string]any{"render-style": "focused", "allow-try": false},
})| Option | Default | |
|---|---|---|
UIPath |
/docs |
URL path the reference page is served at: the path you mount the handler on. / is the site root |
SpecURL |
URL of your OpenAPI document, which the browser fetches when the page opens. Use it when the app already serves the document | |
Spec |
The OpenAPI document itself: JSON as []byte, json.RawMessage or string, or a value that encoding/json marshals to it. It is written into the page, so the browser makes no request for it. With both, Spec is shown and SpecURL is the download link. YAML is not read here; serve it and use SpecURL |
|
Title |
API Reference |
Page title |
Assets |
rapidoc.Bundled |
Bundled: the app serves rapidoc-min.js. rapidoc.CDN: the browser loads the same version from jsDelivr, checked against its SRI hash. rapidoc.ScriptURL(url, integrity): a URL of your own |
Attributes |
Attributes of <rapi-doc>, in kebab-case or camelCase. They win over the package's own, and nil removes one |
|
Head |
HTML added to the end of <head>, as given |
|
CSS |
CSS added in a <style> element, as given |
|
Lang |
en |
Language of the page |
Nonce |
func(*http.Request) string that reads the request's Content-Security-Policy nonce, which is then set on every <script> and <style> |
|
PublicUIPath |
UIPath |
UIPath as the browser sees it, when http.StripPrefix or a reverse proxy removes a prefix before the request reaches the handler |
These are the options of every RapiDoc integration, in Go naming.
Handler panics on options that cannot be used (an attribute name that is not valid, a Spec that is not JSON), as http.ServeMux does on a pattern it cannot use. When the options come from outside the program, such as a spec read from a file at startup, check them first:
if err := options.Validate(); err != nil {
log.Printf("API docs disabled: %v", err)
} else {
mux.Handle("/docs/", rapidoc.Handler(options))
}Middleware(options) serves the reference and passes every other request to the next handler.
The handler matches the full request path, so mount it without stripping the prefix.
// chi
r.Mount("/docs", rapidoc.Handler(rapidoc.Options{SpecURL: "/openapi.json"}))
// Gin
docs := gin.WrapH(rapidoc.Handler(rapidoc.Options{SpecURL: "/openapi.json"}))
router.GET("/docs", docs)
router.GET("/docs/*file", docs)
// Echo
docs := echo.WrapHandler(rapidoc.Handler(rapidoc.Options{SpecURL: "/openapi.json"}))
e.GET("/docs", docs)
e.GET("/docs/*", docs)
// Fiber (github.com/gofiber/fiber/v2/middleware/adaptor)
app.Use("/docs", adaptor.HTTPHandler(rapidoc.Handler(rapidoc.Options{SpecURL: "/openapi.json"})))Huma generates the OpenAPI document and serves it at /openapi.json. Turn off its built-in docs page and mount RapiDoc on the router underneath:
router := chi.NewMux()
config := huma.DefaultConfig("Pets API", "1.0.0")
config.DocsPath = "" // RapiDoc serves /docs
api := humachi.New(router, config)
router.Mount("/docs", rapidoc.Handler(rapidoc.Options{
SpecURL: "/openapi.json",
Title: "Pets API",
}))- The handler answers
GETandHEADon the page,{UIPath}/oauth-receiver.html,{UIPath}/rapidoc-min.jsand its license file. Any other path is a 404 (or goes to the next handler, withMiddleware). - OAuth sign-in returns to
{UIPath}/oauth-receiver.html, served on your own origin, also withrapidoc.CDN. - The bundle is sent with an
ETag, andrapidoc-min.js.LICENSE.txtnext to it lists the libraries inside it. rapidoc.BundleVersion()is the RapiDoc release the package carries (the fileRAPIDOC_VERSION).- The page, its routes and its HTML follow the RapiDoc integration contract, which the integrations for JavaScript, Python and .NET also implement;
testdata/conformanceholds its fixtures.
Go 1.22 or later. MIT.
v0.3.2 ships rapidoc 14.1.0, which adds the amber theme (Attributes: map[string]any{"theme": "amber"}). Nothing else changes in Attributes or in what the page shows.
v0.3.1 ships rapidoc 14. Nothing changes in Attributes or in what the page shows: rapidoc 14 has no attribute changes (v13 to v14).
v0.3.0 ships rapidoc 13, which changed one set of attributes: filter-operations replaces match-paths, match-type and remove-endpoints-with-badge-label-as, and rapidoc 13 ignores those three. If your Attributes set any of them, set filter-operations instead. Matching is exact, so a v12 match-paths prefix becomes a glob: Attributes: map[string]any{"filter-operations": "/pets/**"}. The v12 to v13 migration guide has the other terms (tag:, badge:, deprecated, ! to leave out).
A release is a git tag; the Go module proxy picks it up, and there is no registry account or upload.
-
Choose the bundle. To ship a newer RapiDoc, run
scripts/sync.sh <version>(for examplescripts/sync.sh 12.3.0). It replacesassets/, the fixtures intestdata/conformance/andRAPIDOC_VERSION. Skip this for a release that only changes the Go code. -
Check.
go test ./... -
Merge to
mainand wait for the Test workflow. -
Tag and push:
git tag v0.1.1 git push origin v0.1.1
-
Verify.
go get github.com/rapi-doc/rapidoc-go@v0.1.1in a new module, and check https://pkg.go.dev/github.com/rapi-doc/rapidoc-go (the first request for a new version makes the proxy fetch it).
Which number to raise: a fix or a newer bundle is a patch (v0.1.1); a new option is a minor (v0.2.0). While the version is v0.x, a change that breaks existing code is also a minor, and is called out in the release notes. A tag cannot be moved or reused once the proxy has seen it, so fix a bad release with a new tag.