Images or other encoded or binary responses can be returned by simply using a []byte body and providing some additional information at operation registration time, such as the response body content type.
packagemainimport("context""fmt""net/http""github.com/danielgtaylor/huma/v2""github.com/danielgtaylor/huma/v2/adapters/humachi""github.com/danielgtaylor/huma/v2/humacli""github.com/go-chi/chi/v5")// Options for the CLI.typeOptionsstruct{Portint`help:"Port to listen on" short:"p" default:"8888"`}// ImageOutput represents the image operation response.typeImageOutputstruct{ContentTypestring`header:"Content-Type"`Body[]byte}funcmain(){// Create a CLI app which takes a port option.cli:=humacli.New(func(hookshumacli.Hooks,options*Options){// Create a new router & APIrouter:=chi.NewMux()api:=humachi.New(router,huma.DefaultConfig("My API","1.0.0"))// Register GET /imagehuma.Register(api,huma.Operation{OperationID:"get-image",Summary:"Get an image",Method:http.MethodGet,Path:"/image",Responses:map[string]*huma.Response{"200":{Description:"Image response",Content:map[string]*huma.MediaType{"image/jpeg":{},},},},},func(ctxcontext.Context,input*struct{})(*ImageOutput,error){resp:=&ImageOutput{}resp.ContentType="image/png"resp.Body=[]byte{/* ... image bytes here ... */}returnresp,nil})// Tell the CLI how to start your server.hooks.OnStart(func(){fmt.Printf("Starting server on port %d...\n",options.Port)http.ListenAndServe(fmt.Sprintf(":%d",options.Port),router)})})// Run the CLI. When passed no commands, it starts the server.cli.Run()}