Bun
Prerequisites
Install the latest Bun and open your Bun project.
Install
Install Dotenvx:
$ bun add @dotenvx/dotenvx
Disable Bun's automatic .env loading so Dotenvx handles your encrypted files. Add this top-level setting to bunfig.toml:
# bunfig.toml
env = false
Keep this file with your app when deploying.
Encrypt
Start with a secret value in the .env file at your project root, like:
# .env
HELLO="Secret"
Encrypt it with a single command.
$ bunx --bun dotenvx encrypt
◈ encrypted .env (022 0D8) via .env.keys ⛉
The values become ciphertext and only your private key can unlock them.
# .env
DOTENV_PUBLIC_KEY="0220d830351410598be484f43a7b07097e09851f50426832876e8b5815a1752990"
HELLO="encrypted:BHLTACNJMr00nTG6yXpkCyWFKF/MY0ajN855tg3uVtKopTe2AGzSkQlcPd21pTOT3Ci8IKrdIg2TMZFoq1mDR6yb06QCRvqHXtpkZkAHYCEHfeWqqC8tMFovcYq5JS2uZSrC/qUGDA=="
Commit
Commit your encrypted .env files with your code. It's safe. Now you can securely share secrets through git.
$ git add .env
$ git commit -m "encrypt .env"
Ship
Create a Bun server that reads your secret:
// index.ts
import { config } from '@dotenvx/dotenvx';
config({ strict: true });
const server = Bun.serve({
port: Number(process.env.PORT || 3000),
fetch() {
return new Response(`Hello ${process.env.HELLO}`);
},
});
console.log(`Server running on ${server.url}`);
HELLO is a demonstration value. Use real secrets in server-side operations without returning them to the browser.
Add these scripts to package.json, keeping your other scripts:
{
"scripts": {
"start": "bun index.ts",
"dev": "bun --watch index.ts"
}
}
Run your app:
$ bun run start
⟐ injected env (2) from .env (022 0D8) via .env.keys
Server running on http://localhost:3000/
Dotenvx loads and decrypts .env before the server starts. Use bun run dev to restart when your code changes.
Visit http://localhost:3000 to see Hello Secret.
Share
Have teammates? Share access with Dotenvx Armor ⛨
Log in to Dotenvx.
$ bunx --bun dotenvx login
◉ logged in (username)
Create the Armored Key ⛨
$ bunx --bun dotenvx armor up
⛨ armored .env (022 0D8)
Invite your teammate.
$ bunx --bun dotenvx armor invite
⛨ invited [email protected] to team
Your teammate accepts the invitation, pulls the repository, installs dependencies, and logs in with dotenvx login. They can then run the app:
$ bun run start
⟐ injected env (2) from .env (022 0D8) via Armor ⛨
Server running on http://localhost:3000
Visit http://localhost:3000 to see Hello Secret.
Share a private key Alternative →
Production
Create a .env.production file.
# .env.production
HELLO="Production"
Encrypt it with an Enclaved Key ◊
$ bunx --bun dotenvx encrypt -f .env.production
◈ encrypted .env.production (022 0D8) via Armor Enclave ◊
When prompted, choose Enclaved, select your deployment service, connection, and resource, and use DOTENV_PRIVATE_KEY_PRODUCTION as the private key name.
Commit .env.production.
$ git add -f .env.production
$ git commit -m "encrypt .env.production"
Deploy your code, package.json, Bun lockfile, bunfig.toml, and encrypted .env.production file to a resource with Bun installed. Install your dependencies there:
$ bun install
Set DOTENV_FILE to .env.production in your deployment environment so Dotenvx knows which file to load. Keep .env.keys on your local machine.
Set NODE_ENV to production and PORT to the port your host expects. Run your app from the project root:
$ bun run start
Dotenvx loads and decrypts .env.production with your Enclaved Key ◊
Open your deployed app's URL to see Hello Production.
Deploy with a private key Alternative →
Conclusion
You've encrypted your secrets, shared access with Armor, and loaded production secrets in Bun with an Enclaved Key. Your secrets now travel with your code.