@l8b/env
v1.0.0
Published
Environment variables API for LootiScript
Readme
@l8b/env
Environment Variables API - Read-only access to environment variables from LootiScript.
Overview
The @l8b/env package provides a secure, read-only interface for accessing environment variables from LootiScript game code. Environment variables are loaded from .env files during development and build, and exposed to your game via the global env object.
Installation
This package is part of the L8B monorepo and is automatically available when using @l8b/runtime.
Usage
In LootiScript
function init()
-- Get environment variable
local apiKey = env.get("API_KEY")
local apiUrl = env.get("API_URL") or "https://api.example.com"
-- Check if variable exists
if env.has("DEBUG") then
print("Debug mode enabled")
end
-- Get all keys
local keys = env.keys()
for i = 1, #keys do
print("Env var: " .. keys[i])
end
endEnvironment Files
Create .env files in your project root:
# .env
API_KEY=your-api-key-here
API_URL=https://api.example.com
DEBUG=trueFor different environments:
.env- Default (loaded in all environments).env.local- Local overrides (gitignored).env.development- Development environment.env.production- Production environment
API Reference
env.get(key: string): string | undefined
Get environment variable value by key.
Parameters:
key- Environment variable key (case-sensitive)
Returns:
- Environment variable value as string, or
undefinedif not found
Example:
local apiKey = env.get("API_KEY")
if apiKey then
print("API Key found: " .. apiKey)
endenv.has(key: string): boolean
Check if environment variable exists.
Parameters:
key- Environment variable key (case-sensitive)
Returns:
trueif variable exists,falseotherwise
Example:
if env.has("DEBUG") then
print("Debug mode enabled")
endenv.keys(): string[]
Get all environment variable keys.
Returns:
- Array of all available environment variable keys
Example:
local keys = env.keys()
for i = 1, #keys do
print("Env var: " .. keys[i] .. " = " .. env.get(keys[i]))
endSecurity
- Read-only access: Environment variables can only be read, never modified from LootiScript
- No sensitive data exposure: Only variables explicitly loaded from
.envfiles are available - Case-sensitive: Keys are case-sensitive for consistency
Best Practices
- Never commit
.envfiles: Add.env.localand.env.*.localto.gitignore - Use
.env.example: Create example file with placeholder values - Prefix sensitive keys: Use prefixes like
API_,SECRET_for clarity - Document required vars: List required environment variables in README
Integration
The EnvService is automatically initialized by the runtime orchestrator when environment variables are provided in RuntimeOptions. No manual setup required.
import { RuntimeOrchestrator } from "@l8b/runtime";
const runtime = new RuntimeOrchestrator({
canvas: document.getElementById("game"),
env: {
API_KEY: process.env.API_KEY,
API_URL: process.env.API_URL,
},
});