env()
Safely access environment variables in your tools.Function Signature
string
required
Environment variable name to retrieve
string | undefined - Value of the environment variable, or undefined if not set
Loading Priority
Environment variables are loaded in this order (later overrides earlier):1
System Environment
Variables from your shell/system
2
.env File
Variables from project
.env fileFor production, use
lua env command to manage variables on the server.Examples
Basic Usage
With Default Values
Multiple Environment Variables
Environment-Specific Configuration
Setting Variables
Method 1: .env File (Local Development)
Create.env in project root:
.env.example for documentation:
Method 2: System Environment
Set in your shell:Best Practices
Validate Required Variables
Validate Required Variables
Use Descriptive Names
Use Descriptive Names
Provide Defaults for Non-Secrets
Provide Defaults for Non-Secrets
Don't Log Sensitive Values
Don't Log Sensitive Values
Type Conversion
Type Conversion
Environment variables are always strings. Convert when needed:
Common Patterns
Pattern: Required Variable
Pattern: Validation
Pattern: Caching
Security
.gitignore
Troubleshooting
Variable not found
Variable not found
Problem:
env('MY_VAR') returns undefinedSolutions:- Check spelling in
.envfile - Ensure
.envis in project root - Restart CLI command (variables loaded at startup)
- For production, use
lua envto verify variables on server
Changes not taking effect
Changes not taking effect
Problem: Updated Or use
.env but value unchangedSolution: Restart the command:lua env to verify and update variablesNext Steps
Environment Variables Guide
Complete guide to configuration
Payment Tool Example
See environment variables in action

