Environment Configuration
Learn how to manage different API environments like Dev, Staging, and Production.
Plyson makes it easy to switch between different API environments without changing your test code. By using environment files, you can manage base URLs, API keys, and other stage-specific variables in one place.
Environment Files
Environment files are stored in the environments/ directory. Plyson supports two formats:
- JSON (
*.env.json): Structured configuration with schema support. - Dotenv (
*.env): Standard key-value pairs for environment variables.
For a detailed breakdown of all available fields, see the Environments Reference.
environments/dev.env.jsonand/orenvironments/dev.env-> selected via--env devenvironments/staging.env.jsonand/orenvironments/staging.env-> selected via--env staging
Structure of an Environment File
JSON Format (dev.env.json)
{
"$schema": "../Project-schema/environment.schema.json",
"baseUrl": "https://api.dev.json",
"variables": {
"API_KEY": "dev_secret_key_123"
}
}Dotenv Format (dev.env)
baseUrl=https://api.dev.env
API_KEY=dev_override_key
TIMEOUT=5000Merging & Priority
If both a .env.json and a .env file exist for the same environment, Plyson merges them. Dotenv files take precedence over JSON files.
Additionally, System Environment Variables (e.g., from your OS or CI/CD pipeline) have the highest priority and will override values from both file formats.
Resolution Priority (Highest to Lowest):
- System Environment Variables
.envFile (dev.env).env.jsonFile (dev.env.json)
CI/CD & System Overrides
You can override any environment variable or the baseUrl using system environment variables. This is particularly useful for passing secrets in CI/CD pipelines without committing them to your repository.
BASE_URLorbaseUrl: Overrides the environment's base URL.VARIABLE_NAME: Overrides a variable defined in the environment'svariablesblock.
# Example: Override baseUrl and a specific variable via command line
BASE_URL=https://api.test.com DB_PASSWORD=secret npx plyson run --env devSwitching Environments
To run your tests against a specific environment, use the --env flag:
npx plyson run --env stagingDefault Environment
You can specify a default environment in your project.json so you don't have to provide the flag every time:
{
"title": "My API Project",
"defaultEnv": "dev"
}With this setting, running npx plyson run will automatically target the dev environment.
Best Practices
- Don't Hardcode URLs: Always use relative paths in your test suites (e.g.,
/users) and define thebaseUrlin your environment files. - Variable Overrides: Use environment variables for things that change between stages, such as database IDs or feature flags.