环境变量与机密信息管理
通过管理机密信息、分离公开变量与私有变量,并避免将密钥泄露给客户端,安全地配置跨环境运行的 SaaS。
环境变量与机密信息管理 是 CoddyKit 上的免费 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Powered SaaS: Stripe + Auth + Billing + Deploy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Configuration Matters
A SaaS connects to databases, payment providers, and AI APIs — each with keys that differ per environment. Environment variables keep them out of code so one codebase runs anywhere.
The .env File
Local config lives in a .env file as plain key-value pairs. It's loaded at startup and must never be committed to git.
DATABASE_URL=postgresql://localhost/app
STRIPE_SECRET_KEY=sk_test_123
OPENAI_API_KEY=sk-abcNever Commit Secrets
Never commit secrets: add .env to .gitignore and ship a .env.example with empty values, so teammates know what to fill in without seeing real keys.
# .gitignore
.env
.env.localPublic vs Private Variables
In Next.js, only variables prefixed NEXT_PUBLIC_ reach the browser bundle — everything else stays server-only. Never prefix a real secret, or every visitor sees it.
NEXT_PUBLIC_APP_URL=https://app.com # safe in browser
STRIPE_SECRET_KEY=sk_live_xxx # server onlyReading Variables
Read variables through process.env. Server code can access any of them; client code only ever sees the public, NEXT_PUBLIC_ ones.
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
const url = process.env.NEXT_PUBLIC_APP_URL;Validating Env at Startup
A missing key should fail fast, not at 2am in production. Validate required variables on boot with a schema like Zod so problems surface immediately.
import { z } from 'zod';
const env = z.object({
DATABASE_URL: z.string().url(),
STRIPE_SECRET_KEY: z.string().min(1)
}).parse(process.env);Per-Environment Files
Next.js loads .env.local, then .env.development or .env.production. These per-environment files keep different values cleanly separated.
Secrets in Hosting Platforms
In production you don't ship a .env file. Set variables in your host's dashboard — Vercel, Render — so they're injected securely at runtime.
vercel env add STRIPE_SECRET_KEY productionRotating Keys
If a key leaks, rotate it: generate a new one, update your env store, redeploy. Because keys live outside code, rotation is quick and low-risk.
Avoiding Common Leaks
Watch for common leaks: logging full process.env, returning secrets in API responses, hardcoding keys as fallbacks, or committing a real .env.
Best Practices
Best practices: keep secrets in .env and out of git, expose only truly public values with NEXT_PUBLIC_, validate at startup, and store prod secrets in your host.
Quick Check
Test your secrets knowledge.
Recap
Recap: store config in .env and keep it out of git, use NEXT_PUBLIC_ only for safe values, validate at startup, and set prod secrets in your host.
用 AI 导师学习 AI Powered SaaS: Stripe + Auth + Billing + Deploy — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「环境变量与机密信息管理」课时是免费的吗?
是的 — 「环境变量与机密信息管理」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程的其余内容,请升级到 CoddyKit PRO。 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程共包含 4 节课。
「环境变量与机密信息管理」这节课中我会学到什么?
通过管理机密信息、分离公开变量与私有变量,并避免将密钥泄露给客户端,安全地配置跨环境运行的 SaaS。 你通过在浏览器中直接运行的动手代码来练习 AI Powered SaaS: Stripe + Auth + Billing + Deploy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Powered SaaS: Stripe + Auth + Billing + Deploy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「环境变量与机密信息管理」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课中编写并运行代码吗?
能。每节 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- SaaS 与人工智能协同简介
- 选择技术栈
- 项目初始化与结构
- 环境变量与机密信息管理