grafana/k6 · error

The REST API server is disabled, but this command needs to…

Error message

The REST API server is disabled, but this command needs to read metrics from it. Enable it by setting --address or K6_ADDRESS.

What it means

The hidden `k6 stats` command (internal/cmd/stats.go:24) reads live aggregated metrics from the REST API of a running k6 process. If gs.Flags.Address is empty, the REST API is disabled, so the command refuses to run instead of failing on connection. The fix is to point it at an API server started with --address/K6_ADDRESS.

Solutions

  1. Start the test with the API enabled: `k6 run --address localhost:6565 script.js`.
  2. Then query it: `k6 stats --address localhost:6565` (or export K6_ADDRESS=localhost:6565).
  3. For machine-readable live data, consider `--out json` or the web dashboard instead of the legacy REST commands.

Example fix

# before
k6 run script.js
k6 stats   # -> error
# after
k6 run --address localhost:6565 script.js &
k6 stats --address localhost:6565
Defensive patterns

Strategy: validation

Validate before calling

# wrapper for k6 stats: require an address
: "${K6_ADDRESS:?$0 needs --address or K6_ADDRESS pointing at a running k6 REST API}"
k6 stats --address "$K6_ADDRESS"

Prevention

When it happens

Trigger: Running `k6 stats` with no --address flag and no K6_ADDRESS env var; or the target `k6 run` was started with the API disabled (default), so there is nothing to query.

Common situations: Operators scripting around a running test (stats/status/pause/scale) but forgetting to enable the API server on the runner with `--address localhost:6565` at start time.

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/a27bc1ee75cd39b4. Report an issue: GitHub.

Appendix: source

Thrown at internal/cmd/stats.go:24

	"github.com/spf13/cobra"

	"go.k6.io/k6/v2/api/v1/client"
	"go.k6.io/k6/v2/cmd/state"
)

func getCmdStats(gs *state.GlobalState) *cobra.Command {
	// statsCmd represents the stats command
	statsCmd := &cobra.Command{
		Use:    "stats",
		Short:  "Show test metrics",
		Hidden: true,
		Long: `Show test metrics.

  Use the global --address flag or the K6_ADDRESS environment variable to specify
  the URL to the API server.`,
		RunE: func(_ *cobra.Command, _ []string) error {
			if gs.Flags.Address == "" {
				return errors.New("The REST API server is disabled, but this command needs" + //nolint:staticcheck
					" to read metrics from it. Enable it by setting --address or K6_ADDRESS.")
			}
			c, err := client.New(gs.Flags.Address)
			if err != nil {
				return err
			}
			metrics, err := c.Metrics(gs.Ctx)
			if err != nil {
				return err
			}

			return yamlPrint(gs.Stdout, metrics)
		},
	}
	return statsCmd
}

View on GitHub (pinned to 01ffac6f24)