Grafana Cloud k6 — interaction reference
The default path is the gcx CLI. When gcx isn't installed, every
endpoint here is still reachable via direct curl against k6's public
hosts — see §1.2 for the auth-header and host-translation rules. Two
principles shape the rest:
- gcx owns Grafana-side auth (when present). It injects the right
headers on every call, so you should not set auth headers yourself.
The only header you ever set by hand is
X-K6TestRun-Id, on Loki log queries (§4), browser screenshot/file fetches (§6), and Tempo trace queries (§7) — anything else gets overwritten or causes conflicts. In curl mode the auth headers are manual; see §1.2. - Reach for
gcx k6 ...subcommands first. They wrap the common paths with friendlier ergonomics and handle pagination. Discover what's available withgcx help-tree k6(and drill in further withgcx help-tree k6 <subcommand>); fall back togcx apionly when no subcommand exists for what you need.
1. Authentication
1.1 With gcx (default)
Once a context is logged in, every gcx api ... and gcx k6 ... call
inherits its auth state. If a call returns "Invalid or expired token —
run gcx login to refresh", the OAuth session has lapsed — re-run
gcx login --context <ctx>.
1.2 Without gcx — direct curl
Check command -v gcx first. If it's missing, every endpoint in this
skill is still reachable directly against k6's public hosts — three
things change versus the gcx examples elsewhere:
- Auth headers are manual. Set both on every call:
Authorization: Bearer <k6_token>X-Stack-ID: <int>
- Hosts replace the plugin proxy.
- REST (
/cloud/v6/...,/cloud/v5/...,/cloud-resources/v1/...,/insights/...) →https://api.k6.io - Logs (Loki) and traces (Tempo) →
https://cloudlogs.k6.io
- REST (
- No
/api/plugins/k6-app/resources/{cloud,logs,insights}prefix. Drop it; everything after that prefix in the gcx examples is the real k6 path. The doubledcloud/cloud/quirk from §2 collapses to a single/cloud/— the first one was just the proxy route.
Obtaining the credentials
Don't guess these — prompt the user once per session for:
-
k6 API token — long-lived bearer; the same value
gcx k6 auth tokenwould print when gcx is configured. -
Stack — either the integer stack ID (used directly in
X-Stack-ID) or a Grafana stack URL (e.g.https://myorg.grafana.net). If the user supplies a URL, resolve it to an ID once withGET /cloud/v6/auth, which takes the URL in theX-Stack-Urlheader and returns{stack_id, default_project_id}:Cache the resolved ID for the session — every subsequent call needs it in
X-Stack-ID. (Note:/cloud/v6/authis the only endpoint that takesX-Stack-Urlinstead ofX-Stack-ID— it's how you cross the gap from "user-known URL" to "API-required ID".)
Translation cheat-sheet
In every -H ... slot above, send both auth headers:
-H "Authorization: Bearer $K6_TOKEN" -H "X-Stack-ID: $STACK_ID".
Notes:
- Endpoint-specific headers gcx leaves to you —
X-K6TestRun-Idon log, trace, and files endpoints (§4, §6, §7) — are still required in addition to the auth pair. - The
gcx apiflag quirks in §2 (spill envelope,--json fieldfiltering,-ofor output format) don't apply to curl. Use plain curl flags:-o fileto save body,--data-binary @filefor PUT payloads,-w '%{http_code}'for status code, etc. - Pagination semantics (
$orderby,$top,$skip,@nextLinkfrom §3) are properties of the v6 endpoints themselves and work identically over curl. The@nextLinkURL returned by the server is already an absolutehttps://api.k6.io/...URL — pass it back to curl unchanged; the plugin-proxy reshape in §3 isn't needed.
2. How gcx api paths are shaped
When no subcommand exists, fall back to gcx api against the Grafana
plugin-proxy routes:
- REST API (
/cloud/v6/,/cloud/v5/) — prefix with/api/plugins/k6-app/resources/cloud/<k6-path>. - Logs (Loki) — prefix with
/api/plugins/k6-app/resources/logs/<loki-path>.
Note the doubled cloud/cloud/ in every REST path — the first cloud
is the proxy route, the second is k6's /cloud/v{N}/ namespace.
gcx api flag quirks
gcx api is not a curl clone — a few flags differ from what curl muscle
memory suggests:
-
Response body is written to stdout. There is no
-o <file>flag for saving the body;-oselects output format (json,yaml,agents). Use shell redirection (> file) or$(...)capture instead. -
Request body uses
-d <string>,-d @file, or-d @-(stdin). There is no--data-binary;-d @filealready preserves bytes. -
Field selection without jq:
--json field1,field2,...returns only the listed fields, and--json list(or--json '?') discovers what's available. Often cleaner than piping into jq for shallow extractions. -
Stderr noise: gcx prints a one-line
hint:to stderr on most invocations. Pipelines intojqshould redirect with2>/dev/nullto avoid surprises. -
Response headers are not directly exposed by
gcx api. When a branch in the workflow hinges onContent-Type(e.g. script GET in §5), inspect the downloaded body withfile <path>instead. -
Large responses spill to a temp file, with stdout reduced to a wrapper envelope. The threshold is a few tens of KB, so even moderately-sized list endpoints trigger it. The envelope looks like this:
Naive
json.loads(stdout).get('value', [])patterns silently return empty in this case — the envelope has novaluekey. Two reliable fixes:- Pass
-o jsonto force inline output regardless of size (recommended for scripts that parse the body). The agent-mode default formatting is what triggers the spill;-o jsonopts out. - Or detect the envelope and re-read from the spilled file:
Either works;
-o jsonis fewer lines. - Pass
-
The Grafana plugin proxy rewrites
Content-Type: multipart/...toapplication/json.gcx apiitself forwards your-H "Content-Type: ..."header correctly (visible in--log-http-payloadtraces), but the upstream plugin proxy rewrites multipart Content-Types to JSON before they reach k6's API. The net effect: endpoints that require multipart bodies — notablyPOST /cloud/v6/projects/{id}/load_testsfor test creation, which takesname+scriptas form parts — returnHTTP 415 "Unsupported media type \"application/json\""no matter what header you set on the gcx side. Fall back to direct curl againstapi.k6.io(§1.2) for these endpoints —curl -F name=... -F script=@...builds the multipart body for you and bypasses the plugin proxy. Other Content-Type values (e.g.application/octet-streamfor the script-update PUT in §5) are forwarded through the proxy unchanged.
3. Discovering and calling endpoints
gcx k6 subcommands (see gcx help-tree k6) cover the common reads.
For everything else — mutations, niche reads, endpoints not surfaced as
subcommands — the k6 Cloud API surface is large and changes over time.
Rather than rely on a cheat-sheet that goes stale, discover the
operation you need from the OpenAPI spec at request time.
Workflow
-
Fetch the spec once per session (it's large; cache to
/tmp): -
Index by
operationId+descriptionfirst — this keeps the working payload small while you search:Grep this for the action you need ("abort", "limits", "schedule", …).
-
Pull the chosen operation's full schema — parameters, request body, response — resolving any
$refindirections: -
Build the
gcx apicall against the same path, prefixed per §2. Authorization is already injected by gcx — do not add it yourself.
List requests
When the operation you're calling is a list endpoint, default to
ordering by created descending (newest first) and paginating
by 20 — both via whatever query parameter names the OpenAPI schema
for that operation exposes. Names vary (ordering=-created,
order_by=created.desc, sort=-created, limit=20, page_size=20),
which is exactly why discovering them from the spec in step 3 matters.
Newest-first means the entries the user usually cares about land in
the first page; a page size of 20 keeps the response small enough to
summarise without burning context.
Full enumeration: paginating with @nextLink
When you need to enumerate all rows (not just the newest page), the
v6 list endpoints cap each response at 1000 rows (the default
$top) and return an @nextLink field pointing at the next page.
gcx k6 runs list --limit 0 does NOT auto-follow @nextLink and
defaults to ascending order — for tests with >1000 historical runs it
silently returns the oldest 1000, not the newest. The
practical effect: (first/last_run) summarised from this output can
be wildly stale (e.g. a daily-scheduled test that's been running for
years will show a last_run from ~3 years ago). Use gcx api
against the v6 endpoint with $orderby=created desc for the
newest-first slice, or loop on @nextLink until it's absent for full
enumeration.
The v6 list endpoints expose OData-style query parameters:
Two practical defaults for the loop: $orderby=created desc so the
first page contains the newest runs (most of the time the agent only
cares about those), and -o json to opt out of the spill envelope
(see §2) so the parsing is uniform across pages.
Same pattern works for any v6 list endpoint that returns @nextLink,
not just /test_runs. If you only need the few most-recent items,
stop the loop after the first page — with $orderby=created desc that
page is already the newest 1000.
Metrics
Time-series and aggregate metrics live on the v5 API
(/cloud/v5/...) — Prometheus-like query semantics inside OData-style
URL function-calls (query_range_k6(query='...',metric='...')), not
captured by the v6 OpenAPI. The full endpoint reference, selector
syntax, query methods per metric type, and worked examples are inlined
in references/metrics.md [blocked] — read that before
constructing a metrics query.
Logs
See §4.
4. Logs (Loki via gcx api)
The plugin proxies Loki under /api/plugins/k6-app/resources/logs/. The
only header you supply by hand is X-K6TestRun-Id; gcx handles
authorization.
Pull individual entries from /tmp/run_${RUN_ID}_logs.json as needed
(e.g. jq '.data.result[].values[]' …) rather than re-running the
query.
Every LogQL query must include the {test_run_id="<run_id>"} stream
selector — the plugin proxy partitions logs by run and rejects (or
returns nothing for) queries without it. Layer additional filters on
top of that selector:
{test_run_id="<run_id>"}— everything{test_run_id="<run_id>"} | level=~"(error|warn)"— errors and warnings only{test_run_id="<run_id>"} |= "specific text"— substring filter
Direction & window tips:
- Scope
start/endto the run'screated/endedfields, not to wall-clock "last hour" — otherwise queries silently miss anything older than the window. The only reason to deviate is when you specifically want a different interval (e.g. surrounding context). direction=forwardwithstart = run.createdto find the first errors.direction=backwardwithend = run.ended(or now, if still running) to find the most recent output.- Loki retention typically outlives k6's cascade-delete, so logs of a deleted child run may still be queryable for a while.
5. Editing a test script safely
Two distinct script endpoints
There are two GET endpoints that return a k6 script body, and they are not interchangeable:
The two can drift apart: if the load-test script is edited after a run completes, that run's bundled snapshot stays frozen at what it executed, while the load-test endpoint serves the new current version. The bundled-snapshot endpoint is GET-only — you cannot mutate historical bytes.
The safe-edit recipe below uses the load-test endpoint (the editable
one). For run-vs-run script diffs (e.g. "did the script change between
the last passing and first failing run?"), GET both runs'
/test_runs/<id>/script and diff them locally.
Script body format
The script GET endpoint returns one of two shapes — always detect which before assuming the format:
-
Single js/ts file. The recipe below handles this case directly.
-
k6 tar archive (plain tar or gzipped). A multi-file project bundle (entry script + imported modules + assets). To edit: extract the JS file, modify it, then use
k6 archiveto rebuild the archive from the modified JS. Do not manually repack withtar— the archive contains ametadata.jsonwith parsed options (projectID, thresholds, scenarios) thatk6 archiveregenerates correctly from the script source. Manual repacking preserves stale metadata and causes runtime errors (e.g. projectID mismatch).
gcx api doesn't expose response headers (see §2), so detect the shape
by inspecting the downloaded body with file(1):
The PUT body must be the raw script (or archive) bytes
(application/octet-stream). gcx forwards the request to k6 unchanged.
If the PUT returns 415, double-check that -H "Content-Type: application/octet-stream" made it through (some shell quoting mistakes
can drop it). Pass -vvv to gcx api for the request/response trace
when debugging.
6. Browser screenshots
Browser-module runs write screenshot PNGs to per-run S3 storage —
that's the only artifact type the files API surfaces today. They're
not exposed via /cloud/v6/; retrieval goes through a separate
cloud-resources/v1/files/ plugin route in a two-step flow. Both
calls require the same X-K6TestRun-Id header used for log queries
(§4) — gcx still handles auth, but the run ID must be supplied by
hand or the endpoint rejects with HTTP 422.
Notes worth knowing
- Index before signing.
generate-pre-signed-urldoes not validate that the file exists — it will happily mint a URL for any key, and the 404 only surfaces when you try to download (S3 returns<Error><Code>NoSuchKey</Code>...). Always derive the file list fromfiles/index, not from a guess at the path shape. - Batch the sign request.
filesis an array, so request all the URLs you need in one POST rather than one call per file — same 24h expiry covers the whole batch. - Browser tests vs. protocol tests. Only runs that called
page.screenshot()have entries in the index; a pure protocol/HTTP run will return[]. Don't assume the index is non-empty.
7. Browser traces (Tempo via gcx api)
Browser-module runs emit OTel spans for every iteration, navigation,
locator click, screenshot, web-vital observation, etc. The plugin
proxies a Tempo backend under /api/plugins/k6-app/resources/logs/
(same prefix as Loki — see §4), and the same X-K6TestRun-Id header
is required on every call. Without it, both search and fetch return
HTTP 401 "Test run ID missing".
Retrieval is a two-step flow: TraceQL search → fetch full trace by
ID. The full trace is large (≈100 KB on disk for a 120-span iteration
because -o json pretty-prints; ~50 KB compact), so the
join+summarise step is what keeps agent context lean.
Compact summary for an agent
The OTLP body for a single iteration can run ~100 KB pretty-printed. Dumping it verbatim into context is wasteful — most of the useful signal lives in the span-name distribution, the slowest individual spans, and the web-vital ratings. The pipeline below collapses a trace into ~1–2 KB of plain text:
On a run like 7542817 the output is something like:
Two paragraphs of text capture which navigations are slow, which web vitals are regressing, and the overall span-name distribution — enough to reason about browser performance without paging through the OTLP tree.
Notes worth knowing
- Header is mandatory. Both
/searchand/traces/<id>return HTTP 401 withoutX-K6TestRun-Id. The header scopes the query to the run's tenant. start/endare unix seconds. RFC3339 ISO strings getHTTP 400 invalid start: strconv.ParseUint. They're technically optional — search works without them — but providing a window matching the run'screated/ended(plus ~60s buffer) keeps the query fast and avoids matching unrelated runs that share the scenario name.- TraceQL, not LogQL. The
qparameter uses TraceQL — span predicates are written asspan.<attribute>and combined with&&. The suggested query filters iteration root spans for a specific scenario; broaden by droppingtest.scenario, narrow by adding e.g.span.test.vu = 3 && span.test.iteration.number = 5to pinpoint a single iteration on a single VU. - Scenario names come from the run. They're the keys of
.options.scenarioson the run object —gcx api /api/plugins/k6-app/resources/cloud/cloud/v6/test_runs/$RUN_ID --json options 2>/dev/null | jq -r '.options.scenarios | keys[]'lists them. The default scenario is nameddefault; browser tests often name itui,browser, etc. - Span IDs in the OTLP body are base64. The
traceIDin the search response is hex (the form/traces/<id>accepts); the innertraceId/spanId/parentSpanIdfields inside the OTLP batch are base64-encoded bytes. Cross-correlate parent/child viaparentSpanId == spanId, both in base64 — no need to decode. - Useful attribute keys on browser-test spans:
navigation.url,page.goto.url,screenshot.path,web_vital.name,web_vital.value,web_vital.rating,test.scenario,test.vu,test.iteration.number,k6.test_run_id. These are the ones worth surfacing in summaries; the OTel value envelope is{stringValue|intValue|boolValue|doubleValue}(the jqattrvalhelper above handles all four).
8. Cloud Insights (audit results for a run)
Cloud Insights runs heuristics against a finished test — checks for
high cardinality, web-vital regressions, missing thresholds,
overutilised load generators, and so on — and exposes the results
through a separate /resources/insights/ plugin route (not
/cloud/v{N}/). gcx still handles auth; no extra header is needed.
Three calls produce the data, and the agent-useful output is a join across two of them:
Both responses use the same top-level key — audits — but the
contents differ: step 2 is the catalog of what each audit checks,
step 3 is what that audit found on this particular run. Cross-join
them via result.audit_id == audit.id (1:1 in practice).
Joining into a compact, agent-readable summary
The raw audit + results JSON together is ~10 KB. Dumping both into
context just to read out three lines per audit is wasteful — do the
join in jq and emit a single tight block. The pipeline below is
the recommended shape:
For a 15-audit run this emits ~5 KB of plain text — title, score, one-line description, one-line explanation, and any action items. That's enough for the agent to reason about the test's health without re-reading either JSON blob.
Notes worth knowing
status≠ verdict.status: "succeeded"means the audit executed. The verdict lives inscore(binarytrue/false, ornumeric0…1 where 1 is best).status: "failed"means the audit itself could not run (typicallystatus_reason: "missing data"— e.g. the HTTP Spans audit on a non-tracing test); thescorefield is absent. Treat these as "no signal", not as failures.- Score thresholds vary per audit. A
numeric0.94 might be fine for one audit and concerning for another — there's no global cutoff. Theexplanationis authoritative for what the score means; surface it verbatim rather than inventing a pass/fail rule. actionsis the actionable bit. Only present when the audit has concrete recommendations (e.g. "Reduce the cardinality of theurllabel …"). When summarising a run for a user who's trying to improve it, lead with audits that have a non-emptyactionsarray.- Pick the last execution, not the first.
.executions[]is in chronological order; re-runs append. Older executions reflect older audit logic and may have stale results. - Insights is a post-run analysis. If the run hasn't finished
(or never produced enough data for insights to compute), the
executions list may be empty — bail out gracefully on
length == 0.
9. Local k6 CLI (smoke tests and k6 cloud run)
The local k6 CLI is the right tool for parse checks and 1-iteration
smoke runs before pushing to cloud:
For k6 cloud run (uploads + runs in cloud from your laptop), authenticate
k6 cloud login with the token and stack URL pulled from gcx — token
comes from gcx k6 auth token, stack URL from the active gcx context's
grafana.server field:
k6's cloud config (~/.config/k6/cloud.json) is single-context, so
re-run k6 cloud login whenever you switch gcx contexts — otherwise
k6 cloud run will keep targeting the previous stack.
Exit codes: 0 pass, 99 threshold fail, anything else = script/runtime error.
10. Gotchas
11. Mutations not covered by update -f
gcx k6 <resource> update -f follows the v6 PATCH schema for that
resource. Fields outside the schema are silently dropped while gcx
still prints ✔ Updated <resource> <id> — see the §10 row. Several
common mutations have dedicated endpoints instead, and the PATCH
route will no-op on them.
Polling a started run: After starting a run, poll
GET /cloud/v6/test_runs/{id} until status reaches completed or
aborted. Always check the result field alongside status —
status: completed with result: error means a configuration or
infrastructure failure (not a threshold breach). On any non-passed
result, immediately fetch logs (§4) to surface the error rather than
waiting for the user to report it.
| Abort a running test | POST /cloud/v6/test_runs/{id}/abort | empty |
| Set / overwrite a schedule | POST /cloud/v6/load_tests/{id}/schedule | Schedule body (recurrence_rule or cron) |
| Deactivate / reactivate a schedule | POST /cloud/v6/schedules/{id}/{deactivate,activate} | empty |
| Create a load test (multipart!) | POST /cloud/v6/projects/{id}/load_tests | multipart: name + script |
| Persist a run past retention | POST /cloud/v6/test_runs/{id}/save | empty (paired with /unsave) |
The PATCH-style updates that do work via update -f:
Both schemas declare additionalProperties: false — any other field
you put in the manifest is silently filtered out before the PATCH is
sent, even though gcx still reports ✔ Updated. Don't try to flip
is_default or move a grafana_folder_uid through update -f;
they're not exposed for mutation on the v6 PATCH.
Cross-check by reading the operation's request schema in the OpenAPI spec (workflow in §3) before assuming a field is mutable.
Worked example — move a test between projects
The OpenAPI description for this endpoint is explicit: "Move a load test to a different project of the same organization. All respective test runs will be also moved to the new project." You don't need to migrate runs separately.
Cascade behavior worth knowing
- Deleting a load test cascade-deletes its schedule. The schedule
is gone from
/cloud/v6/schedulesand/cloud/v6/load_tests/{id}/scheduleimmediately. No need togcx k6 schedules delete <load-test-id>first as a defensive step. - Deleting a project with a running test fails with HTTP 409
("Cannot delete project with a running test." per the OpenAPI
spec). Non-running tests appear to be removed with the project,
but if you want to inventory a project's contents before deleting
it, use
GET /cloud/v6/projects/{id}/load_tests(NOTgcx k6 load-tests list --project-id <id>, which doesn't filter — see §10). - Moving a test moves its runs and run history with it. Schedule
attachment also follows the test (it's keyed by
load_test_id, not by project).
Verification rule of thumb
The recurring pattern in this skill is "gcx confirms success even when the underlying call no-ops". Whenever you mutate state:
- Note what you expected to change (field, count, status).
- Re-GET the resource and confirm the change is reflected.
- If it isn't, check whether the mutation needed a dedicated
endpoint (this section) rather than
update -f.


