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 mirror case of the stack guard in `k6 cloud login`: when non-interactive input is detected (stack was provided) but the token input is invalid or empty, login refuses to proceed. A stack without a token cannot authenticate, and in non-interactive mode there is nothing to prompt for.

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 93accf6570)

Solutions

  1. Supply both values: `k6 cloud login --token <TOKEN> --stack <slug-or-URL>`
  2. Verify the token variable is actually populated in the environment (`test -n "$K6_CLOUD_TOKEN"`)
  3. Use the interactive `k6 cloud login` form, which validates the token is non-empty before submitting

Example fix

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

# after
k6 cloud login --token "$K6_CLOUD_TOKEN" --stack my-team
test -n "$K6_CLOUD_TOKEN" || { echo "token missing"; exit 1; }  # guard in scripts
Defensive patterns

Strategy: validation

Validate before calling

# assert the secret is present and non-empty before login
test -n "$K6_CLOUD_TOKEN" || { echo "K6_CLOUD_TOKEN empty or unset" >&2; exit 1; }
k6 cloud login --token "$K6_CLOUD_TOKEN" --stack "$K6_CLOUD_STACK"

Prevention

When it happens

Trigger: `k6 cloud login --stack my-team` without --token; or a --token flag that resolves to an empty string (empty environment variable interpolated into the command).

Common situations: CI job exports the stack but the token secret was not mounted/injected, so $K6_CLOUD_TOKEN expands to empty; token pasted with only whitespace; shell quoting mistakes that swallow the value.

Related errors


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