liveBook · Manning

.env file

As explained in Build AI-Enhanced Web Apps

A .env file is a configuration file containing environment-variable assignments, commonly used to provide local application settings and credentials.

A .env file stores environment-variable assignments outside application source code. Backend code reads credentials such as OPENAI_API_KEY or GEMINI_API_KEY from process.env, avoiding hardcoded secrets and supporting provider-specific configuration. section 2.5.5

Use a .env file as part of the local application setup:

  • Create the file in the project root before starting the application. section 2.5.5
  • Add provider credentials as assignments such as OPENAI_API_KEY=<OPENAI_SECRET_KEY> or GEMINI_API_KEY=<GEMINI_SECRET_KEY>. section 2.8.3
  • Load variables in an Express backend with dotenv/config, then retrieve them through process.env. section 2.5.5
  • Add the file to .gitignore so credentials are not committed to version control. section 2.5.5

Next.js can load .env.local or .env automatically, so the dotenv package is unnecessary there. Server-side code can read process.env.OPENAI_API_KEY while keeping the key away from the browser. Only variables prefixed with NEXT_PUBLIC_ become part of the client bundle; reserve that prefix for nonsensitive values such as analytics IDs, public endpoints, or feature flags. section 2.8.3

Provider-backed chat configuration

Create a root .env file with OPENAI_API_KEY=<OPENAI_SECRET_KEY>. Load it with import "dotenv/config"; retrieve the value using const apiKey = process.env.OPENAI_API_KEY; and reject startup when it is missing. The backend can then pass the key to the OpenAI client while the frontend receives only the generated response. section 2.5.5

OPENAI_API_KEY=<OPENAI_SECRET_KEY>
GEMINI_API_KEY= <GEMINI_SECRET_KEY>

Browse the environment-variable listing for OpenAI and Gemini credential assignment syntax. section 2.8.3

How Build AI-Enhanced Web Apps covers it

How deep it goes
Explains file placement, loading, client exposure, and version-control protection, withholds broader deployment configuration.
Examples
Uses OpenAI and Gemini API-key assignments plus a NEXT_PUBLIC_GOOGLE_ANALYTICS_ID example.
What it assumes
Assumes familiarity with JavaScript application servers and process.env while teaching framework-specific loading conventions.
What you can do after
Configure and validate provider credentials for Express.js or Next.js applications while keeping secrets out of client bundles and repositories.
How it connects
Connects environment variables with process.env, dotenv, Express.js, Next.js, API keys, and client-side JavaScript bundles.

Also explained in

Compare how 4 books explain .env file →

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.