Advanced

Environment Variables

BanglaCode provides built-in support for loading and managing environment variables from .env files, with multi-environment support for development, staging, and production configurations.

Why Use .env Files?

Environment variables help you manage configuration settings across different environments without hardcoding sensitive data like API keys, database passwords, or URLs in your code.

  • Security - Keep secrets out of version control
  • Flexibility - Different configs for dev, staging, production
  • Portability - Same code works across environments
  • Best Practice - Industry-standard configuration management

Environment Variable Functions

env_load(filename)

Load environment variables from a specific .env file

env_load_auto(environment)

Automatically load .env.{environment} or fallback to .env

env_get(key)

Get environment variable value (throws error if not found)

env_get_default(key, default)

Get environment variable with a default fallback value

env_set(key, value)

Set environment variable at runtime

env_all()

Get all environment variables as a map

env_clear()

Clear all loaded environment variables

Creating .env Files

Create a .env file in your project root with KEY=VALUE pairs:

1# Database Configuration
2DB_HOST=localhost
3DB_PORT=5432
4DB_NAME=myapp
5DB_USER=admin
6DB_PASSWORD=secret123
7
8# API Configuration
9API_KEY=your_api_key_here
10API_URL=http://localhost:3000
11SECRET_KEY=your_secret_key
12
13# Application Settings
14APP_NAME=My BanglaCode App
15NODE_ENV=development
16DEBUG=true
17PORT=8080

Loading Environment Variables

Basic Loading

1// Load .env file
2env_load(".env");
3
4// Get environment variables
5dhoro api_key = env_get("API_KEY");
6dhoro api_url = env_get("API_URL");
7dhoro app_name = env_get("APP_NAME");
8
9dekho("App Name:", app_name);
10dekho("API URL:", api_url);
11dekho("API Key:", api_key);

Using Default Values

1// Load with defaults (safer)
2env_load(".env");
3
4dhoro api_url = env_get_default("API_URL", "http://localhost:3000");
5dhoro debug = env_get_default("DEBUG", "false");
6dhoro port = env_get_default("PORT", "8080");
7
8dekho("API URL:", api_url);
9dekho("Debug Mode:", debug);
10dekho("Server Port:", port);

Multi-Environment Support

BanglaCode supports multiple environment files: .env, .env.dev,.env.uat, .env.staging, .env.prod

Environment-Specific Files

.env (Default/Development)

1API_URL=http://localhost:3000
2DB_HOST=localhost
3DEBUG=true

.env.prod (Production)

1API_URL=https://api.production.com
2DB_HOST=prod-db.example.com
3DEBUG=false

.env.uat (User Acceptance Testing)

1API_URL=https://api.uat.example.com
2DB_HOST=uat-db.example.com
3DEBUG=true

Auto-Loading with Fallback

1// Load production environment
2// Tries .env.prod first, then falls back to .env if not found
3env_load_auto("prod");
4
5dhoro api_url = env_get("API_URL");
6dekho("Production API:", api_url); // Uses .env.prod value
7
8// Load UAT environment
9env_clear(); // Clear previous env vars
10env_load_auto("uat");
11
12dhoro uat_url = env_get("API_URL");
13dekho("UAT API:", uat_url); // Uses .env.uat value
14
15// Load non-existent environment (fallbacks to .env)
16env_clear();
17env_load_auto("staging"); // No .env.staging, uses .env
18
19dhoro default_url = env_get("API_URL");
20dekho("Fallback API:", default_url); // Uses .env value

Runtime Environment Variables

1// Load .env file
2env_load(".env");
3
4// Set runtime variables
5env_set("CURRENT_USER", "Rahim Ahmed");
6env_set("SESSION_ID", "abc123xyz");
7env_set("REQUEST_COUNT", "42");
8
9// Get runtime variables
10dhoro user = env_get("CURRENT_USER");
11dhoro session = env_get("SESSION_ID");
12
13dekho("Current User:", user);
14dekho("Session ID:", session);

Practical Examples

Database Connection with .env

1// Load environment-specific config
2env_load_auto("prod");
3
4// Get database credentials from .env
5dhoro db_host = env_get("DB_HOST");
6dhoro db_port = env_get_default("DB_PORT", "5432");
7dhoro db_name = env_get("DB_NAME");
8dhoro db_user = env_get("DB_USER");
9dhoro db_pass = env_get("DB_PASSWORD");
10
11// Connect to database using env variables
12dhoro conn = db_jukto("postgres", {
13 "host": db_host,
14 "port": sonkha(db_port),
15 "database": db_name,
16 "user": db_user,
17 "password": db_pass
18});
19
20dhoro users = db_query(conn, "SELECT * FROM users");
21dekho("Found", dorghyo(users["rows"]), "users");
22
23db_bandho(conn);

HTTP Server with Configuration

1// Load environment config
2env_load_auto("dev");
3
4// Get server settings
5dhoro host = env_get_default("HOST", "0.0.0.0");
6dhoro port = sonkha(env_get_default("PORT", "8080"));
7dhoro app_name = env_get_default("APP_NAME", "My App");
8
9dekho("Starting", app_name, "on", host + ":" + lipi(port));
10
11// Start server
12server_chalu(port, kaj(req) {
13 dhoro api_key = env_get("API_KEY");
14
15 // Validate API key from request
16 jodi (req.headers["x-api-key"] != api_key) {
17 ferao json_uttor(401, {error: "Invalid API key"});
18 }
19
20 ferao json_uttor(200, {
21 message: "Welcome to " + app_name,
22 environment: env_get_default("NODE_ENV", "development")
23 });
24});

