---
title: "Environment variables | Grafana k6 documentation"
description: "You can access any environment variables from your k6 script code and use this to supply your VUs with configuration information."
---

# Environment variables

Often, scripts need only minor tweaks to be reusable in different contexts. Rather than creating several separate scripts for these different contexts or environments, you can use [environment variables](/docs/k6/latest/reference/glossary/#environment-variables) to make parts of your script tweakable.

You can use environment variables for two main purposes:

1. Passing environment variables to the k6 Script
2. Configuring [k6 Options](/docs/k6/latest/using-k6/k6-options/how-to/) with environment variables

## Passing environment variables to the k6 script

In k6, the environment variables are exposed through a global `__ENV` variable, a JS object. For reference, see the script example below:

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
import http from 'k6/http';
import { sleep } from 'k6';

export default function () {
  const res = http.get(`http://${__ENV.MY_HOSTNAME}/`);
  sleep(1);
}
```

The recommended option to pass environment variables to your testing script is to use one or more [`-e` / `--env` CLI flags](/docs/k6/latest/using-k6/k6-options/reference/#supply-environment-variables) (this command works the same for all platforms):

Bash ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```bash
k6 run -e MY_HOSTNAME=test.k6.io script.js
```

> #### ⚠ The `-e` flag does not configure options
> 
> This flag just provides variables to the script, which the script can use or ignore. For example, `-e K6_ITERATIONS=120` does *not* configure the script iterations.
> 
> Compare this behavior with `K6_ITERATIONS=120 k6 run script.js`, which *does* set iterations.

Using System Environment Variables

A second option to pass environment variables is to source them from the local system.

Bash ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```bash
MY_HOSTNAME=test.k6.io k6 run script.js
```

windows ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```windows
set "MY_HOSTNAME=test.k6.io" && k6 run script.js
```

powershell ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```powershell
$env:MY_HOSTNAME="test.k6.io"; k6 run script.js
```

#### ⚠️ Warning

By default, passing system environment variables doesn’t work for `k6 archive`, `k6 cloud run`, and `k6 inspect`. This is a security measure to avoid the risk of uploading sensitive data to k6 Cloud. To override this mode, specify [–include-system-env-vars](/docs/k6/latest/using-k6/k6-options/reference/#include-system-env-vars).

## Configure k6 options with environment variables

You can also configure k6 [options](/docs/k6/latest/using-k6/k6-options/how-to/) with environment variables. Consider this script:

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
import http from 'k6/http';
import { sleep } from 'k6';

export default function () {
  const res = http.get('https://test.k6.io');
  sleep(1);
}
```

By default, a local run of this script will execute a single iteration with one virtual user(VU). To modify the default behavior, pass [k6 options](/docs/k6/latest/using-k6/k6-options/how-to/) as environment variables. For example, this snippet configures the script to run 10 virtual users for a duration of 10 seconds:

Bash windows powershell

![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

Bash ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```bash
K6_VUS=10 K6_DURATION=10s k6 run script.js
```

windows ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```windows
set "K6_VUS=10 K6_DURATION=10s" && k6 run script.js
```

powershell ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```powershell
$env:K6_VUS=10 ; $env:K6_DURATION="10s" ; k6 run script.js
```

As the preceding example shows, you need to prefix `K6_` in the environment variable name for k6 to evaluate it as an option parameter. However, be aware that not all options are supported as environment variables. You can confirm whether one is by checking the [documentation for each option](/docs/k6/latest/using-k6/k6-options/reference/).

Note that when you define options in multiple places, there’s an [order of precedence](/docs/k6/latest/using-k6/k6-options/how-to/) that determines the option to use. To ensure you’re always working with the highest precedence, use command-line flags instead of environment variables:

Bash ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```bash
k6 run -e MY_HOSTNAME=test.k6.io --duration 10s --vus 10 script.js
```

## Read more

- [Manage environment variables in k6 Cloud](/docs/grafana-cloud/testing/k6/author-run/cloud-scripting-extras/cloud-environment-variables/)
