FastAPI is a modern web framework for building APIs with Python. It helps create fast and easy-to-use web services.
Click to reveal answer
beginner
Which Python package do you import to start a FastAPI app?
You import the FastAPI class from the fastapi package to create your application instance.
Click to reveal answer
beginner
What does the @app.get('/') decorator do in FastAPI?
It tells FastAPI to run the function below it when someone visits the root URL ('/') with a GET request.
Click to reveal answer
beginner
How do you run a FastAPI app locally for testing?
You use the command 'uvicorn main:app --reload' in the terminal, where 'main' is your Python file and 'app' is your FastAPI instance.
Click to reveal answer
beginner
What will this FastAPI function return?
@app.get('/')
async def root():
return {"message": "Hello, FastAPI!"}
It returns a JSON response with a key 'message' and value 'Hello, FastAPI!'. The browser or client will see {"message": "Hello, FastAPI!"}.
Click to reveal answer
What is the first step to create a FastAPI app?
ARun a database
BWrite HTML code
CInstall React
DImport FastAPI and create an app instance
✗ Incorrect
You start by importing FastAPI and creating an app instance to build your API.
Which command runs a FastAPI app with auto-reload?
Auvicorn main:app --reload
Bnpm start
Cpython main.py
Dfastapi run
✗ Incorrect
The 'uvicorn main:app --reload' command runs the app and reloads on code changes.
What does the function decorated with @app.get('/') respond to?
APOST requests to root URL
BGET requests to root URL
CAny request to /api
DPUT requests to root URL
✗ Incorrect
The @app.get('/') decorator handles GET requests to the root URL.
What type of data does FastAPI return by default from a function?
AJSON
BPlain text
CHTML
DXML
✗ Incorrect
FastAPI returns JSON responses by default.
Which Python keyword is used to define an async function in FastAPI?
Ayield
Bawait
Casync
Ddef
✗ Incorrect
The 'async' keyword defines an asynchronous function.
Describe the steps to create and run your first FastAPI application.
Think about how you start a web server and respond to a web request.
You got /5 concepts.
Explain what happens when you visit the root URL of a FastAPI app with a simple @app.get('/') function.
Imagine you open a webpage and the server sends back a message.
You got /4 concepts.
Practice
(1/5)
1. What does the @app.get("/") decorator do in a FastAPI application?
easy
A. It starts the FastAPI server automatically.
B. It imports the FastAPI library into the project.
C. It defines a route that responds to HTTP GET requests at the root URL.
D. It creates a database connection for the app.
Solution
Step 1: Understand the decorator role
The @app.get("/") decorator tells FastAPI to listen for GET requests at the root URL path.
Step 2: Connect decorator to route handling
When a user visits the root URL with a GET request, the function below this decorator runs and returns a response.
Final Answer:
It defines a route that responds to HTTP GET requests at the root URL. -> Option C
Quick Check:
Route decorator = defines GET route [OK]
Hint: Decorators like @app.get define routes for HTTP GET requests [OK]
Common Mistakes:
Thinking @app.get starts the server
Confusing route decorators with imports
Assuming it creates database connections
2. Which of the following is the correct way to create a FastAPI app instance?
easy
A. app = FastAPI.create()
B. app = FastAPI()
C. app = FastAPI
D. app = fastapi()
Solution
Step 1: Recall FastAPI app creation syntax
The FastAPI app is created by calling the FastAPI class with parentheses: FastAPI().
Step 2: Check each option for correctness
app = FastAPI() correctly calls FastAPI(). app = fastapi() uses lowercase which is incorrect. app = FastAPI misses parentheses, so it is a class reference, not an instance. app = FastAPI.create() uses a non-existent method.
Final Answer:
app = FastAPI() -> Option B
Quick Check:
FastAPI instance = FastAPI() [OK]
Hint: Create app with FastAPI() including parentheses [OK]
Common Mistakes:
Omitting parentheses when creating app
Using lowercase 'fastapi' instead of 'FastAPI'
Trying to call a non-existent create() method
3. Given this FastAPI code snippet, what will be the JSON response when accessing /hello?
C. FastAPI does not support returning lists in JSON.
D. The function read_items is missing an indentation for the return statement.
Solution
Step 1: Check Python syntax indentation
Python requires the return statement inside the function to be indented. Here, return is not indented, causing a syntax error.
Step 2: Verify other parts
The route path '/items' is valid, returning lists is supported, and the import is correct.
Final Answer:
The function read_items is missing an indentation for the return statement. -> Option D
Quick Check:
Python indentation error = missing indent [OK]
Hint: Indent return inside function to avoid syntax errors [OK]
Common Mistakes:
Ignoring Python indentation rules
Thinking route path must be singular
Believing FastAPI can't return lists
5. You want to create a FastAPI app that returns a greeting with a user name from the URL path, like /greet/Alice. Which code correctly implements this?
hard
A. from fastapi import FastAPI
app = FastAPI()
@app.get("/greet/{name}")
def greet(name: str):
return {"greeting": f"Hello, {name}!"}
B. from fastapi import FastAPI
app = FastAPI()
@app.get("/greet")
def greet(name: str):
return {"greeting": f"Hello, {name}!"}
C. from fastapi import FastAPI
app = FastAPI()
@app.get("/greet/{name}")
def greet():
return {"greeting": "Hello!"}
D. from fastapi import FastAPI
app = FastAPI()
@app.get("/greet/{name}")
def greet(name):
return {"greeting": "Hello, name!"}
Solution
Step 1: Use path parameter in route
The route must include {name} in the path to capture the user name from the URL.
Step 2: Define function with typed parameter and return greeting
The function should accept name: str and return a dictionary with a greeting using an f-string to insert the name.
Step 3: Check other options for errors
from fastapi import FastAPI
app = FastAPI()
@app.get("/greet")
def greet(name: str):
return {"greeting": f"Hello, {name}!"} misses the path parameter in the route. from fastapi import FastAPI
app = FastAPI()
@app.get("/greet/{name}")
def greet():
return {"greeting": "Hello!"} does not accept the name parameter. from fastapi import FastAPI
app = FastAPI()
@app.get("/greet/{name}")
def greet(name):
return {"greeting": "Hello, name!"} does not use f-string, so it returns the literal text "name".
Final Answer:
Uses path parameter and returns personalized greeting. -> Option A