Challenge - 5 Problems
Environment Settings Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does FastAPI behave with environment-based settings?
Given a FastAPI app that loads settings from environment variables using Pydantic's BaseSettings, what will be the output when accessing the `/info` endpoint if the environment variable `APP_MODE` is set to `production`?
FastAPI
from fastapi import FastAPI from pydantic import BaseSettings class Settings(BaseSettings): app_mode: str = "development" class Config: env_prefix = "APP_" settings = Settings() app = FastAPI() @app.get("/info") async def get_info(): return {"mode": settings.app_mode}
Attempts:
2 left
💡 Hint
Remember that Pydantic BaseSettings reads environment variables with the prefix defined in Config.
✗ Incorrect
The Settings class uses env_prefix 'APP_', so it looks for environment variable 'APP_MODE'. If set to 'production', settings.app_mode will be 'production'.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in environment settings loading
Which option contains a syntax error when defining environment-based settings with Pydantic in FastAPI?
FastAPI
from pydantic import BaseSettings class Config(BaseSettings): database_url: str class Config: env_prefix = "DB_"
Attempts:
2 left
💡 Hint
Check how string values are assigned in Python.
✗ Incorrect
Option B misses quotes around DB_, causing a NameError or SyntaxError because DB_ is undefined.
❓ state_output
advanced2:00remaining
What is the value of settings after loading multiple environment variables?
Consider this settings class and environment variables set as `APP_MODE=testing` and `APP_DEBUG=true`. What will be the value of `settings.debug` and `settings.app_mode`?
FastAPI
from pydantic import BaseSettings class Settings(BaseSettings): app_mode: str = "production" debug: bool = False class Config: env_prefix = "APP_" settings = Settings()
Attempts:
2 left
💡 Hint
Boolean environment variables are parsed by Pydantic automatically.
✗ Incorrect
Pydantic reads APP_MODE and APP_DEBUG from environment, converting 'true' to True boolean.
🔧 Debug
advanced2:00remaining
Why does the FastAPI app not reflect updated environment variables?
A developer changes environment variables after the FastAPI app has started but the app still shows old settings values. What is the most likely reason?
Attempts:
2 left
💡 Hint
Think about when the settings object is created and how environment variables are read.
✗ Incorrect
Settings instance is created once at startup, so changes to environment variables after that won't affect the running app.
🧠 Conceptual
expert3:00remaining
Which approach ensures secure environment-based settings in FastAPI?
To keep sensitive data like API keys secure and configurable per environment, which practice is best when using FastAPI with environment-based settings?
Attempts:
2 left
💡 Hint
Think about common security best practices for managing secrets.
✗ Incorrect
Using environment variables and .env files excluded from version control is a standard secure practice.