Environments
Environments let you switch between local, staging, and production configs without changing your request files.
Project Config File
Create yapi.config.yml in your project root:
yapi: v1
default_environment: local
defaults:
vars:
API_VERSION: v1
SHARED_VAR: shared_value
environments:
local:
url: http://localhost:3000
vars:
API_KEY: dev_key
DEBUG: "true"
staging:
url: https://staging.api.example.com
vars:
API_KEY: ${STAGING_API_KEY}
prod:
url: https://api.example.com
vars:
API_KEY: ${PROD_API_KEY}
env_file: .env.prod
Key Fields
default_environment: Used when-eflag is not specifieddefaults: Variables available in ALL environmentsenvironments: Environment-specific settingsurl: Base URL for requests usingpath:instead ofurl:vars: Environment-specific variablesenv_file: Path to a.envfile to load
Selecting an Environment
yapi run request.yapi.yml -e staging
yapi test ./tests -e prod
Without -e, the default_environment is used.
How URL Resolution Works
Request files can use path: instead of url::
# request.yapi.yml
yapi: v1
path: /api/users
method: GET
The path is appended to the environment's url. So with -e local,
the full URL becomes http://localhost:3000/api/users.
Variable Precedence
When the same variable is defined in multiple places:
- Chain step references (highest priority)
- Environment-specific
vars defaults.vars- Shell environment variables
- Default values (
${VAR:-default})
Env Files
Load secrets from .env files per environment:
environments:
prod:
env_file: .env.prod
# .env.prod
API_KEY=sk-prod-abc123
DB_URL=postgres://prod-host/db
See Also
yapi docs variables— Variable interpolation and resolutionyapi docs config— Full YAML config field referenceyapi docs testing— Running tests across environments