grafana/k6 · error

failed to get a reference ID

Error message

failed to get a reference ID

What it means

cloudapi.Client.CreateTestRun (cloudapi/api.go) submits the test-run creation request; the call itself succeeded (2xx, JSON decoded), but the response contained no reference_id, so there is no cloud test run identifier to associate metrics with, and the client refuses to continue. It means the endpoint answered with an unexpected success payload - not that the request was rejected.

Source

Thrown at cloudapi/api.go:111

func (c *Client) CreateTestRun(testRun *TestRun) (*CreateTestRunResponse, error) {
	url := fmt.Sprintf("%s/tests", c.baseURL)

	// Because the kind of request we make can vary depending on the test run configuration, we delegate
	// its creation to a helper.
	request, err := c.makeCreateTestRunRequest(url, testRun)
	if err != nil {
		return nil, err
	}

	response := CreateTestRunResponse{}
	err = c.Do(request, &response)
	if err != nil {
		return nil, err
	}

	c.handleLogEntriesFromCloud(response)
	if response.ReferenceID == "" {
		return nil, fmt.Errorf("failed to get a reference ID")
	}

	return &response, nil
}

// makeCreateTestRunRequest creates a new HTTP request for creating a test run.
//
// If the test run archive isn't set, the request will be a regular JSON request with the test run information.
// Otherwise, the request will be a multipart form request containing the test run information and the archive file.
func (c *Client) makeCreateTestRunRequest(url string, testRun *TestRun) (*http.Request, error) {
	// If the test run archive isn't set, we are not uploading an archive and can use the regular request JSON format.
	if testRun.Archive == nil {
		return c.NewRequest(http.MethodPost, url, testRun)
	}

	// Otherwise, we need to create a multipart form request containing the test run information as
	// well as the archive file.
	fields := [][2]string{

View on GitHub (pinned to 93accf6570)

Solutions

  1. Make sure K6_CLOUD_HOST matches the environment the token belongs to (Grafana Cloud k6 uses the host printed by 'k6 cloud login' - re-run it)
  2. Update k6 to the latest release so the create-test-run request/response contract matches the backend
  3. Reproduce the call with curl -v against the same host and inspect the response body for reference_id
  4. Check HTTP(S)_PROXY settings - an intercepting proxy may be returning its own 2xx response

Example fix

# before - host/token from different environments
export K6_CLOUD_HOST=https://api.old-cloud.example
k6 cloud run script.js   # fails: failed to get a reference ID

# after - log in so host and token are consistent
k6 cloud login -t $K6_CLOUD_TOKEN   # or set the token via env
k6 cloud run script.js
Defensive patterns

Strategy: validation

Validate before calling

# before a cloud run, confirm the host/token pair yields a test run with an ID
curl -sS -X POST "$K6_CLOUD_HOST/v1/test-runs" \
  -H "Authorization: Token $K6_CLOUD_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{"name":"probe"}' | grep -q reference_id \
  || echo 'host/token mismatch: response has no reference_id'

Prevention

When it happens

Trigger: POST to the create-test-run endpoint under K6_CLOUD_HOST returning 2xx JSON without reference_id: a token valid for a different cloud environment than the configured host, a backend API version the k6 build does not speak, or an intermediary (proxy, gateway) returning its own 2xx page for the request.

Common situations: K6_CLOUD_HOST mismatched with the token (e.g. pointing at a legacy loadImpact host with a Grafana Cloud token or vice versa); a very old k6 binary against a newer backend; a reverse proxy or mock server in front of the API that swallows the real response body.

Related errors


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