Secrets Management and Environment Variables
Distinguish between server-side and client-side env vars, prevent secret leakage in bundles, and use runtime injection.
Secrets Management and Environment Variables is a free React Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Classes of Environment Variables
In a React application, environment variables fall into two classes: server-side (never sent to the browser) and client-side (embedded in the JavaScript bundle). Understanding which class your variables belong to is the first step in secrets management, because client-side variables are effectively public once the bundle is deployed.
Vite Prefix Convention
In Vite projects, only variables prefixed with VITE_ are included in the client bundle. VITE_PUBLIC_API_URL=https://api.example.com is accessible in browser code via import.meta.env.VITE_PUBLIC_API_URL. All other variables are stripped from the bundle at build time and remain server-only.
Next.js Prefix Convention
In Next.js, NEXT_PUBLIC_ prefix exposes a variable to browser JavaScript. NEXT_PUBLIC_ANALYTICS_ID=UA-123 is embedded in the client bundle. Variables without the prefix (like DATABASE_URL) are accessible only in server-side code (Server Components, API routes, Server Actions) and never sent to the browser.
The Accidental Exposure Risk
A non-prefixed variable is still accessible if it is accidentally serialized. For example, if a Server Component passes its full configuration object as a prop to a Client Component, and that config includes DATABASE_URL, that secret appears in the HTML sent to the browser. Careful prop drilling and serialization auditing prevent this.
Never Put These in Client Code
The following must never appear in the client bundle: database connection strings and passwords, third-party API secret keys (Stripe secret key, OpenAI API key), private RSA/EC keys, JWT signing secrets, internal service authentication tokens. If any of these are needed for an API call, proxy that call through a server endpoint.
The Backend for Frontend Pattern
The Backend for Frontend (BFF) pattern places a thin server layer between the React app and sensitive external services. The client calls /api/stripe/charge, the BFF server attaches the Stripe secret key and calls the Stripe API, and the BFF returns only the necessary response data. Secrets stay on the server permanently.
dotenv-vault and Managed Secret Storage
dotenv-vault encrypts your .env file and allows secure sharing and deployment across environments without checking plaintext secrets into version control. Production alternatives include HashiCorp Vault (enterprise), AWS Parameter Store (SSM), and GCP Secret Manager — all provide API-based secret retrieval at runtime rather than at build time.
Secrets in CI/CD Pipelines
CI/CD platforms (GitHub Actions, GitLab CI) provide encrypted secret storage accessible as environment variables during builds. Store secrets in GitHub Secrets, reference them in workflows as ${{ secrets.STRIPE_SECRET_KEY }}. Secrets are masked in logs and are never included in the built artifact.
Environment-Specific Configuration Files
Next.js and Vite support multiple .env files: .env (all environments), .env.local (local overrides, gitignored), .env.development, .env.production. The local file overrides values from the shared file, enabling developer-specific configuration without affecting other environments.
Auditing Bundles for Secrets
After building, audit the JavaScript bundle for accidentally exposed secrets. Run grep -r "password|secret|api_key" dist/ or use a dedicated tool like strings analysis on the built files. Some secrets, if accidentally included, are clearly identifiable by their format (UUIDs, base64 strings, or key prefixes like sk_live_).
Principle of Least Exposure
Each environment and each service should have access only to the secrets it needs. The React client needs no secrets at all — it should only receive public identifiers. The BFF server needs only the secrets required for the APIs it calls. Development environments should use non-production API keys with limited permissions and spending caps.
NEXT_PUBLIC_ Prefix
What happens to a Next.js environment variable with the NEXT_PUBLIC_ prefix?
Lesson Recap
Vite uses VITE_ and Next.js uses NEXT_PUBLIC_ to expose variables to the browser bundle. All other variables are server-side only. Never include database passwords, API secret keys, or JWT signing secrets in client code. Use the BFF pattern to proxy sensitive API calls. Use dotenv-vault, AWS Parameter Store, or GitHub Secrets for production secret management. Audit built bundles for accidentally exposed secrets.
Frequently asked questions
Is the “Secrets Management and Environment Variables” lesson free?
Yes — the full text of “Secrets Management and Environment Variables” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “Secrets Management and Environment Variables”?
Distinguish between server-side and client-side env vars, prevent secret leakage in bundles, and use runtime injection. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Secrets Management and Environment Variables” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- XSS in React: dangerouslySetInnerHTML and Third-Party Scripts
- CSRF Protection in React and API Setups
- Content Security Policy for React Apps
- Secrets Management and Environment Variables