Skip to main content

env()

Safely access environment variables in your tools.

Function Signature

string
required
Environment variable name to retrieve
Returns: 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 file
For 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:
Add .env to .gitignore - never commit secrets!
Create .env.example for documentation:

Method 2: System Environment

Set in your shell:

Best Practices

Environment variables are always strings. Convert when needed:

Common Patterns

Pattern: Required Variable

Pattern: Validation

Pattern: Caching

Security

Never commit secrets to version control!
  • Add .env to .gitignore
  • Use .env.example for documentation
  • Store production secrets using lua env command (server-managed)
  • Rotate keys regularly

.gitignore

Troubleshooting

Problem: env('MY_VAR') returns undefinedSolutions:
  1. Check spelling in .env file
  2. Ensure .env is in project root
  3. Restart CLI command (variables loaded at startup)
  4. For production, use lua env to verify variables on server
Problem: Updated .env but value unchangedSolution: Restart the command:
Or use lua env to verify and update variables

Next Steps

Environment Variables Guide

Complete guide to configuration

Payment Tool Example

See environment variables in action