Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

PythonintermediatebackendAI generated

FastAPI Bookstore REST API

Build a REST API for a bookstore with FastAPI: it manages authors, books and stock, with endpoints to add, view, update and delete them, search and filter books, and page through results. Data lives in a database through SQLAlchemy, request and response data is checked by Pydantic models (for example prices can't be negative), and FastAPI automatically produces interactive documentation you can try in the browser. Everything is covered by automated tests. You'll learn designing REST endpoints, SQLAlchemy models and relationships, Pydantic validation, dependency injection in FastAPI, pagination and filtering, proper HTTP status codes, and testing with pytest.

Estimate
~10.5h
Steps
6
Completed by
0
Proposed by
codeseed.app

FastAPI · SQLAlchemy · Pydantic · Uvicorn

Project roadmap

  1. 01

    Define the models

    ~2h

    Create a project with FastAPI, SQLAlchemy 2 and Uvicorn. Define SQLAlchemy models: Author (name and bio), Book (title, ISBN unique, price in cents, genre, published year, author) and Inventory (book, quantity, one per book), with relationships. Create separate Pydantic schemas for creating, updating and reading each one. Set up the database session as a FastAPI dependency (SQLite for development). Done when: the app starts and creates the tables.

  2. 02

    Build the CRUD endpoints

    ~2.5h

    Add routers for /authors and /books with list, get, create, update (PATCH) and delete. Book responses include the author's name and the stock. Return 201 on create, 404 for unknown ids, 409 when the ISBN already exists, and refuse to delete an author who still has books. Done when: every endpoint works from the /docs page.

  3. 03

    Add filtering and pagination

    ~1.5h

    Support GET /books?author_id=&genre=&min_price=&max_price=&q=&sort=&limit=&offset=, where q searches the title, and limit is at most 100. Build the query with SQLAlchemy conditions only for the filters given, and return the items with the total count. Load authors with the books in one query (selectinload) to avoid one query per book. Done when: combined filters and paging return the right books.

  4. 04

    Validate business rules

    ~1.5h

    Use Pydantic field constraints and validators: prices and stock not negative, a valid ISBN-13 check digit, a published year not in the future, and non-empty titles. Add PATCH /books/{id}/stock to adjust stock by an amount, refusing to go below zero. Return errors in FastAPI's standard 422 format, and your own clear messages for business rule errors. Done when: invalid data is rejected with clear messages.

  5. 05

    Write automated tests

    ~2h

    Write pytest tests with FastAPI's TestClient and a separate test database (overriding the session dependency): CRUD for authors and books, duplicate ISBNs, deleting an author with books, filters and pagination, validation errors and stock changes. Done when: pytest passes.

  6. 06

    Prepare it to run

    ~1h

    Read settings such as the database URL from environment variables with pydantic-settings, add a /health endpoint, and write a README.md with setup and run instructions (and optionally a Dockerfile). Done when: someone can run the API by following README.md.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~10.5h · 6 steps

Tech stack

FastAPISQLAlchemyPydanticUvicorn