Go Client Generation#

Scope: advanced template API surface. This guide consumes REST/RPC endpoints generated by the advanced template — it is not a client for the pgmi deployment CLI.

Generate Go types and client code from your deployment’s /openapi.json. Download openapi.json with session handles first; see Client generation .

oapi-codegen#

The standard Go OpenAPI code generator. Produces types, client, and server stubs.

go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latest

# Generate types + client
oapi-codegen -generate types,client \
  -package api \
  -o api/client.gen.go \
  openapi.json

Usage:

package main

import (
    "context"
    "fmt"
    "net/http"
    "yourproject/api"
)

func main() {
    client, _ := api.NewClientWithResponses("http://localhost:8080/api",
        api.WithRequestEditorFn(func(_ context.Context, req *http.Request) error {
            req.Header.Set("Cookie", "__Host-session="+loginSession+"; __Host-csrf="+csrfToken)
            req.Header.Set("X-CSRF-Token", csrfToken)
            req.Header.Set("x-organization-session", organizationSession)
            return nil
        }))
    resp, _ := client.HelloWorldWithResponse(context.Background(), &api.HelloWorldParams{
        Name: ptr("World"),
    })
    fmt.Println(resp.JSON200.Message)
}

func ptr[T any](v T) *T { return &v }

go generate#

Keep the downloaded openapi.json next to a generate.go file, so go generate ./... regenerates the client from it:

//go:generate oapi-codegen -generate types,client -package api -o client.gen.go openapi.json
package api