Offline API Docs
#1035
|
What is the simplest way to bundle the API documentation UI (e.g. Stoplight Elements) in the built huma application binary, or at least make it easy to serve the docs UI offline? A quick look at the code shows that the builtin API docs use hardcoded CDN URLs. |
Answered by
wolveix
May 30, 2026
Replies: 1 comment 1 reply
|
Hi @akhayyat. The simplest way to do this would be to disable the built-in docs renderers and set your own route, e.g.: config := huma.DefaultConfig("Your App", version)
config.DocsPath = ""
router = humachi.New(mux, config)
router.Adapter().Handle(&huma.Operation{
Method: http.MethodGet,
Path: server.apiPath + "/docs",
}, func(ctx huma.Context) {
ctx.SetHeader("Content-Type", "text/html")
if _, err := ctx.BodyWriter().Write([]byte(`<!doctype html><html lang="en"><head><title>Your App - API Reference</title><meta charset="utf-8"><meta content="width=device-width,initial-scale=1" name="viewport"></head><body><script data-url="` + server.apiPath + `/docs.json" id="api-reference"></script><script>let apiReference = document.getElementById("api-reference")</script><script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script></body></html>`)); err != nil {
server.logger.Error().Err(err).Msg("an error occurred while serving the API documentation")
}
})Then just swap out the CDN references with bundled assets, using Go's built-in embed functionality. |
1 reply
Answer selected by
wolveix
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @akhayyat. The simplest way to do this would be to disable the built-in docs renderers and set your own route, e.g.: