Encrypt your first .env file
$ dotenvx encrypt
Create a .env file in your project root.
# .env
HELLO="Secret"
$ dotenvx encrypt
◈ encrypted .env (039 D06) via .env.keys ⛉
(When prompted, choose Classic (.env.keys).)
Your .env values become ciphertext. The private key that unlocks them goes in .env.keys.
Add these lines to your .gitignore:
# .gitignore
.env.keys
!.env
Then commit the encrypted .env with your code.
$ git add -f .env
$ git add .gitignore
$ git commit -m "encrypt .env"
Keep .env.keys private and backed up. It holds the key you need to decrypt your secrets. Share the key manually with your team or use Armor or other custody options.
Create index.js:
// index.js
console.log(`Hello ${process.env.HELLO}`)
Run your app with Dotenvx:
$ dotenvx run -- node index.js
⟐ injected env (2) from .env (039 D06) via .env.keys ⛉
Hello Secret
Dotenvx reads the key from .env.keys, decrypts your values, and injects them into your app at runtime. Replace node index.js with your own app’s command.
That’s it! You’ve encrypted your secrets and loaded them into your app with Dotenvx.
The same workflow works in CI and production. Create a separate .env.production.
# .env.production
HELLO="production"
$ dotenvx encrypt -f .env.production
◈ encrypted .env.production (03A 61D) via .env.keys ⛉
Choose Classic (.env.keys) again. Dotenvx adds a separate production key to .env.keys.
Copy the value named DOTENV_PRIVATE_KEY_PRODUCTION into an environment variable with that same name in your deployment settings.
Commit the encrypted production file:
$ git add -f .env.production
$ git commit -m "encrypt .env.production"
Run your deployed app with:
$ dotenvx run -f .env.production -- node index.js
⟐ injected env (2) from .env.production (03A 61D) via .env.keys ⛉
Hello production
Your encrypted file travels with your code. The production key stays in your deployment environment.
That’s it! Your app now loads encrypted secrets in production, with the private key stored in your deployment environment.
Next: