Render with Express
In this tutorial, learn how to integrate Dotenv Vault with Render. This tutorial assumes you are familiar with Render or have gone through one of their tutorials.
You can find a complete example repo here.
Install dotenv-vault
Create your local .env
file.
HELLO="World"
Install dotenv-vault-core.
$ npm install dotenv-vault-core --save
Require it as early as possible in your Express application.
// app.js
require('dotenv-vault-core').config();
console.log(process.env) // for debugging purposes. remove when ready.
const express = require("express");
...
Output process.env.HELLO in the html.
// app.js
...
<body>
<section>
Hello ${process.env.HELLO}!
</section>
</body>
...
Test that it is working locally.
$ node app.js
{
HELLO: 'World'
}
Example app listening on port 3001!
Next, we need to build our encrypted .env.vault file.
Build .env.vault
First set a production value. Run dotenv-vault open to edit production values.
$ npx dotenv-vault open production
Then build your localized encrypted .env.vault file.
$ npx dotenv-vault build
Great! Safely commit your .env.vault file to code.
Set DOTENV_KEY
Lastly, set the DOTENV_KEY on Render.
Run npx dotenv-vault keys production to get your production decryption key.
$ npx dotenv-vault keys production
remote: Listing .env.vault decryption keys... done
dotenv://:[email protected]/vault/.env.vault?environment=production
Then in Render click Project Settings > Environment > Add Environment Variable.
Enter your DOTENV_KEY and save changes.
That’s it!
Commit your changes to code and push.
When the build runs, it will recognize the DOTENV_KEY, decrypt the .env.vault file, and load the production environment variables to Render. If a DOTENV_KEY is not set (like during development on your local machine) it will fall back to regular dotenv.
You will know it worked when you see the message ‘Loading env from encrypted .env.vault’.