Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

FastAPI
  • FastAPI Hello World: Routes, uvicorn and the Free Swagger Page
  • RESTful API Basics: Resources, Methods, Stateless Requests and Status Codes
  • Django, FastAPI or Flask? The Same Blog Feature in Three Shapes, and How to Choose
  • One Blog, Three Frameworks: Routing, Models, Templates and Layout in Django, FastAPI and Flask
  • One Request, Two Answers: the Same Post as an HTML Page and as JSON
  • What FastAPI Is: Starlette, Pydantic, uvicorn and What async def Really Gives You
  • Building a Blog with an AI Coding Agent: Plan Mode, a Master Prompt and the First Review
  • Handing a Figma Design to an AI Agent: Compare the Files, Then Review the Change
  • Inside a FastAPI Blog: Every Folder and File an AI Agent Built, and What to Question
  • Swagger UI and OpenAPI in FastAPI: Reading the Contract Your Code Writes
  • POST, PUT and DELETE in FastAPI: Choosing 201, 200, 204 and 404 on the Server
  • Paging, Sorting and Filtering a REST API in FastAPI
  • API Versioning in FastAPI with an API-Version Header
  • Idempotent REST Methods, HATEOAS and the Maturity Model: Which Requests Are Safe to Repeat
  • A FastAPI REST API End to End: One App Visit, Seven Requests
  • The Book API in FastAPI: A Contract a React Client Can Rely On
  • A React Client for a FastAPI API: The CORS Wall, CORSMiddleware and the Vite Proxy
  • FastAPI Request and Response Models with Pydantic: Design the JSON on Purpose
  • FastAPI Path, Query and Body Parameters: Reading a 422, response_model and Depends
  • Validation and Problem Details in FastAPI: From a 422 to One Error Shape for the Whole API
  • FastAPI with SQLAlchemy Models and Alembic Migrations: A Table from a Class, a Session per Request
  • From a List to a Database in FastAPI: Route, Service, Repository and the SQL SQLAlchemy Sends
  • Docker Compose: Run a FastAPI App and Postgres From One File
  • Docker Health Checks, Networks and Resource Limits: Up Is Not Enough
  • How a Request and a Login Flow Through a FastAPI Blog: Depends, bcrypt, a Signed Session Cookie and async def
  • Enterprise Integration for an AI App: One Adapter per Edge for SSO, the CRM, a Legacy CSV and Webhooks
  1. Blog
  2. /
  3. FastAPI
  4. /
  5. Fundamentals

A FastAPI REST API End to End: One App Visit, Seven Requests

One phone app talks to a FastAPI Book API from first screen to last: a page of books, a search by author, version 2 of one book, a good and a bad POST, a cover upload and a DELETE sent twice. Real requests and answers.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Fundamentals

What you will learn

  • Follow one client visit through paging, filtering, versioning, create, upload and delete requests
  • Read each status code a REST API sends back: 200, 201, 204, 400, 404
  • See how query strings, headers and request bodies each shape an answer
  • Use Swagger UI as the map of every operation in the API

Before you start

  • RESTful API Basics
  • FastAPI Hello World: Routes, uvicorn and the Free Swagger Page

Open a library app on your phone. From the first screen to the last, what does that app actually say to the server?

This article follows one visit to the Book API built in the REST section. There's no new code here. Every request is one the section already built, and here they join up into a single story. All requests, status codes and bodies come from the finished API's saved run.

One visit, seven requests

The phone app sends seven requests to the Library API at localhost:8080: browse a page (200), search by author (200), open one book in version 2 (200), add a book (201), add a bad book (400), add a cover (201 then 200 to download), remove a book (204, then 404 the second time). Phone app Library API localhost:8080 1 GET /api/books?page=1&size=5&sort=pages,desc 2 GET /api/books?author=austen 3 GET /api/books/10 + API-Version: 2 4 POST /api/books (Matilda) 5 POST /api/books (bad book) 6 POST, then GET /api/books/1/cover 7 DELETE /api/books/3, twice 200 200 200 201 400 201, 200 204, 404
Every step is one request and one answer.

1. Browse a page

The phone wants the books, five at a time. It sends:

