0
0
FastAPIframework~30 mins

CRUD operations in FastAPI - Mini Project: Build & Apply

Choose your learning style9 modes available
CRUD Operations with FastAPI
📖 Scenario: You are building a simple web API to manage a list of books in a library. Each book has an id, title, and author. You will create endpoints to add, read, update, and delete books.
🎯 Goal: Create a FastAPI app with endpoints to perform CRUD (Create, Read, Update, Delete) operations on a list of books stored in memory.
📋 What You'll Learn
Use FastAPI to create the web API
Store books in a list of dictionaries with keys: id, title, author
Create endpoints for: adding a book, getting all books, updating a book by id, and deleting a book by id
Use proper HTTP methods: POST for create, GET for read, PUT for update, DELETE for delete
💡 Why This Matters
🌍 Real World
APIs like this are used to manage data in web applications, mobile apps, and services.
💼 Career
Understanding CRUD operations with FastAPI is essential for backend development roles and building RESTful APIs.
Progress0 / 4 steps
1
Set up initial book data
Create a list called books with these exact entries: {'id': 1, 'title': '1984', 'author': 'George Orwell'} and {'id': 2, 'title': 'Brave New World', 'author': 'Aldous Huxley'}.
FastAPI
Need a hint?

Use a list with two dictionaries exactly as shown.

2
Create FastAPI app and import
Import FastAPI from fastapi and create an app instance called app.
FastAPI
Need a hint?

Use from fastapi import FastAPI and then app = FastAPI().

3
Add GET endpoint to read all books
Create a GET endpoint at /books using @app.get('/books') that returns the books list.
FastAPI
Need a hint?

Use @app.get('/books') decorator and a function that returns books.

4
Add POST, PUT, DELETE endpoints for CRUD
Add these endpoints to app: - POST /books to add a new book from JSON body - PUT /books/{book_id} to update a book's title and author by book_id - DELETE /books/{book_id} to remove a book by book_id Use @app.post, @app.put, and @app.delete decorators respectively.
FastAPI
Need a hint?

Use async functions with appropriate decorators and handle book list updates. Raise 404 error if book not found.