/ Search - type / Modes - type M Star on GitHub - 5.7k

Docs

Cloudflare

Use dotenvx with Cloudflare Workers and Pages.

Initial setup

Select Build System v3 (bun) and install dependencies:

# Install dotenvx and wrangler
bun install @dotenvx/dotenvx wrangler --save

Cloudflare Workers

Add dotenvx to your Worker entrypoint to load environment variables at startup:

// Load .env variables automatically
import '@dotenvx/dotenvx/config'

export default {
  async fetch(request) {
    return new Response(`Hello ${process.env.HELLO}`)
  }
}
name = "dotenvx-worker"
main = "index.js"
compatibility_date = "2025-08-21"

Add your .env file and encrypt it:

# .env
HELLO="World"

bunx dotenvx encrypt

Set the private key as a Worker secret and deploy:

wrangler secret put DOTENV_PRIVATE_KEY
wrangler publish

Cloudflare Pages

For Pages builds, run dotenvx during the build and dev scripts:

{
  "scripts": {
    "dev": "dotenvx run -- bun run dev",
    "build": "dotenvx run -- bun run build"
  }
}

In your framework config (e.g. Next.js), expose env vars:

export default {
  env: {
    HELLO: process.env.HELLO,
  },
}
# .env.production
HELLO="Production"

bunx dotenvx encrypt -f .env.production

In the Pages dashboard: Settings → Environment Variables, add DOTENV_PRIVATE_KEY from .env.keys. Pages injects the encrypted values during build.

For a shorter Workers quickstart, see Cloudflare Workers.