Vapor v0.10.0 Vapor.Provider.Dotenv View Source

The dotenv config provider will look for a .env file and load all of the values for that file. The values can be written like so:

DATABASE_URL=https://localhost:9432
PORT=4000
REDIS_HOST=1234

Multiline variables can be written using bash-style heredocs like so:

API_PRIVATE_KEY=<< EOF
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEArq74P2ButEMolub0tHfxdWSLcaGi7Da7IJX7jOZFdcSjvXjt
...
4A5j0wlIuJqz+OZmV+WSOlJdFXQugTaMd6hMLG8SE6AvEM+L91E=
-----END RSA PRIVATE KEY-----
EOF
PORT=4000
REDIS_HOST=1234

If the file can't be found then this provider will still return an ok but will (obviously) not load any configuration values. The primary use case for this provider is local development where it might be inconvenient to add all of the necessary environment variables on your local machine and it makes tradeoffs for that use case.

Existing environment variables

By default the dotenv provider won't overwrite any existing environment variables. You can change this by setting the overwrite key to true:

%Dotenv{overwrite: true}

File hierarchy

If no file is specified then the dotenv provider will load these files in this order. Each proceeding file is loaded over the previous. In these examples ENV will be the current mix environment: dev, test, or prod.

  • .env
  • .env.ENV
  • .env.local
  • .env.ENV.local

You should commit .env and .env.ENV files to your project and ignore any .local files. This allows users to provide a custom setup if they need to do that.