GET /api/books?page=1&size=5&sort=pages,desc
HTTP/1.1 200
Content-Type: application/json
{"content":[
  {"id":9,"title":"The Silmarillion", ...
   ... 4 more books ...
 ],"page":{"size":5,"number":1,
  "totalElements":12,"totalPages":3}}

Page numbers start at zero, so page=1 is the second page. The phone draws five titles, The Silmarillion (365 pages), The Hobbit (310), Mort (272), Dune Messiah (256) and Persuasion (249), and the footer says "Page 2 of 3", because the page object says there are 3 pages of 12 books.

The route reads those three query parameters with FastAPI's Query, and the limits are part of the declaration:

def list_books(
    author: str | None = None,
    page: int = Query(0, ge=0),
    size: int = Query(5, ge=1, le=100),
    sort: list[str] = Query(["title"]),
    service: BookService = Depends(get_service),
):

2. Search by author

The user types austen into the search box. The app calls the same address and adds one word to the query string:

GET /api/books?author=austen
HTTP/1.1 200
{"content":[
  {"id":3,"title":"Emma", ...
   ... 2 more books ...
 ],"page":{"size":5,"number":0,
  "totalElements":3,"totalPages":1}}

The list shrinks to Emma, Persuasion and Pride and Prejudice (more in paging, sorting and filtering). In the repository, the filter is one extra where on the query, and the database does the paging:

        query = select(Book)
        if author:
            query = query.where(Book.author.ilike(f"%{author}%"))
        total = self.db.scalar(select(func.count()).select_from(query.subquery()))
        columns = [getattr(Book, name).desc() if desc else getattr(Book, name) for name, desc in orders]
        rows = self.db.scalars(query.order_by(*columns, Book.id).offset(page * size).limit(size))

3. Open one book, version 2

The user taps Good Omens, book 10. The app sends one extra header:

GET /api/books/10
API-Version: 2
HTTP/1.1 200
{"id":10,"title":"Good Omens",
 "authors":["Terry Pratchett","Neil Gaiman"],"pages":412}

Version 2 turns the author text into a list, so the detail screen can show two names. Without the header, the same address answers with version 1. A small dependency reads the header, and an unknown version is a 400 (see API versioning with a header):

def get_version(
    api_version: str = Header("1", alias="API-Version", json_schema_extra={"enum": ["1", "2"]}),
) -> str:
    """The client names the version in a header; no header means version 1."""
    if api_version not in ("1", "2"):
        raise UnknownVersion(api_version)
    return api_version

4 and 5. Add a book, good and bad

The user fills in a form, title Matilda, author Roald Dahl, 240 pages, and the book travels in the body as JSON:

POST /api/books
{"title":"Matilda","author":"Roald Dahl","pages":240}
HTTP/1.1 201
Location: /api/books/13
Content-Type: application/json
{"id":13,"title":"Matilda","author":"Roald Dahl","pages":240}

201 means created, and the Location header says where the new book lives. The phone shows "Saved: book 13".

Then a bad try: an empty title and zero pages.

POST /api/books
{"title":"","author":"Roald Dahl","pages":0}
HTTP/1.1 400
Content-Type: application/problem+json
{"detail":"The book has invalid fields", ...
 "errors":{"title":"title is required",
           "pages":"pages must be more than 0"}}

The rules live on the request model, and an error handler turns FastAPI's own validation error into a 400 Problem Details body with one message per field. The phone puts each message under its own field. Validation and Problem Details builds that handler.

Text = Annotated[str, StringConstraints(strip_whitespace=True, min_length=1)]


class BookRequest(BaseModel):
    """What a client may send: no id, no shelf code, and rules the data must follow."""

    title: Text = Field(examples=["Matilda"])
    author: Text = Field(examples=["Roald Dahl"])
    pages: int = Field(gt=0, examples=[240])

6. Add a cover

The Hobbit has no cover yet. The app uploads a picture as multipart form data, a 230-byte file called hobbit.png:

POST /api/books/1/cover
Content-Type: multipart/form-data
file: hobbit.png, 230 bytes
HTTP/1.1 201
Location: /api/books/1/cover

To show it, the app sends a GET to the same address. This time the body is the picture itself:

HTTP/1.1 200
Content-Type: image/png
Content-Length: 113

Files go up as multipart and come down as bytes, with Content-Type saying what they are. The upload rules are in file upload and download in FastAPI.

7. Remove a book

The phone shows Emma with a Remove button.

DELETE /api/books/3

HTTP/1.1 204

204 means done, nothing to send back, and the card leaves the screen. If the button is pressed a second time:

HTTP/1.1 404
Content-Type: application/problem+json
{"detail":"No book with id 3","instance":"/api/books/3","status":404,"title":"Book not found"}

The 404 uses the same Problem Details shape as the 400, so the app handles every error the same way. Deleting twice is safe, by the way: the book is gone either way. Idempotent REST methods explains why.

Everything in one contract

Where is all this written down? In the OpenAPI contract, which Swagger UI shows at /docs (see the OpenAPI contract and Swagger UI). It lists seven operations under the Books tag:

OperationSteps of the visit
GET /api/books: List books, a page at a time1 browse, 2 search
GET /api/books/{book_id}: One book (API-Version 1 or 2)3 open one book
POST /api/books: Add a book4 and 5, good and bad
PUT /api/books/{book_id}: Replace a booknot needed in this visit
DELETE /api/books/{book_id}: Remove a book7 remove
POST /api/books/{book_id}/cover: Upload the cover6 upload
GET /api/books/{book_id}/cover: Download the cover6 download

Those summaries come straight from the summary= argument of each route decorator. The app itself is small: main.py creates it, plugs in the books router and registers the error handlers.

app = FastAPI(
    title="Library API",
    version="1",
    openapi_tags=[{"name": "Books", "description": "Read, add, change and remove books"}],
    lifespan=lifespan,
)
app.include_router(books.router)
register_handlers(app)  # every error leaves the API as application/problem+json

Common mistakes

  • Counting pages from 1. This API counts from 0, so page=1 is the second page. The client must read number and totalPages from the answer rather than guess.
  • Making a new URL for every variant. Searching is still /api/books with a query string, and version 2 is still /api/books/10 with a header. The resource keeps its address.
  • Different error shapes for different errors. Here the 400 and the 404 share one Problem Details shape, so the client needs one error handler, not five.
  • Sending a picture inside JSON. Files travel as multipart form data on the way up and as plain bytes with a Content-Type on the way down.

The whole section in five lines

IdeaIn the Book API
URLs name resources/api/books/10
Methods say what to doGET, POST, PUT, DELETE
Status codes say what happened200, 201, 204, 400, 404
JSON, checked by Pydantic models, carries the data{"title":"Matilda"}
Swagger UI documents it all/docs

For the models behind these answers, see FastAPI request and response models with Pydantic. Try it yourself: run the project and send the requests in its requests.http file in the order of this visit, then open /docs and match each one to its operation.

Keep going in FastAPI · 15 of 26

Previous
Idempotent REST Methods, HATEOAS and the Maturity Model: Which Requests Are Safe to Repeat
Next post
The Book API in FastAPI: A Contract a React Client Can Rely On
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

The Python Architect Masterclass: Full Stack to AI Engineer

Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)

Related posts

FastAPI Hello World: Routes, uvicorn and the Free Swagger Page

The smallest FastAPI app: two routes in main.py, started with uvicorn. Follow one request from the browser to the route table and back as JSON, see a 404, and try the routes on the free Swagger page at /docs.

RESTful API Basics: Resources, Methods, Stateless Requests and Status Codes

An API is the waiter between your program and a server. REST gives each resource an address and uses the HTTP method for the action. Send GET, POST, PUT, PATCH and DELETE to a tiny local server with urllib, see why requests are stateless, and read 200, 201, 204, 400 and 404.

Django, FastAPI or Flask? The Same Blog Feature in Three Shapes, and How to Choose

One small project lists two blog posts three times: as a Django view, a Flask route and a FastAPI endpoint. See each shape with its real output, what each framework includes, and a three-question way to choose.

One Blog, Three Frameworks: Routing, Models, Templates and Layout in Django, FastAPI and Flask

The same blog built in Django, FastAPI and Flask, compared line by line: the list route, the Post model, the template loop, the project layout and the extras, with a ROSETTA map you can use to port a feature with AI.

On this page

  • One visit, seven requests
  • 1. Browse a page
  • 2. Search by author
  • 3. Open one book, version 2
  • 4 and 5. Add a book, good and bad
  • 6. Add a cover
  • 7. Remove a book
  • Everything in one contract
  • Common mistakes
  • The whole section in five lines
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

The Python Architect Masterclass: Full Stack to AI Engineer

Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.