Thuta Learning
AdvancedWeb Developmentintermediate

Environment Variables and Security Basics

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Distinguish server secrets from public variables
  • Validate environment variables at startup
  • Write a basic production security checklist

Just keeping your API key out of Git isn't enough. Any variable with the NEXT_PUBLIC_ prefix can end up in the browser bundle, so it's no longer a secret. The first thing to figure out is where the variable is actually used.

The key idea

.env.local is for local secrets, so it should never be committed. A server-only variable like DATABASE_URL should have no prefix at all. Reserve NEXT_PUBLIC_ for things the browser genuinely needs, like a public analytics id. Validate at app startup so a missing variable doesn't crash things mid-runtime. On top of that, your production checklist should cover user input validation, output escaping, security headers, CSP, HTTPS, and keeping dependencies patched.

Let's try it together

typescript
// .env.local (Git ထဲမထည့်ပါနှင့်)
DATABASE_URL="postgres://..."
NEXT_PUBLIC_APP_NAME="Myanmar Notes"

// lib/env.ts
import "server-only";

const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) throw new Error("DATABASE_URL is required");

export const env = { databaseUrl };

// .env.example
DATABASE_URL=
NEXT_PUBLIC_APP_NAME=Myanmar Notes

How the code works

The env helper catches a missing DATABASE_URL early. Client Components must never import this module. .env.example only lists key names, never real values.

You should see
Secrets stay server-only, and a clear error appears at startup if the environment isn't fully configured.

5-Minute Try-It

Split your project's environment variables into public and server-only groups, then create a .env.example for it.

Next.js — Environment VariablesNext.js

Easy traps

  • Putting a secret key behind the NEXT_PUBLIC_ prefix
  • Committing .env.local to Git

Exercise

Split your project's environment variables into public and server-only groups, then create a .env.example for it.

You'll know it worked when: Secrets stay server-only, and a clear error appears at startup if the environment isn't fully configured.

Environment Variables and Security Basics | Thuta Learning