0
0
FastAPIframework~20 mins

Environment-based settings in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Settings Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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}
A{"mode": "production"}
B{"mode": "development"}
C{"mode": "APP_production"}
D{"mode": null}
Attempts:
2 left
💡 Hint
Remember that Pydantic BaseSettings reads environment variables with the prefix defined in Config.
📝 Syntax
intermediate
2: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_"
A
class Settings(BaseSettings):
    database_url: str

    class Config:
        env_prefix = "DB_"
B
class Settings(BaseSettings):
    database_url: str

    class Config:
        env_prefix = DB_
C
"_BD" = xiferp_vne        
:gifnoC ssalc    

rts :lru_esabatad    
:)sgnitteSesaB(sgnitteS ssalc
D
class Settings(BaseSettings):
    database_url: str

    class Config:
        env_prefix = 'DB_'
Attempts:
2 left
💡 Hint
Check how string values are assigned in Python.
state_output
advanced
2: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()
Asettings.app_mode == 'production' and settings.debug == True
Bsettings.app_mode == 'production' and settings.debug == False
Csettings.app_mode == 'testing' and settings.debug == False
Dsettings.app_mode == 'testing' and settings.debug == True
Attempts:
2 left
💡 Hint
Boolean environment variables are parsed by Pydantic automatically.
🔧 Debug
advanced
2: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?
AFastAPI caches environment variables internally and ignores changes.
BEnvironment variables cannot be changed after Python starts.
CSettings are loaded once at app startup and not reloaded dynamically.
DPydantic BaseSettings automatically reloads environment variables on each request.
Attempts:
2 left
💡 Hint
Think about when the settings object is created and how environment variables are read.
🧠 Conceptual
expert
3: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?
AUse environment variables loaded from a secure .env file and never commit it to version control.
BHardcode secrets in the settings class as default values for easy access.
CStore secrets in the source code but encrypt them with a custom algorithm.
DPass secrets as query parameters in API requests to the FastAPI app.
Attempts:
2 left
💡 Hint
Think about common security best practices for managing secrets.