Course contentsShow
← Web Development
Lesson 1811 of 2,98233. Static Site Generation and JamstackFree lesson

Environment Variables

Managing build-time and runtime environment variables across deployment platforms.

Environment Variables

You'll learn how to securely manage configuration values that differ between build time and runtime across various deployment platforms.

What Are Environment Variables?

Environment variables are name-value pairs that exist outside your code, letting you configure your application differently for development, staging, and production without changing any source files. They're especially crucial for sensitive data like API keys, database credentials, and feature flags that you never want committed to Git.

Build-Time vs Runtime Variables

Build-time variables get baked into your static files during the build process. In SSG projects, these values are replaced in your code before deployment—once a user downloads your HTML/JS, those values are visible in the source.

Runtime variables remain on the server and are evaluated when a request is made (only possible with SSR or serverless functions).

For purely static sites, all variables must be build-time variables.

Platform-Specific Management

Each deployment platform handles environment variables slightly differently:

  • Netlify: Set in the Site Settings UI or netlify.toml
  • Vercel: Configure in Project Settings or .env files with specific naming conventions
  • Cloudflare Pages: Manage through the dashboard under Settings → Environment Variables
  • GitHub Pages: Stored as repository secrets for GitHub Actions workflows

Security Considerations

Never expose sensitive keys in client-side code. Use prefixes like NEXT_PUBLIC_ or VITE_ only for truly public values (like a Google Maps API key restricted by domain). Keep secret keys strictly server-side.

Think of environment variables like backstage passes—some are general admission (build-time, public), others are VIP-only (runtime, private).

Key Takeaway: Environment variables let you configure applications without hardcoding values, but understanding the build-time vs runtime distinction is essential for security and proper static site deployment.