0
0
FastAPIframework~20 mins

Environment variable management in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Variable Mastery in FastAPI
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does FastAPI load environment variables with Pydantic BaseSettings?
Given this FastAPI settings class using Pydantic's BaseSettings, what will be the value of settings.api_key if the environment variable API_KEY is set to secret123?
FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    api_key: str

settings = Settings()
A"api_key"
B"secret123"
CRaises a ValidationError because no default is set
D"" (empty string)
Attempts:
2 left
💡 Hint
Pydantic BaseSettings automatically reads environment variables matching attribute names in uppercase.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this FastAPI environment variable loading code
Which option contains the correct way to define a Pydantic BaseSettings class to load an environment variable DATABASE_URL?
FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    database_url: str

settings = Settings()
A
class Settings(BaseSettings):
    database_url: str

settings = Settings()
B
class Settings(BaseSettings):
    database_url = str

settings = Settings()
C
class Settings(BaseSettings):
    database_url: str = None

settings = Settings()
D
class Settings(BaseSettings):
    database_url: str

settings = Settings
Attempts:
2 left
💡 Hint
Check how type annotations and instantiation are done.
state_output
advanced
2:00remaining
What is the output when environment variable is missing and no default is set?
Consider this FastAPI settings class. What happens when you run settings = Settings() but the environment variable SECRET_KEY is not set?
FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    secret_key: str

settings = Settings()
Asettings.secret_key is set to 'secret_key'
Bsettings.secret_key is set to an empty string
Csettings.secret_key is set to None
DRaises pydantic.ValidationError because secret_key is required but missing
Attempts:
2 left
💡 Hint
Required fields without defaults must be provided by environment or raise error.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI app fail to read environment variables correctly?
This FastAPI app uses Pydantic BaseSettings but always returns default values instead of environment variables. What is the likely cause?
FastAPI
from fastapi import FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    app_name: str = "MyApp"

settings = Settings()

app = FastAPI()

@app.get("/")
def read_root():
    return {"app_name": settings.app_name}
AThe app_name field must be Optional[str] to read environment variables
BBaseSettings does not support environment variables by default
CEnvironment variables are not loaded because the .env file is missing or not in the working directory
DFastAPI requires special middleware to read environment variables
Attempts:
2 left
💡 Hint
Check if environment variables are actually available to the app process.
🧠 Conceptual
expert
3:00remaining
How does Pydantic BaseSettings prioritize environment variables and defaults?
Given a Pydantic BaseSettings class with a field timeout: int = 30, and an environment variable TIMEOUT=60 set, what will be the value of settings.timeout after instantiation?
FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    timeout: int = 30

settings = Settings()
A60, because environment variables override default values
B30, because default values always take precedence over environment variables
CNone, because the environment variable is ignored
DRaises ValidationError because environment variables cannot override defaults
Attempts:
2 left
💡 Hint
Think about how BaseSettings merges environment variables with defaults.