Web Development
26 Aug 2026
Environment Variables Explained
A quick guide to environment variables, .env files, secrets, and environment-specific configuration.

Environment variables are values stored outside the source code that can be used to configure an application.
They are commonly used for things like API keys, database URLs, application settings, and other values that may differ between environments.
Why use environment variables?
Instead of putting sensitive values directly in the code:
const apiKey = "my-secret-api-key";
you can store them in an environment file:
API_KEY=my-secret-api-key
and access them from your application:
const apiKey = process.env.API_KEY;
This keeps configuration separate from the source code and makes it easier to use different values in development and production.
.env files
A common setup is to use a .env file locally:
DATABASE_URL=postgresql://... STRIPE_SECRET_KEY=...
For Next.js projects, .env.local is commonly used for local development.
Environment files containing secrets should not be committed to Git. They are usually added to .gitignore instead.
Deploying Environment Variables
Local .env files are useful during development, but production applications need their environment variables to be configured on the hosting platform as well.

For example, Vercel allows environment variables to be added directly to a project's settings. These values are then available to the application during deployment and runtime.
Public environment variables
Some frameworks distinguish between server-only variables and values that can be exposed to the browser.
For example, in Next.js, variables prefixed with NEXTPUBLIC can be included in client-side code:
NEXT_PUBLIC_API_URL=https://api.example.com
This means you should never put secret keys or passwords in NEXTPUBLIC variables.
Development and production
Environment variables also make it possible to use different configuration for different environments.
# Development DATABASE_URL=local_database # Production DATABASE_URL=production_database
The application can use the same code while the environment provides the appropriate configuration.
Conclusion
Environment variables provide a simple way to separate application configuration from source code.
They are especially useful for managing secrets, environment-specific settings, and external services such as databases and APIs.