Lesson 1 · Foundations

🌐 APIs, HTTP & Why FastAPI

A web API lets one program ask another for data or actions over HTTP, without either side needing to know how the other is built. FastAPI is a Python framework for building these APIs quickly and safely.

🌐 What a REST API does

A client (a phone app, a website, another server) sends an HTTP request to a URL; the server sends back a response. A REST API organises this around resources (things, like users or orders) addressed by URLs, and uses standard HTTP methods to act on them.

GET /users list users GET /users/42 get user 42 POST /users create a user PUT /users/42 replace user 42 PATCH /users/42 update part of user 42 DELETE /users/42 delete user 42

GET, PUT, DELETE are idempotent: doing them twice has the same effect as once. POST usually is not — posting the same form twice can create two records.

🖼️ See it

A request on its way through a FastAPI app📱Client➡️HTTP request🚦Route match🐍Yourfunction📦JSONresponse

🖼️ See it

HTTP methods and what they are forMethodTypical useIdempotent?GETRead dataYesPOSTCreate something newNoPUTReplace a wholeresourceYesPATCHUpdate part of aresourceNoDELETERemove a resourceYes

🚦 Status codes tell the client what happened

  • 2xx success: 200 OK, 201 Created, 204 No Content.
  • 3xx redirect: 301 Moved Permanently.
  • 4xx client error: 400 Bad Request, 401 Unauthorized (not logged in), 403 Forbidden (logged in, not allowed), 404 Not Found, 422 Unprocessable Entity (FastAPI uses this for bad input).
  • 5xx server error: 500 Internal Server Error — a bug, not the client's fault.
401 vs 403. 401 means “tell me who you are”; 403 means “I know who you are, and the answer is still no”.

⚡ Why FastAPI specifically

FastAPI is built on Starlette (the web parts) and Pydantic (data validation), running on an ASGI server such as Uvicorn. Three things set it apart from older Python frameworks:

  • Type hints become validation. Writing age: int is enough for FastAPI to reject a request where age is text.
  • Automatic interactive docs. Every app gets a /docs page (Swagger UI) for free, generated from your code.
  • Native async support, so one process can handle many slow requests (waiting on a database or another API) at once.
# the smallest FastAPI app from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"message": "Hello, world"} # run it: fastapi dev main.py (FastAPI CLI, Uvicorn underneath) # or: uvicorn main:app --reload
Common mistake. Forgetting --reload while developing (you keep testing stale code), or leaving it on in production (it is slower and meant for development only).

🆕 What a 2026 FastAPI project looks like

Newer FastAPI versions ship a command-line tool. Install fastapi[standard] and you get fastapi dev main.py (auto-reload, for your laptop) and fastapi run main.py (for production). Both use Uvicorn underneath, so older tutorials that type uvicorn main:app --reload still work.

pip install "fastapi[standard]" # or: uv add "fastapi[standard]" fastapi dev main.py # http://127.0.0.1:8000/docs
Pydantic v2 only. Current FastAPI uses Pydantic v2: write model_dump() (not .dict()), model_config = ConfigDict(from_attributes=True) (not class Config: orm_mode = True) and Field(pattern=...) (not regex=). If you copy code from an old blog post, these are the lines that break.

🪜 Step by step: choosing method and status code

Pick the HTTP method from the action and the status code from the result.

Problem: For a "books" API, choose method, path and success code for: list books, create a book, delete book 7, and a request for a book that does not exist.

  1. List.
    GET /books → 200
  2. Create.
    POST /books → 201 Created
  3. Delete.
    DELETE /books/7 → 204 No Content
  4. Missing book.
    GET /books/99 → 404 Not Found

Answer: GET 200, POST 201, DELETE 204, missing → 404.

Common mistake: returning 200 with {"error": "not found"}. Use the status code to signal the result.
✏️ Quick check · 1 of 8
Methods

Which method is the right one for creating a brand-new order?

GET only reads; it should not change anything.
✏️ Quick check · 2 of 8
Status

A client sends a request with no login token to a page that requires one. The correct status is:

Not logged in, vs logged in but not allowed.
✏️ Quick check · 3 of 8
Status

A logged-in user tries to delete someone else's post. The correct status is:

They are known; the action is refused.
✏️ Quick check · 4 of 8
Status

A request body is missing a required field. FastAPI responds with:

It is the client's data that is wrong, not a server bug.
✏️ Quick check · 5 of 8
Idempotence

Which of these is NOT idempotent (repeating it can create extra data)?

Think about what happens if the same request is sent twice by accident.
✏️ Quick check · 6 of 8
Stack

FastAPI uses which library to validate request data against Python type hints?

Not a web framework — a data-validation library.
✍️ Type your answer · 7 of 8

An API has 6 resource types, each with GET-list, GET-one, POST, PUT and DELETE.

How many routes is that in total?

Enter a whole number.
Resources × operations per resource.
✍️ Type your answer · 8 of 8

A server has handled 180 requests, of which 15 returned a 4xx or 5xx status.

How many returned a success status (2xx or 3xx)?

Enter a whole number.
Subtract the error responses from the total.
📝 Lesson quiz · 5 questions
0 / 5
Re-read the lesson, then give it another go.
Question 1 of 5

A REST API organises things around:

Question 2 of 5

FastAPI generates interactive API documentation at:

Question 3 of 5

The ASGI server commonly used to run a FastAPI app is:

Question 4 of 5

500 Internal Server Error usually means:

Question 5 of 5

Type hints in FastAPI are used to: