> ## Documentation Index
> Fetch the complete documentation index at: https://www.orionjs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Env

> Orion Env is a utility for managing a collection of secrets in source control.

Orion Env is a robust utility for securely managing environment variables directly in your source control. Unlike traditional `.env` files that must be excluded from repositories, Orion Env allows you to safely commit your configuration to your codebase using post-quantum public key cryptography.

> This package works as a standalone package. It does not depend on any other Orion.js packages.

## Key Features

* **Secure Storage**: Encrypt sensitive data with hybrid post-quantum cryptography (ML-KEM-768 + X25519)
* **Source Control Integration**: Store all configurations directly in your repository
* **TypeScript Support**: Automatically generate TypeScript definitions for your environment variables
* **Secret Manager Integration**: Read variables from JSON-formatted environment variables populated by secret managers
* **Flexible Usage**: Access environment variables directly or load them into process.env

## Installation

```sh theme={null}
pnpm add @orion-js/env
```

## Getting Started

### Creating a new Env configuration file

Run the following command in your project. You'll receive a password that you'll need to keep secure - it's required to decrypt your environment variables.

```sh theme={null}
pnpm orion-env init --path=<path>
```

This will create a new configuration file at the specified path with an initial setup.

### Adding environment variables

To add a new environment variable to your configuration:

```sh theme={null}
pnpm orion-env add --path=<path>
```

This interactive command will prompt you for:

* The variable name
* The variable value

### Reading variables in your application

There are two main approaches to using your environment variables:

#### 1. Direct access via the `env` object

```typescript theme={null}
import { env } from '@orion-js/env'

// Access variables directly
const databaseUrl = env.DATABASE_URL
const apiKey = env.API_KEY
```

#### 2. Loading into process.env

```typescript theme={null}
import { loadEnv } from '@orion-js/env'

// Load all variables into process.env
loadEnv()

// Now use process.env as usual
const databaseUrl = process.env.DATABASE_URL
```

## Configuration Options

### Environment Setup

For either approach, you'll need to configure these environment variables:

* `ORION_ENV_FILE_PATH`: Path to your Orion Env configuration file
* `ORION_ENV_SECRET_KEY`: The password to decrypt encrypted variables

You can set these using traditional methods (environment variables, .env files, etc.)

### loadEnv Options

The `loadEnv()` function accepts the following options:

```typescript theme={null}
loadEnv({
  // Secret password used to decrypt the encrypted env file
  // Default: process.env.ORION_ENV_SECRET_KEY
  secretKey: 'your-secret-key',

  // Location of the file to read
  // Default: process.env.ORION_ENV_FILE_PATH
  envFilePath: './config/env.yml',

  // Whether to override existing process.env variables
  // Default: process.env.ORION_ENV_OVERRIDE === truthy
  override: true
})
```

## TypeScript Integration

Orion Env can automatically generate TypeScript definition files for your environment variables, providing autocomplete and type checking.

```typescript theme={null}
import { writeDtsFileFromConfigFile } from '@orion-js/env'

// Generate types file from your config
writeDtsFileFromConfigFile('./config/env.yml', './types/env.d.ts')
```

This will create a declaration file that defines the types for all your environment variables, enabling full TypeScript support.

## Secret Manager Integration

You can configure Orion Env to read variables from JSON-formatted environment variables, which can be populated from secret managers like AWS Secrets Manager, HashiCorp Vault, or similar services.

### Configuration

In your Orion Env configuration file, add a `readFromSecret` section that maps environment variable names to arrays of variable keys:

```yaml theme={null}
version: '2.0'
publicKey: 'pq1:...'
cleanKeys:
  PUBLIC_URL: 'https://example.com'
encryptedKeys:
  DATABASE_PASSWORD: '...'
readFromSecret:
  AWS_SECRETS:
    - DATABASE_URL
    - API_KEY
  GCP_SECRETS:
    - SERVICE_ACCOUNT_KEY
    - STORAGE_BUCKET
```

### How it works

1. Orion Env looks for environment variables specified in the `readFromSecret` object (e.g., `AWS_SECRETS`).
2. If found, it parses the value as JSON.
3. For each variable name in the array (e.g., `DATABASE_URL`), it looks for corresponding properties in the parsed JSON.
4. These values become available in the `env` object alongside your other variables.

### Secret Key Discovery

If the JSON content contains an `ORION_ENV_SECRET_KEY` property, Orion Env will use that value as the secret key for decrypting your encrypted variables. This allows you to store your decryption password in the secret manager as well.

Example JSON in `process.env.AWS_SECRETS`:

```json theme={null}
{
  "DATABASE_URL": "postgresql://user:pass@localhost:5432/db",
  "API_KEY": "xyz123",
  "ORION_ENV_SECRET_KEY": "my-decryption-password"
}
```

## Post-Quantum Encryption

New env files use hybrid post-quantum encryption: every value is encrypted with a fresh [X-Wing](https://datatracker.ietf.org/doc/draft-connolly-cfrg-xwing-kem/) key encapsulation (ML-KEM-768 + X25519) and AES-256-GCM. Being hybrid, values stay protected as long as either ML-KEM or X25519 remains unbroken, so ciphertexts committed today can't be harvested and decrypted later by a quantum computer.

Post-quantum keys and values are prefixed with `pq1:` and the file has `version: '2.0'`. Env files created with older versions (`version: '1.0'`) can still be read, but you should migrate them.

### Migrating an existing env file

```sh theme={null}
pnpm orion-env migrate --path=<path> --secret=<current-secret-key>
```

This decrypts every value with your current secret key, generates a new post-quantum keypair, re-encrypts everything, verifies the result and rewrites the file. It prints the new secret key: **the old one stops working**, so update `ORION_ENV_SECRET_KEY` everywhere it is used (local env, CI, secret managers) when you deploy the migrated file. If `--secret` is omitted you'll be prompted for it.

Running `migrate` on a post-quantum file rotates its keypair.

## Best Practices

1. **Rotate Keys Regularly**: Run `orion-env migrate` periodically to rotate your keypair.
2. **Use CI/CD Variables**: For production environments, consider storing your secret key in CI/CD variables.
3. **Clean vs. Encrypted**: Only store sensitive data (API keys, passwords) as encrypted; public URLs and non-sensitive configs can use cleanKeys.
4. **Version Control**: Always include your env configuration file in version control - that's the whole point!

## CLI Reference

### init

```sh theme={null}
pnpm orion-env init --path=<path>
```

Creates a new Orion Env configuration file with auto-generated encryption keys.

### add

```sh theme={null}
pnpm orion-env add --path=<path>
```

Interactively add a new environment variable to your configuration.

### read

```sh theme={null}
pnpm orion-env read --path=<path> --key=<key>
```

Decrypt and display the value of a specific environment variable.

### migrate

```sh theme={null}
pnpm orion-env migrate --path=<path> --secret=<secret>
```

Migrates the env file to post-quantum encryption (or rotates its keypair), re-encrypting all values. Prints the new secret key.

## File Format

The Orion Env configuration file uses YAML format with the following structure:

```yaml theme={null}
version: '2.0'
publicKey: 'pq1:...'
cleanKeys:
  # Non-sensitive variables stored as plaintext
  PUBLIC_URL: 'https://example.com'
  NODE_ENV: 'development'
encryptedKeys:
  # Sensitive variables stored encrypted
  DATABASE_PASSWORD: '...'
  API_SECRET: '...'
readFromSecret:
  # Variables to read from JSON environment variables
  AWS_SECRETS:
    - DATABASE_URL
    - API_KEY
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.