Encrypt secrets in Express

Learn how to encrypt your first .env file using Express and the Dotenvx Node.js SDK.

Prerequisites

To get the most out of this guide, you'll need to:

1. Install

Get the Dotenvx Node.js SDK.

npm install @dotenvx/dotenvx

2. Encrypt env file

Encrypt your .env file.

$ terminal

npx dotenvx encrypt

3. Inject secrets

Then inject your encrypted secrets at runtime.

index.js

require('@dotenvx/dotenvx').config()
const express = require('express')

const app = express()
app.use(express.json())

app.get('/', (req, res) => {
  res.json({
    ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY
  })
})

const port = process.env.PORT || 3000
app.listen(port, () => {
  console.log(`Server running on http://localhost:${port}`)
})