📖 Scenario: You are building a simple web API to manage a list of books. You want to understand how to create this API using three popular Python web frameworks: FastAPI, Flask, and Django. This project will guide you step-by-step to create a basic API endpoint in FastAPI, then compare its structure and features with Flask and Django.
🎯 Goal: Create a FastAPI application with a simple endpoint to list books. Learn how to set up initial data, configure a route, implement the main logic to return data, and finalize the app. Understand the differences in setup and usage compared to Flask and Django.
📋 What You'll Learn
Create a list of books as initial data
Add a configuration variable for API version
Implement a GET endpoint to return the list of books
Complete the FastAPI app with proper route and run configuration
💡 Why This Matters
🌍 Real World
Web APIs are common in many applications like online stores, social media, and data services. FastAPI, Flask, and Django are popular tools to build these APIs efficiently.
💼 Career
Understanding these frameworks helps you work on backend development roles, build scalable APIs, and collaborate with frontend teams.
Progress0 / 4 steps
1
DATA SETUP: Create the initial list of books
Create a list called books with these exact dictionaries: {'id': 1, 'title': '1984', 'author': 'George Orwell'} and {'id': 2, 'title': 'Brave New World', 'author': 'Aldous Huxley'}.
FastAPI
Hint
Use a list with two dictionaries exactly as shown.
2
CONFIGURATION: Add API version variable
Add a string variable called api_version and set it to 'v1'.
FastAPI
Hint
Just create a variable named api_version and assign the string 'v1'.
3
CORE LOGIC: Create FastAPI app and GET endpoint
Import FastAPI from fastapi. Create an app instance called app. Use @app.get decorator with path f'/api/{api_version}/books' to define a function get_books that returns the books list.
FastAPI
Hint
Remember to import FastAPI, create app, and use the decorator with the formatted path.
4
COMPLETION: Add code to run the FastAPI app
Add the standard if __name__ == '__main__': block. Inside it, import uvicorn and run the app with uvicorn.run(app, host='127.0.0.1', port=8000).
FastAPI
Hint
This block lets you run the FastAPI app directly with Python.
Practice
(1/5)
1. Which framework is best known for being modern, fast, and supporting automatic type checking for APIs?
easy
A. Flask
B. Pyramid
C. Django
D. FastAPI
Solution
Step 1: Understand framework features
FastAPI is designed to be modern and fast, with built-in support for Python type hints to validate and document APIs automatically.
Step 2: Compare with other frameworks
Flask is minimal and easy to start but lacks built-in type checking. Django is full-featured but more heavyweight and not focused on type checking.
Final Answer:
FastAPI -> Option D
Quick Check:
Modern + fast + type checking = FastAPI [OK]
Hint: Modern + fast + type hints = FastAPI [OK]
Common Mistakes:
Confusing Flask's simplicity with FastAPI's speed
Thinking Django has built-in type checking
Choosing Pyramid which is less common
2. Which of the following is the correct way to create a simple route in Flask?
easy
A. app.route('/')(def home(): return 'Hello')
B. @app.route('/')
def home():
return 'Hello'
C. app.get('/')
def home():
return 'Hello'
D. @app.get('/')
def home():
return 'Hello'
Solution
Step 1: Recall Flask route syntax
In Flask, routes are created using the @app.route decorator above a function.
Step 2: Identify correct decorator and syntax
@app.route('/')
def home():
return 'Hello' uses @app.route('/') with proper Python function definition and indentation, which is correct Flask syntax.
Final Answer:
@app.route('/')
def home():
return 'Hello' -> Option B
Quick Check:
Flask route decorator = @app.route [OK]
Hint: Flask uses @app.route decorator, not app.route() call [OK]
Common Mistakes:
Missing @ symbol before decorator
Using app.get instead of app.route in Flask
Incorrect indentation or function syntax
3. Given this FastAPI code snippet, what will be the output when accessing /items/5?