liveBook · Manning

Environment variable

As explained in Build AI-Enhanced Web Apps

A named configuration value supplied to a process through its execution environment rather than hard-coded application source.

An environment variable is a named configuration value supplied to an application rather than hard-coded in its source. In a web application, it can provide API credentials, service URLs, or a simple feature toggle. The important distinction is where the value will be read: server-side code can use secrets, while browser-visible configuration must be safe to disclose. section 2.8.3 section 10.3.2

Configure variables according to the application runtime:

  • In Express.js, load a local `.env` file with `dotenv`, then read values through `process.env`. Keep the file out of version control with `.gitignore`. section 2.5.5
  • In Next.js, place local values in `.env.local` or `.env`; Next.js loads them into `process.env` without a separate `dotenv` setup. Read secrets only in server-side code, such as API routes. section 2.8.3
  • Prefix a variable with `NEXT_PUBLIC_` only when client-side code needs it. Its value is embedded in the browser JavaScript bundle, so never use that prefix for API keys, database credentials, or other secrets. section 2.8.3

Read a server-side API key

For a Next.js server route, set `OPENAI_API_KEY` locally and read it as `process.env.OPENAI_API_KEY`. In an Express backend, a missing-key check can throw `Missing OPENAI_API_KEY environment variable` before the application serves requests. Keep authenticated calls to the provider on the server rather than passing the key to the browser. section 2.8.3 section 2.5.5 section 9.4.2

// .env
GEMINI_API_KEY=<GEMINI_AI_SECRET_KEY>
UPSTASH_REDIS_REST_URL=<UPSTASH_REDIS_REST_URL>
UPSTASH_REDIS_REST_TOKEN=<UPSTASH_REDIS_REST_TOKEN>
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=<NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY>
CLERK_SECRET_KEY=<CLERK_SECRET_KEY>

The chat deployment configuration separates server-side Gemini, Upstash, and Clerk secrets from the browser-visible Clerk publishable key. section 9.6.3

For deployment, configure values in the Vercel dashboard instead of hard-coding them in the repository; Vercel can inject them into the application. Environment variables also work as quick feature flags, but a dedicated feature-management service offers more control when rollout must vary by user segment. section 9.4.1 section 10.3.2

How Build AI-Enhanced Web Apps covers it

How deep it goes
Explains local loading, server-versus-browser exposure, and deployment configuration, with a missing-key check; does not elaborate secret rotation.
Examples
Uses OpenAI and Gemini API keys, Upstash connection values, and Clerk publishable and secret keys in application configurations.
What it assumes
Assumes familiarity with JavaScript web applications while introducing Next.js environment-file loading and the `NEXT_PUBLIC_` exposure rule.
What you can do after
Operate/configure it by separating public values from server secrets and setting deployment variables in Vercel.
How it connects
Connects environment variables to API routes, API key management, browser bundles, feature flags, and Vercel deployment.

Also explained in

Compare how 19 books explain environment variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.