0
0
FastAPIframework~10 mins

Environment-based settings in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load environment variables using Pydantic BaseSettings.

FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    app_name: str

settings = Settings([1])
Drag options to blanks, or click blank then click option'
Afile='.env'
Bconfig_file='.env'
Cenv_file='.env'
Dload_env='.env'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names like config_file or file.
Not passing any parameter and expecting automatic loading.
2fill in blank
medium

Complete the code to access the environment variable DATABASE_URL from settings.

FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    database_url: str

settings = Settings(env_file='.env')

print(settings.[1])
Drag options to blanks, or click blank then click option'
Adatabase_url
BDATABASE_URL
CDatabaseUrl
Ddb_url
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access settings.DATABASE_URL instead of settings.database_url.
Using camelCase or other naming styles.
3fill in blank
hard

Fix the error in the code to correctly load environment variables with a custom .env file.

FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    secret_key: str

settings = Settings([1]='config.env')
Drag options to blanks, or click blank then click option'
Aenvpath
Benv_file
Cenvfile
Dfile_path
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names like envpath or envfile.
Using camelCase instead of snake_case.
4fill in blank
hard

Fill both blanks to create a settings class that reads API_KEY and DEBUG from environment variables.

FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    api_key: [1]
    debug: [2]

settings = Settings(env_file='.env')
Drag options to blanks, or click blank then click option'
Astr
Bbool
Cint
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using int or float for API key or debug values.
Mixing up the types for api_key and debug.
5fill in blank
hard

Fill all three blanks to create a settings class that loads HOST, PORT, and USE_SSL with correct types.

FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    host: [1]
    port: [2]
    use_ssl: [3]

settings = Settings(env_file='.env')
Drag options to blanks, or click blank then click option'
Astr
Bint
Cbool
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using float for port number.
Using string for use_ssl instead of bool.
Mixing types between host, port, and use_ssl.