Overview
There are three ways to provide environment variables:- Inline variables —
@var = valuein.httpfiles (same for all environments) - Environment file —
.hitspec.env.jsonwith per-environment values - Config file —
hitspec.yamlwith anenvironmentssection - System environment variables — accessed via
$env()
Inline Variables
Define variables directly in your test file:- Values are the same regardless of environment
- Secrets are visible in the test file
- Switching environments requires editing the file
Environment File (.hitspec.env.json)
Create a.hitspec.env.json file in your project root with per-environment variable sets:
--env flag:
Config File (hitspec.yaml)
Thehitspec.yaml config file combines global settings with environment definitions:
hitspec.yamlhitspec.yml
Using .env Files
Pass a.env file with the --env-file flag to inject additional variables:
.env file uses standard KEY=VALUE format:
System Environment Variables
Reference system environment variables in your test files using$env():
.hitspec.env.json, use the {{$env.VAR}} syntax:
hitspec.yaml, use shell-style ${VAR} syntax:
HITSPEC_ Environment Variables
All major CLI flags can be set via environment variables with theHITSPEC_ prefix, which is useful in CI/CD pipelines:
| Environment Variable | CLI Flag | Description |
|---|---|---|
HITSPEC_ENV | --env | Environment to use |
HITSPEC_ENV_FILE | --env-file | Path to .env file |
HITSPEC_CONFIG | --config | Path to config file |
HITSPEC_TIMEOUT | --timeout | Request timeout |
HITSPEC_TAGS | --tags | Filter by tags |
HITSPEC_OUTPUT | --output | Output format |
HITSPEC_OUTPUT_FILE | --output-file | Output file path |
HITSPEC_BAIL | --bail | Stop on first failure |
HITSPEC_PARALLEL | --parallel | Run in parallel |
HITSPEC_CONCURRENCY | --concurrency | Concurrent requests |
HITSPEC_QUIET | --quiet | Suppress output |
HITSPEC_NO_COLOR | --no-color | Disable colors |
HITSPEC_PROXY | --proxy | Proxy URL |
HITSPEC_INSECURE | --insecure | Skip SSL verification |
Variable Resolution Order
When the same variable name is defined in multiple sources, hitspec resolves it in this order (later sources override earlier ones):- Built-in functions —
$uuid(),$timestamp(), etc. - Environment file —
.hitspec.env.jsonfor the active environment - Config file —
hitspec.yamlenvironments section - Inline variables —
@variable = valuein the.httpfile - Captured values —
{{requestName.captureName}}from prior responses
Inline variables override environment file values. This means a
@baseUrl = ... in your .http file will take precedence over the baseUrl defined in .hitspec.env.json. Remove inline definitions when you want environment-specific values to apply.Best Practices
Keep Secrets Out of Version Control
Add.hitspec.env.json to .gitignore if it contains tokens or passwords:
Use System Variables in CI/CD
- GitHub Actions
- Shell
Shared Base Configuration
Use adefault environment key for values shared across all environments:
Environment-Specific Test Behavior
Use environment variables for timeouts, retry settings, and other behavioral differences:Recommended Project Structure
Troubleshooting
Variable not found
Variable not found
- Check that
.hitspec.env.jsonorhitspec.yamlcontains the variable for your environment. - Verify the
--envflag matches an environment key:hitspec run tests/ --env dev. - Check for typos in variable names (they are case-sensitive).
Environment not found
Environment not found
- Add a
"staging"key to.hitspec.env.jsonorhitspec.yaml. - Check for typos in the environment name.
System variable not set
System variable not set
- Export the variable:
export API_TOKEN=xxx - Set it inline:
API_TOKEN=xxx hitspec run tests/ - In CI/CD, add it to your secrets and pass it to the workflow.
- Use a default value:
{{$env(API_TOKEN, fallback-value)}}