Find code examples for this guide on GitHub.
Initial setup
Add a Procfile and a simple Hello World app, then push to Heroku.
# Procfile
web: node index.js
// index.js
const express = require('express')
const app = express()
const PORT = process.env.PORT || 3000
app.get('/', (req, res) => {
res.send(`Hello ${process.env.HELLO || ''}`)
})
app.listen(PORT, () => {
console.log(`Server running on port:${PORT}`)
})
Run dotenvx
Install dotenvx via the dotenvx buildpack.
heroku buildpacks:add https://github.com/dotenvx/heroku-buildpack-dotenvx
Update your Procfile to use dotenvx.
# Procfile
web: dotenvx run -- node index.js
Add production environment
# .env.production
HELLO="production"
Encrypt production
$ dotenvx encrypt -f .env.production
Commit .env.production. Do not commit .env.keys. Keep private keys somewhere safe like 1Password or Armor ⛨.
Set decryption key
Set DOTENV_PRIVATE_KEY_PRODUCTION on Heroku from your .env.keys file.
heroku config:set DOTENV_PRIVATE_KEY_PRODUCTION='your-private-key'
git push heroku
Your app restarts and env is injected from the encrypted .env.production file.