0Pricing
MCP Academy · 课时

通过环境配置设置与机密信息

加载设置,而不将凭据硬编码。

通过环境配置设置与机密信息 是 CoddyKit 上的免费 MCP Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 MCP Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 MCP Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Never Hardcode Secrets

An API key typed straight into your code can leak the moment you push to Git. Keep secrets out of source files entirely.

The Environment Holds Config

The standard place for settings is the environment. Your server reads values at startup instead of baking them into the build.

import os
api_key = os.environ["WEATHER_API_KEY"]

Same Code, Many Setups

Reading from the environment lets one build run in dev, staging, and prod by simply swapping the variables around it.

Required vs Optional Values

Use indexing for a required value so a missing key fails loudly, and getenv with a default for optional ones.

key = os.environ["DB_URL"]
timeout = int(os.getenv("TIMEOUT", "30"))

Validate Config at Startup

Check every setting when the server boots, not on the first request. Fail early with a clear message about what is missing.

Centralize in a Settings Class

Gather all values into one settings object so the rest of your code reads tidy attributes instead of scattered getenv calls.

class Settings:
    db_url = os.environ["DB_URL"]
    timeout = int(os.getenv("TIMEOUT", "30"))

Pydantic Settings Helps

The BaseSettings class reads env vars, casts types, and validates them for you, turning loose strings into a checked config object.

from pydantic_settings import BaseSettings
class Settings(BaseSettings):
    db_url: str
    timeout: int = 30

Use .env for Local Dev

A local .env file keeps your machine's values handy, but it must be gitignored so those values never reach the repository.

DB_URL=postgres://localhost/dev
TIMEOUT=15

Never Log Secrets

When you print config for debugging, mask sensitive fields. A token in a log line is just as exposed as one in the code.

Inject Config, Don't Reach for It

Load settings once and pass them into your services through the lifespan. Avoid scattering os.environ reads deep in the code.

Document Every Variable

Keep a sample .env.example listing each variable with a comment. New users can copy it and know exactly what to fill in.

Quick Check

What is the safest way to give your server an API key?

Recap: Config & Secrets

Read settings from the environment, validate at boot, centralize in a settings object, and never commit or log secrets. Next: idempotency. 🔑

常见问题解答

「通过环境配置设置与机密信息」课时是免费的吗?

是的 — 「通过环境配置设置与机密信息」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 MCP Academy 课程的其余内容,请升级到 CoddyKit PRO。 MCP Academy 课程共包含 4 节课。

「通过环境配置设置与机密信息」这节课中我会学到什么?

加载设置,而不将凭据硬编码。 你通过在浏览器中直接运行的动手代码来练习 MCP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 MCP Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 MCP Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「通过环境配置设置与机密信息」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 MCP Academy 课中编写并运行代码吗?

能。每节 MCP Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 分层服务器架构
  2. 通过环境配置设置与机密信息
  3. 幂等且感知副作用的工具
  4. 工具与模式的版本管理
← 返回 MCP Academy