Skip to main content

Karma Config Package

The karma/config package helps you manage application configuration using .env files. It includes helpers for loading default configs, custom structs, and individual environment variables — all without hardcoding secrets.
Karma relies heavily on environment variables across all modules. Use this package to manage them effectively.

🔧 Setup

Install karma:
Create a .env file in your project root:

📦 Default Configuration

Use DefaultConfig() to load all predefined environment variables into a Config struct.
The Config struct includes a comprehensive list of fields such as:
  • Port, JWTSecret, DatabaseURL, RedisURL, etc.
  • AWS, Twilio, Stripe, OpenAI, and Mailgun credentials
  • Custom webhook and platform keys (KARMAPAY_*)
Check the full struct in the GitHub Repo for supported variables.

🧩 Custom Struct Configuration

Use CustomConfig(cfg interface{}) if you only need a subset of variables or a custom struct layout.

Example

Each field must be tagged with env:"VARIABLE_NAME". If the env tag is missing, it defaults to the field name.

🔍 Get Individual Variables

Use these helpers when you want to fetch specific keys:

GetEnv

Returns the value or an error if .env fails to load.

GetEnvOrDefault

Returns a default value if the variable is missing or empty.

GetEnvRaw

Returns the value directly as a string. Silent on load errors.

🧪 Example .env Template


🧠 Pro Tips

  • Always use CustomConfig() for CLI tools or microservices with limited env requirements.
  • Store .env at the root of your project and never commit it to version control.
  • Prefer GetEnvOrDefault() when building tools or scripts that work in local/dev environments.