๐ FASTAPI STUDENT LAB MANUAL + EXERCISE BOOK
For Undergraduate Students — 12 Modules
This manual contains:
-
Module-wise exercises
-
Lab tasks
-
Assessment questions
-
Mini-projects
-
Expected outputs
-
Teacher demonstration notes
Students can follow along using VS Code + FastAPI.
๐ Before Starting — Lab Environment Setup
Students must complete:
-
Install Python 3.10+
-
Install VS Code
-
Install Python extension in VS Code
-
In Terminal:
pip install fastapi uvicorn
Create project folder:
fastapi_course/
๐งช MODULE-WISE LAB EXERCISES & ACTIVITIES
Below are 12 modules with 3 types of tasks:
-
Practice Exercise (Must do)
-
Lab Task (Hands-on)
-
Assessment Question (Graded)
⭐ MODULE 1 — FASTAPI BASICS
Practice Exercise
-
Create a FastAPI app with one route:
/hello→ returns"Hello Students" -
Add a second route:
/college→ returns your college name.
Lab Task
-
Add a route:
/square/{num}that returns the square of the number.
Example output:
{"number": 5, "square": 25}
Assessment Question
-
Create a route that returns the current date & time.
⭐ MODULE 2 — PATH PARAMETERS
Practice Exercise
-
Create
/student/{id}→ return"Student ID is {id}".
Lab Task
-
Create
/book/{book_id}→ returns a dictionary:
{ "book_id": 10, "title": "Some Title" }
Assessment Question
-
Create
/calc/{a}/{b}→ return sum, difference, product.
⭐ MODULE 3 — QUERY PARAMETERS
Practice Exercise
-
Create
/search?item=pen&qty=10→ return both values.
Lab Task
-
Create
/area?length=10&width=20→ return area calculation.
Assessment Question
-
Create
/filter?category=electronics&limit=5→ return meaningful JSON.
⭐ MODULE 4 — REQUEST BODY (POST)
Practice Exercise
-
Accept a JSON body:
{ "name": "Student", "age": 20 }
Lab Task
-
Create
/register-studentaccepting name, reg_no, department.
Assessment Question
-
Design a POST API
/feedbackaccepting:-
student name
-
course name
-
rating
-
⭐ MODULE 5 — CRUD (IN-MEMORY DB)
Practice Exercise
-
Add item with POST
-
List items with GET
Lab Task
-
Implement:
-
Create item
-
Get by ID
-
Update by ID
-
Delete by ID
-
Assessment Question
-
Build a mini-API for storing student records (name, id, dept).
⭐ MODULE 6 — PYDANTIC MODELS
Practice Exercise
-
Create a Pydantic model for Student(name, age, department).
Lab Task
-
Create a route validating:
-
Product(name, price, stock)
-
Assessment Question
-
Add validation:
Price must be > 0
Stock must be >= 0
⭐ MODULE 7 — ERROR HANDLING
Practice Exercise
-
Raise HTTP 404 manually if ID not found.
Lab Task
-
Create:
/student/{id}
If ID not in list → return custom JSON error.
Assessment Question
-
Create a custom error for invalid age input.
⭐ MODULE 8 — MIDDLEWARE
Practice Exercise
-
Print "Request Received" for every API call using middleware.
Lab Task
-
Log:
-
method
-
url
-
response time
-
Assessment Question
-
Add middleware that blocks any request containing a custom header
"blocked: true".
⭐ MODULE 9 — AUTHENTICATION (Simple Token-Based)
Practice Exercise
-
Add
/loginroute that returns a token"abc123".
Lab Task
-
Create
/secure-datawhich requires token in header.
Assessment Question
-
Generate unique token per user using UUID.
⭐ MODULE 10 — BACKGROUND TASKS
Practice Exercise
-
Create a background task that prints
"Task Completed".
Lab Task
-
Simulate sending email in background:
/send-email?email=abc@gmail.com
Assessment Question
-
Store background log messages in a file.
⭐ MODULE 11 — CORS & STATIC FILES
Practice Exercise
-
Enable CORS for all origins.
Lab Task
-
Serve a
hello.txtfile from/static/hello.txt.
Assessment Question
-
Restrict CORS to only:
⭐ MODULE 12 — SQLITE DATABASE CRUD
Practice Exercise
-
Create SQLite DB file:
students.db -
Table: id, name, department
Lab Task
-
Implement:
-
Add student
-
Get all
-
Update
-
Delete
-
Assessment Question
-
Add
markscolumn -
Return average marks of all students using a query.
๐ผ MINI PROJECTS (For Final Lab Assessment)
Choose any one:
-
Student Information System
Add, update, delete students (SQLite) -
Library Book Manager
Add books, issue books, return books. -
Online Store API
Products + Cart + Order simulation. -
College Attendance API
Students → Subjects → Attendance marking. -
Feedback Collection API
Save feedback to SQLite + list top feedback.
๐ฏ INTERNAL ASSESSMENT TEST QUESTIONS
-
What is FastAPI and why is it fast?
-
Explain Path vs Query parameters with examples.
-
What are Pydantic models?
-
What is middleware? Give an example.
-
Explain background tasks with a use case.
-
What is CORS and why is it needed?
-
How do you connect FastAPI with SQLite?
๐งพ TEACHER DEMO IDEAS (Optional)
Teachers may demonstrate:
-
Running a module with
uvicorn -
Using
/docsfor live testing -
Debugging validation errors
-
Observing middleware logs
-
Creating & reading SQLite DB files
-
Real-time CRUD operations
No comments:
Post a Comment