Encrypt secrets in Astro

Learn how to encrypt your first .env file using Astro 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

Preface Astro scripts with dotenvx run -- and read your env values in Astro.

package.json

{
  "scripts": {
    "dev": "dotenvx run -- astro dev",
    "build": "dotenvx run -- astro build",
    "preview": "dotenvx run -- astro preview"
  }
}

src/pages/api.js

export async function GET() {
  return new Response(
    JSON.stringify({
      HELLO: process.env.HELLO,
    }),
    {
      status: 200,
      headers: {
        "Content-Type": "application/json",
      },
    }
  );
}