Skip to main content

Environment variables

k6 can access environment variables at runtime. You can use environment variables to:
  • Configure test behavior without modifying the script
  • Pass sensitive data like API keys
  • Parameterize tests for different environments
  • Override k6 options

Accessing environment variables

Access environment variables in your script using the __ENV object:

Setting environment variables

You can set environment variables in multiple ways:
Pass variables when running k6:

k6 options via environment variables

You can set k6 options using environment variables with the K6_ prefix:
Common k6 environment variables:
integer
Number of virtual users
string
Test duration (e.g., 30s, 5m, 1h)
integer
Total number of iterations
string
Output destination (e.g., json=results.json, influxdb=http://localhost:8086)
boolean
Skip TLS certificate verification

Common patterns

Environment-specific configuration

Run with:

Sensitive data

Never hardcode sensitive data like API keys, passwords, or tokens in your test scripts.

Feature flags

Best practices

Always provide fallback values for non-critical environment variables:
Fail early if required variables are missing:
Environment variables are always strings. Convert them as needed:
Add comments or README documentation listing required environment variables:

CI/CD integration

Environment variables are particularly useful in CI/CD pipelines:

Options reference

Complete list of k6 options

Automated testing

Integrating k6 with CI/CD