grafana/k6 · error

token value is required but it was not passed or is empty

Error message

token value is required but it was not passed or is empty

What it means

The token-side counterpart in `k6 cloud login`'s non-interactive branch: when -t/--token or --stack is passed, both are mandatory, and a missing or empty token value fails immediately at cloud_login.go:109 before any network call.

Solutions

  1. Pass the token too: `k6 cloud login -t <token> --stack my-team`
  2. Generate the token in Grafana Cloud (Access Policies / k6 tokens) if you don't have one
  3. In CI, assert the secret is non-empty before calling k6 cloud login
  4. Or use the interactive form (no flags) to be prompted for both

Example fix

# before
k6 cloud login --stack my-team          # -> token value is required

# after
k6 cloud login -t glc_ey... --stack my-team
Defensive patterns

Strategy: validation

Validate before calling

test -n "$K6_CLOUD_TOKEN" || { echo 'K6_CLOUD_TOKEN secret is empty'; exit 1; }
test -n "$K6_STACK" || { echo 'K6_STACK is empty'; exit 1; }
k6 cloud login -t "$K6_CLOUD_TOKEN" --stack "$K6_STACK"

Prevention

When it happens

Trigger: Running `k6 cloud login --stack my-team` without -t, or passing an empty token such as `-t ""` / `-t "$TOKEN"` with TOKEN unset; the branch triggers because stackInput is valid.

Common situations: Automation scripts referencing an unset or misnamed secret variable; pasting the stack first and forgetting the token; shell quoting that expands to an empty string.

Understand the failure class

Background: "--flag is required" and "must specify" CLI errors: how missing-required-flag validation works and how to fix it — this error's family across 20 libraries.

Related errors


AI-assisted analysis of grafana/k6@9cb32757e5 (2026-08-18). Data as JSON: /api/errors/1ab21553170d3875. Report an issue: GitHub.

Appendix: source

Thrown at internal/cmd/cloud_login.go:110

	tokenInput := getNullString(cmd.Flags(), "token")
	stackInput := getNullString(cmd.Flags(), "stack")

	switch {
	case reset.Valid:
		newCloudConf.Token = null.StringFromPtr(nil)
		newCloudConf.StackID = null.IntFromPtr(nil)
		newCloudConf.StackURL = null.StringFromPtr(nil)
		newCloudConf.DefaultProjectID = null.IntFromPtr(nil)
		printToStdout(c.globalState, "\nToken and stack info have been reset.\n")
	case show.Bool:
		printConfig(c.globalState, newCloudConf)
		return nil
	case tokenInput.Valid || stackInput.Valid:
		if !stackInput.Valid || stackInput.String == "" {
			return errors.New("stack value is required but it was not passed or is empty")
		}
		if !tokenInput.Valid || tokenInput.String == "" {
			return errors.New("token value is required but it was not passed or is empty")
		}
		err := authenticateUserToken(c.globalState, &newCloudConf, currentJSONConfigRaw, tokenInput.String, stackInput.String)
		if err != nil {
			return err
		}
	default:
		gs := c.globalState

		userinfo, err := promptUserAuthForm(gs)
		if err != nil {
			return err
		}

		err = authenticateUserToken(gs, &newCloudConf, currentJSONConfigRaw,
			userinfo.token, userinfo.stack)
		if err != nil {
			return err
		}

View on GitHub (pinned to 9cb32757e5)