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:
- Create your account (optional)
1. Install
Get the Dotenvx Node.js SDK.
$ npm install @dotenvx/dotenvx
# or
# bun add @dotenvx/dotenvx
# pnpm add @dotenvx/dotenvx
# yarn add @dotenvx/dotenvx
2. Encrypt
Encrypt your .env file.
$ npx dotenvx encrypt
◈ encrypted (.env) + local key (.env.keys)3. Inject
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",
},
}
);
}