grafana/k6 · error

stack URL is required to validate token

Error message

stack URL is required to validate token

What it means

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.

Source

Thrown at internal/cloudapi/v6/api.go:190

	zones := make([]LoadZone, 0, len(res.Value))
	for _, zone := range res.Value {
		zones = append(zones, LoadZone{
			ID:           zone.Id,
			K6LoadZoneID: zone.K6LoadZoneId,
			Name:         zone.Name,
			Public:       zone.Public,
			Available:    zone.Available,
		})
	}

	return zones, nil
}

// ValidateToken validates the cloud authentication token.
func (c *Client) ValidateToken(ctx context.Context, stackURL string) (_ *k6cloud.AuthenticationResponse, err error) {
	if stackURL == "" {
		return nil, errors.New("stack URL is required to validate token")
	}
	if _, err := url.Parse(stackURL); err != nil {
		return nil, fmt.Errorf("invalid stack URL: %w", err)
	}

	res, hr, err := c.apiClient.AuthorizationAPI.
		Auth(c.authCtx(ctx)).
		XStackUrl(stackURL).
		Execute()
	defer closeResponse(hr, &err)

	if err := CheckResponse(hr, err); err != nil {
		return nil, err
	}
	if res == nil {
		return nil, errUnknown
	}

View on GitHub (pinned to 93accf6570)

Solutions

  1. 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)
  2. If the value comes from an env variable, verify it is exported and non-empty in the executing shell/CI job
  3. Default the parameter from your configuration before calling ValidateToken

Example fix

// before
resp, err := client.ValidateToken(ctx, os.Getenv("K6_CLOUD_STACK"))

// after
stackURL := os.Getenv("K6_CLOUD_STACK")
if stackURL == "" {
    return errors.New("K6_CLOUD_STACK must be set before validating a token")
}
resp, err := client.ValidateToken(ctx, stackURL)
Defensive patterns

Strategy: validation

Validate before calling

stackURL := strings.TrimSpace(cfg.StackURL)
if stackURL == "" {
    return errors.New("stack URL missing: set K6_CLOUD_STACK or run `k6 cloud login` before validating a token")
}
resp, err := client.ValidateToken(ctx, stackURL)

Try / catch

if _, err := client.ValidateToken(ctx, stackURL); err != nil {
    if strings.Contains(err.Error(), "stack URL is required") {
        return fmt.Errorf("configuration error: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: 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).

Common situations: 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).

Related errors


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