Multi-Environment Deployment Script

1kaj deploy(environment) {
2 dekho("========================================");
3 dekho("Deploying to:", boroHater(environment));
4 dekho("========================================");
5
6 // Load environment-specific config
7 env_load_auto(environment);
8
9 // Display configuration
10 dekho("");
11 dekho("Configuration:");
12 dekho(" API URL:", env_get("API_URL"));
13 dekho(" DB Host:", env_get("DB_HOST"));
14 dekho(" Debug Mode:", env_get_default("DEBUG", "false"));
15 dekho("");
16
17 // Get all env vars for deployment
18 dhoro all_vars = env_all();
19 dekho("Total env variables:", dorghyo(chabi(all_vars)));
20
21 // Deployment logic here...
22 dekho("Deployment successful!");
23}
24
25// Usage
26deploy("uat"); // Deploy to UAT
27deploy("prod"); // Deploy to production

Configuration Manager

1sreni Config {
2 shuru(environment) {
3 ei.env = environment;
4 ei.loaded = mittha;
5 ei.load();
6 }
7
8 kaj load() {
9 dekho("Loading config for:", ei.env);
10 env_load_auto(ei.env);
11 ei.loaded = sotti;
12 }
13
14 kaj get(key, defaultValue) {
15 jodi (na ei.loaded) {
16 ei.load();
17 }
18
19 jodi (defaultValue != khali) {
20 ferao env_get_default(key, defaultValue);
21 }
22
23 ferao env_get(key);
24 }
25
26 kaj reload() {
27 env_clear();
28 ei.loaded = mittha;
29 ei.load();
30 }
31
32 kaj displayAll() {
33 dhoro vars = env_all();
34 dhoro keys = chabi(vars);
35
36 dekho("Environment Variables:");
37 ghuriye (dhoro i = 0; i < dorghyo(keys); i = i + 1) {
38 dhoro key = keys[i];
39 dekho(" ", key, "=", vars[key]);
40 }
41 }
42}
43
44// Usage
45dhoro config = notun Config("prod");
46
47dhoro api_url = config.get("API_URL");
48dhoro db_host = config.get("DB_HOST");
49dhoro port = config.get("PORT", "3000");
50
51dekho("API URL:", api_url);
52dekho("DB Host:", db_host);
53dekho("Port:", port);
54
55// Display all config
56config.displayAll();
57
58// Reload config
59config.reload();

Secret Validation

1kaj validateSecrets() {
2 dhoro required = [
3 "API_KEY",
4 "DB_PASSWORD",
5 "SECRET_KEY"
6 ];
7
8 dhoro missing = [];
9
10 ghuriye (dhoro i = 0; i < dorghyo(required); i = i + 1) {
11 dhoro key = required[i];
12
13 chesta {
14 env_get(key);
15 } dhoro_bhul (error) {
16 dhokao(missing, key);
17 }
18 }
19
20 jodi (dorghyo(missing) > 0) {
21 dekho("ERROR: Missing required environment variables:");
22 ghuriye (dhoro i = 0; i < dorghyo(missing); i = i + 1) {
23 dekho(" -", missing[i]);
24 }
25 ferao mittha;
26 }
27
28 dekho("All required secrets present!");
29 ferao sotti;
30}
31
32// Load config
33env_load_auto("prod");
34
35// Validate before starting app
36jodi (validateSecrets()) {
37 dekho("Starting application...");
38 // Start app logic
39} nahole {
40 dekho("Cannot start app without required secrets!");
41}

Best Practices

  • Never commit .env files - Add .env* to .gitignore
  • Use .env.example - Commit a template without sensitive values
  • Use descriptive variable names - DB_HOST not H1
  • Provide defaults - Use env_get_default() for non-critical values
  • Validate secrets - Check required variables exist before starting
  • Document environment files - Explain what each variable does
  • Use environment-specific files - .env.dev, .env.prod
  • Rotate secrets regularly - Update API keys and passwords periodically

Security Tips

⚠️ Important Security Notes

  • Never log sensitive values - Don't use dekho() with passwords or API keys
  • Use .gitignore - Prevent accidentally committing .env files
  • Encrypt production secrets - Use secret management tools for production
  • Limit access - Only authorized people should have access to .env files
  • Monitor for leaks - Use tools to scan for exposed secrets

Example .env.example Template

1# Example .env file for BanglaCode
2# Copy this to .env and fill in your actual values
3
4# Database Configuration
5DB_HOST=localhost
6DB_PORT=5432
7DB_NAME=myapp
8DB_USER=admin
9DB_PASSWORD=your_password_here
10
11# API Configuration
12API_KEY=your_api_key_here
13API_URL=http://localhost:3000
14SECRET_KEY=your_secret_key_here
15
16# Application Settings
17APP_NAME=BanglaCode App
18NODE_ENV=development
19DEBUG=true
20PORT=8080
21
22# Email Settings (optional)
23EMAIL_HOST=smtp.gmail.com
24EMAIL_PORT=587
25EMAIL_USER=your_email@gmail.com
26EMAIL_PASSWORD=your_email_password