Managing Secrets
Alchemy provides a built-in Secret system for handling sensitive values like API keys, passwords, and credentials. Secrets are automatically encrypted when stored in state files using a password.Creating Secrets
Usealchemy.secret() to wrap sensitive values:
Secrets require a password to be set. Without a password, secret operations will fail.
Setting the Password
The password is used to encrypt and decrypt secrets. You can provide it in two ways:Global Password
Set the password when creating your application:Environment Variable
Alchemy automatically reads from theALCHEMY_PASSWORD environment variable:
Secret from Environment Variables
Alchemy provides a convenient helper for creating secrets from environment variables:Using secret.env
Using secret()
How Secrets Work
Encryption in State Files
When resources are saved to state files (.alchemy/), secrets are automatically encrypted:
Secret Lifecycle
Named Secrets
You can assign names to secrets for better debugging:Secret Wrapping and Unwrapping
TheSecret class provides utilities for working with secret values:
Wrap
Ensure a value is wrapped in a Secret:Unwrap
Extract the unencrypted value from a Secret:Type Guard
Check if a value is a Secret:Secret Safety Features
toString Protection
Secrets overridetoString() to prevent accidental exposure:
console.log Protection
Secrets implement custom inspect for Node.js:Scoped Secrets
You can use different passwords for different scopes:Recovering from Lost Passwords
If you lose your encryption password, you can erase secrets and start fresh:Best Practices
Example: Complete Secret Setup
Next Steps
- Creating Resources - Learn how to create infrastructure resources
- Local Development - Test with secrets locally
- Deployment - Deploy with secrets to production