{"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/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/cmd/stats.go#L6-L41","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Start the test with the API enabled: `k6 run --address localhost:6565 script.js`.","Then query it: `k6 stats --address localhost:6565` (or export K6_ADDRESS=localhost:6565).","For machine-readable live data, consider `--out json` or the web dashboard instead of the legacy REST commands."],"exampleFix":"# before\nk6 run script.js\nk6 stats   # -> error\n# after\nk6 run --address localhost:6565 script.js &\nk6 stats --address localhost:6565","handlingStrategy":"validation","validationCode":"# wrapper for k6 stats: require an address\n: \"${K6_ADDRESS:?$0 needs --address or K6_ADDRESS pointing at a running k6 REST API}\"\nk6 stats --address \"$K6_ADDRESS\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Start every long-running k6 process you intend to control with an explicit --address.","Prefer the web dashboard or structured outputs for monitoring; REST control commands are hidden/legacy."],"tags":["rest-api","cli","address","observability"],"backgroundTag":"api-server-not-enabled","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}