Menu
Grafana Cloud

Authenticate on the CLI

To run cloud tests from the CLI, you need to authenticate your account. Authentication is required both for tests that run on the cloud service and for results that stream to the cloud service. To authenticate accounts, k6 provides an API token, which you can pass on the CLI or through the REST API.

You can generate two types of API tokens for k6 Cloud, depending on how you want to authenticate:

To generate API tokens, follow these steps.

Get a personal API token

A personal API token provides API access to your account with k6.

To get a personal API token:

  • Log in to your Grafana Cloud account and open a stack.
  • On the main menu, click Testing & synthetics -> Performance -> Settings.

In the Personal API token tab, you can copy, see, and regenerate your personal API token.

Note

k6 API tokens don’t have an expiration date. If you wish to rotate your k6 keys, you have to do it manually. It’s recommended that you rotate your k6 keys periodically to ensure appropriate security.

Get a Grafana Stack API token

Note

Only admins can access and manage these tokens.

A Stack API token provides access to all projects within the Grafana Cloud Stack. You can use it to run tests without linking the run to a specific user, for example, when running tests from a CI platform.

To generate a token, follow these steps:

  • Log in to your Grafana Cloud account and open a stack.
  • On the main menu, click Testing & synthetics -> Performance -> Settings.
  • Click on the Grafana Stack API token tab.

On this section, you can create, see, and regenerate the tokens.

By default, organizations can create a maximum of 5 tokens.

Note that you must specify the project where the test runs will be created when using a non-personal token. Refer to Cloud options to learn how to set the projectID.

Authenticate with the login command

To authenticate with an API token in the CLI, enter the following command:

bash
k6 cloud login --token $TOKEN

k6 cloud login stores your API token in a local configuration file to authenticate when running cloud commands. Unless your token changes or you run tests on multiple cloud accounts, you need to run k6 login only once.

Authenticate with an environment variable

You can also authenticate with your Grafana Cloud k6 authentication token via environment variables. Make sure you have set the K6_CLOUD_TOKEN as your authentication token, and k6 will pick it up when running.

bash
## run a cloud test
K6_CLOUD_TOKEN=$TOKEN k6 cloud run script.js

## run locally and stream to the cloud
K6_CLOUD_TOKEN=$TOKEN k6 cloud run --local-execution script.js

Authenticate with a configuration file

You can also directly add your authentication token to a configuration file:

  • On Linux:

    bash
    ${HOME}/.config/loadimpact/k6/config.json
  • On Mac

    bash
    ${HOME}/Library/Application Support/loadimpact/k6/config.json
  • On Windows

    bash
    C:\Users\<User>\AppData\Roaming\loadimpact\k6\config.json

Or, set the configuration file path with the -c/--config PATH/TO/CONFIG_FILE CLI flag.

After your token has been added to the configuration file, it should look something like this:

json
{
  "collectors": {
    "cloud": {
      "token": "YOUR_API_AUTH_TOKEN"
    }
  }
}

Authenticate in a Docker container

If you’re running k6 in a Docker container, the -c/--config CLI flag is the recommended approach. This ensures that the k6 configuration file where the API authentication information is stored is persisted via a Docker volume to the host machine.

bash
docker run --rm -i -v /path/on-host:/path/in-container/ grafana/k6 \
login cloud -c /path/in-container/config.json