motoko-env
v0.1.3
Published
A simple utility to make environment variables available in Motoko code
Downloads
12
Maintainers
Readme
motoko-env (Beta)
A simple utility to make environment variables available in Motoko. This library generates a Motoko module (env.mo) from your .env file, allowing you to easily access environment variables in your Motoko projects.
⚠️ This package is currently in beta. API may change in future releases.
Installation
# Install globally
npm install -g motoko-env
# Or run directly with npx
npx motoko-env generateUsage
- Create a
.envfile in your project root:
API_URL=https://example.com/api
CANISTER_ID=rrkah-fqaaa-aaaaa-aaaaq-cai- Run the generate command:
npx motoko-env generate- Import the generated module in your Motoko code:
import Env "../env";
actor {
public func getApiUrl() : async Text {
return Env.API_URL;
};
}Features
- Automatically generates a type-safe Motoko module from your
.envfile - Updates
.gitignoreto prevent committing sensitive environment variables - Detects and handles existing
env.mofiles
Best Practices
- NEVER commit your
env.mofile to version control - Use different
.envfiles for different environments (development, staging, production) - Keep sensitive information like API keys and secrets in environment variables
File Structure
After running motoko-env generate, the tool will:
- Read your
.envfile in the project root - Generate an
env.mofile directly in thesrcdirectory - Update .gitignore to protect sensitive files
your-project/
├── .env # Your environment variables (DO NOT COMMIT)
├── .gitignore # Updated to ignore env.mo and .env
├── dfx.json # Your dfx project configuration
└── src/
├── env.mo # Generated Motoko module (DO NOT COMMIT)
├── your_canister/
└── another_canister/Canisters can then import it directly with import Env "../env";
Security Considerations
This package does not encrypt or protect your environment variables in any way. The generated env.mo file contains your environment variables in plain text and should be treated as sensitive.
Environment variables in Motoko canister actors will be stored in the Wasm module and can be inspected by querying the canister's module. Do not include sensitive API keys or secrets that should remain private.
