{"record":{"id":"a27bc1ee75cd39b4","repo":"grafana/k6","slug":"the-rest-api-server-is-disabled-but-this-command","errorCode":null,"errorMessage":"The REST API server is disabled, but this command needs to read metrics from it. Enable it by setting --address or K6_ADDRESS.","messagePattern":"The REST API server is disabled, but this command needs to read metrics from it\\. Enable it by setting --address or K6_ADDRESS\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/stats.go","lineNumber":24,"sourceCode":"\t\"github.com/spf13/cobra\"\n\n\t\"go.k6.io/k6/v2/api/v1/client\"\n\t\"go.k6.io/k6/v2/cmd/state\"\n)\n\nfunc getCmdStats(gs *state.GlobalState) *cobra.Command {\n\t// statsCmd represents the stats command\n\tstatsCmd := &cobra.Command{\n\t\tUse:    \"stats\",\n\t\tShort:  \"Show test metrics\",\n\t\tHidden: true,\n\t\tLong: `Show test metrics.\n\n  Use the global --address flag or the K6_ADDRESS environment variable to specify\n  the URL to the API server.`,\n\t\tRunE: func(_ *cobra.Command, _ []string) error {\n\t\t\tif gs.Flags.Address == \"\" {\n\t\t\t\treturn errors.New(\"The REST API server is disabled, but this command needs\" + //nolint:staticcheck\n\t\t\t\t\t\" to read metrics from it. Enable it by setting --address or K6_ADDRESS.\")\n\t\t\t}\n\t\t\tc, err := client.New(gs.Flags.Address)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tmetrics, err := c.Metrics(gs.Ctx)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\treturn yamlPrint(gs.Stdout, metrics)\n\t\t},\n\t}\n\treturn statsCmd\n}\n","sourceCodeStart":6,"sourceCodeEnd":41,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cmd/stats.go#L6-L41","documentation":"The hidden `k6 stats` command shows live test metrics by querying the k6 REST API of a running test. If gs.Flags.Address is empty no API server was configured, so it aborts before creating the client instead of failing on connection.","triggerScenarios":"Running `k6 stats` with neither --address nor K6_ADDRESS set; also when the target test was itself started without `--address`, nothing listens to query.","commonSituations":"Assuming `k6 stats` reads a local results file rather than a live REST endpoint; forgetting the original run must be started with `--address localhost:6565` (or K6_ADDRESS).","solutions":["Start the monitored run with the API enabled: `k6 run --address localhost:6565 script.js`","Then query it: `k6 stats --address localhost:6565` (K6_ADDRESS works too)","If the run is over, use the end-of-test summary or an output file instead of this live command"],"exampleFix":"# before\nk6 stats\n\n# after (terminal 1, then terminal 2)\nk6 run --address localhost:6565 script.js\nk6 stats --address localhost:6565","handlingStrategy":"validation","validationCode":"k6_stats_or_fail() {\n  [ -n \"${K6_ADDRESS:-}\" ] || { echo 'Set --address / K6_ADDRESS (and start the run with it) first' >&2; return 1; }\n  k6 stats \"$@\"\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Start long-running tests with a fixed `--address localhost:6565` so stats/status/metrics always work","Prefer the summary or `--out json` when the run is already finished — k6 stats is live-only","Remember stats is a hidden REST-API client, not a file reader"],"tags":["cli","rest-api","stats","address"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}