No results for

Powered byAlgolia

Environment variables

suggest edits

k6 and environment variables

Environment variables can be used with k6 in two ways:

  • You can access any environment variables from your k6 script code, and use this to supply your VUs with configuration information.
  • A couple of environment variables are automatically read by k6 on startup, affecting its behavior.

Accessing environment variables from a script

A lot of the time, scripts will only need minor tweaking to be reusable in different contexts. Rather than having to create several separate scripts for these different contexts or environments, you can use environment variables to make parts of your script tweakable.

In k6, the environment variables are exposed through a global __ENV variable, a JS object. The source of the environment variables can be twofold. They could come from the local system and/or be explicitly passed to k6 using one or more -e NAME=VALUE CLI flags.

The primary difference between the two is that only k6 run passes the actual system environment variables to the script code by default, while k6 archive, k6 cloud and k6 inspect do not. So unless you explicitly specify --include-system-env-vars, only the variables passed using the -e CLI flag will be persisted when creating an archive (k6 archive script.js). You can also disable the default passing of system environment variables when running scripts by using --include-system-env-vars=false.

An environment variable could, for example, be specified like this on the command line:

Windows: CMD
Windows: PowerShell
$ k6 run script.js

or using an -e / --env CLI flag (which will be the same for all platforms):

1$ k6 run -e script.js

The environment variable could then be used as follows in a script:

1import { check, sleep } from 'k6';
2import http from 'k6/http';
4export default function () {
5 var r = http.get(`http://${__ENV.MY_HOSTNAME}/`);
6 check(r, {
7 'status is 200': (r) => r.status === 200,
8 });
9 sleep(5);

Environment variables specified with the -e CLI flag takes precedence over (overwrite) actual system environment variables with the same name.

Environment variables k6 will read automatically

k6 will also try to read some specific environment variables that the user can set to change the behavior of k6:

K6_CLOUD_HOSTA URL to connect to, when the --out=cloud results output option is specified.
K6_CLOUD_TOKENAn authentication token to use in API calls to the cloud service, when the --cloud results output option is specified.
K6_NO_USAGE_REPORTDefine this to disable usage reports.
K6_OUTSpecify results output, same as --out command-line option.