alchemy.secret()
Wraps a sensitive value so it will be encrypted when stored in state files. Requires a password to be set either globally in the alchemy application options or locally in analchemy.run() scope.
Signature
Parameters
T
required
The sensitive value to encrypt in state files. Cannot be
undefined.string
Optional name for the secret. Used for debugging and logging. If not provided, an auto-generated name will be used.
Returns
Secret<T>
Environment Variable Helper
alchemy.secret.env
A convenient helper for creating secrets from environment variables with better error messages.alchemy.secret(process.env.X):
- Automatically throws if the environment variable is not set
- Provides clear error messages with the variable name
- More concise syntax
Password Configuration
Secrets require a password for encryption/decryption. The password can be provided in two ways:Global Password
Set a password when creating the application scope:Scoped Password
Set a password for a specific scope usingalchemy.run():
Secret Class
TheSecret class provides static methods for working with secrets:
Secret.wrap()
Ensures a value is wrapped in a Secret.T | Secret<T>
required
The value to wrap. If already a Secret, returns it unchanged.
Secret.unwrap()
Unwraps a Secret if it is wrapped, otherwise returns the value.T | Secret<U>
required
The value to unwrap.
Type Guard
isSecret()
Checks if a value is a Secret wrapper.any
required
The value to check.
true if the value is a Secret, false otherwise.
Example:
Examples
Basic Usage
Using Environment Variable Helper
Flexible Input Types
State File Representation
When secrets are stored in state files (.alchemy/{stage}/{resource}.json), they are encrypted:
Scoped Secrets
Error Recovery
If you lose the encryption password, you can use--erase-secrets to recover:
Security Best Practices
- Never commit passwords to version control: Store passwords in environment variables or use a secret management service.
-
Use different passwords for different stages:
- Rotate passwords periodically: Update your password and redeploy to re-encrypt all secrets.
-
Use
alchemy.secret.envfor better error messages: -
Don’t log or print secrets:
Related
- alchemy() - Create application scope with password
- alchemy.run() - Create scoped password
- Resource - Using secrets in resources