{"record":{"id":"bd8d6ea3d31c19c2","repo":"grafana/k6","slug":"stack-url-is-required-to-validate-token","errorCode":null,"errorMessage":"stack URL is required to validate token","messagePattern":"stack URL is required to validate token","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cloudapi/v6/api.go","lineNumber":190,"sourceCode":"\n\tzones := make([]LoadZone, 0, len(res.Value))\n\tfor _, zone := range res.Value {\n\t\tzones = append(zones, LoadZone{\n\t\t\tID:           zone.Id,\n\t\t\tK6LoadZoneID: zone.K6LoadZoneId,\n\t\t\tName:         zone.Name,\n\t\t\tPublic:       zone.Public,\n\t\t\tAvailable:    zone.Available,\n\t\t})\n\t}\n\n\treturn zones, nil\n}\n\n// ValidateToken validates the cloud authentication token.\nfunc (c *Client) ValidateToken(ctx context.Context, stackURL string) (_ *k6cloud.AuthenticationResponse, err error) {\n\tif stackURL == \"\" {\n\t\treturn nil, errors.New(\"stack URL is required to validate token\")\n\t}\n\tif _, err := url.Parse(stackURL); err != nil {\n\t\treturn nil, fmt.Errorf(\"invalid stack URL: %w\", err)\n\t}\n\n\tres, hr, err := c.apiClient.AuthorizationAPI.\n\t\tAuth(c.authCtx(ctx)).\n\t\tXStackUrl(stackURL).\n\t\tExecute()\n\tdefer closeResponse(hr, &err)\n\n\tif err := CheckResponse(hr, err); err != nil {\n\t\treturn nil, err\n\t}\n\tif res == nil {\n\t\treturn nil, errUnknown\n\t}\n","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cloudapi/v6/api.go#L172-L208","documentation":"ValidateToken refuses to run when the stackURL argument is an empty string. Token validation must be scoped to a specific Grafana Cloud stack (the stack URL is sent as the X-Stack-Url header to the AuthorizationAPI.Auth call), so an empty stack URL cannot produce a meaningful result.","triggerScenarios":"Calling Client.ValidateToken(ctx, \"\") directly, or via CLI paths where stack resolution produced an empty string before the auth check (e.g. a stack slug/URL variable that was never set).","commonSituations":"Automation or scripts calling the client with an unset K6_CLOUD_STACK / stack variable; a wrapper that reads the stack from an environment variable that is empty in CI; calling `k6 cloud login` non-interactively with a missing stack value (a sibling guard in cloud_login.go catches that earlier).","solutions":["Pass a non-empty stack URL, e.g. https://my-team.grafana.net (a bare slug like my-team is accepted by url.Parse but the full URL is the reliable form)","If the value comes from an env variable, verify it is exported and non-empty in the executing shell/CI job","Default the parameter from your configuration before calling ValidateToken"],"exampleFix":"// before\nresp, err := client.ValidateToken(ctx, os.Getenv(\"K6_CLOUD_STACK\"))\n\n// after\nstackURL := os.Getenv(\"K6_CLOUD_STACK\")\nif stackURL == \"\" {\n    return errors.New(\"K6_CLOUD_STACK must be set before validating a token\")\n}\nresp, err := client.ValidateToken(ctx, stackURL)","handlingStrategy":"validation","validationCode":"stackURL := strings.TrimSpace(cfg.StackURL)\nif stackURL == \"\" {\n    return errors.New(\"stack URL missing: set K6_CLOUD_STACK or run `k6 cloud login` before validating a token\")\n}\nresp, err := client.ValidateToken(ctx, stackURL)","typeGuard":null,"tryCatchPattern":"if _, err := client.ValidateToken(ctx, stackURL); err != nil {\n    if strings.Contains(err.Error(), \"stack URL is required\") {\n        return fmt.Errorf(\"configuration error: %w\", err)\n    }\n    return err\n}","preventionTips":["Always source the stack URL from one validated config knob, never ad-hoc string params","Assert required env vars are non-empty in CI before invoking k6 cloud commands","Prefer `k6 cloud login --show` to confirm both token and stack are stored"],"tags":["cloud","auth","validation","stack","go"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}