Lesson 1 · FastAPI basics

🚀 First Steps: Your First API

FastAPI is a modern Python framework for building APIs. You write ordinary Python functions with type hints, and FastAPI turns them into a fast web API with automatic validation and interactive documentation.

📦 Install and run

Install FastAPI with its standard extras (this includes the fastapi command and the Uvicorn server), then save the code below as main.py.

pip install "fastapi[standard]" # main.py from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Hello World"} # run the development server with auto-reload fastapi dev main.py

Open http://127.0.0.1:8000 to see the JSON, /docs for the interactive Swagger UI, and /redoc for the alternative documentation. Both are generated from your code and the OpenAPI schema at /openapi.json.

🖼️ See it

A request travelling through FastAPIClientGET /items/5Uvicornserverreceives HTTPFastAPImatches thepath operationYourfunctionreturns a dictJSONresponse200 OK
FastAPI turns your Python return value into a JSON response automatically.

🧩 The parts of a path operation

PartIn the exampleMeaning
appapp = FastAPI()the main application object
path"/"the URL path (also called route or endpoint)
operationgetthe HTTP method: get, post, put, delete, patch
decorator@app.get("/")tells FastAPI that this function handles GET requests to /
path operation functionasync def root()the function that runs; it returns a dict, list, string, number or model
Use async def when your code uses await, and plain def otherwise. FastAPI handles both correctly.

🌐 HTTP methods

MethodUsual meaning
GETread data
POSTcreate data
PUTreplace data
PATCHpartly update data
DELETEdelete data

A client sends a request (method, path, headers, maybe a body); the server sends back a response (status code, headers, body). APIs usually exchange JSON.

🪜 Step by step: a path parameter

Add a parameter to the path and give it a type hint.

Problem: Create an endpoint so that GET /items/5 returns {"item_id": 5}.

  1. Path. Use @app.get("/items/{item_id}"): the braces mark a path parameter.
  2. Function. async def read_item(item_id: int): the name must match the braces.
  3. Return. return {"item_id": item_id}; FastAPI turns the dict into JSON.
  4. Test. GET /items/5 → {"item_id": 5}; GET /items/abc → 422, because the type hint is int.

Answer: a path operation whose int type hint validates the parameter automatically.

Common mistake: giving the function parameter a different name from the one in the braces.
✏️ Quick check · 1 of 10
Install

Which command installs FastAPI with the standard extras?

It is a pip package.
✏️ Quick check · 2 of 10
Run

Which command starts the development server with auto-reload?

dev mode.
✏️ Quick check · 3 of 10
Docs

Which URL shows the interactive Swagger UI?

Short and obvious.
✏️ Quick check · 4 of 10
Decorator

What does @app.get("/") do?

Method plus path.
✏️ Quick check · 5 of 10
Methods

Which HTTP method is normally used to create data?

Not read, not delete.
✏️ Quick check · 6 of 10
async

When should you use async def?

Think of await.
✍️ Type your answer · 7 of 10

A server runs on the default port of fastapi dev.

URL partshttpscheme127.0.0.1host?port
port = ?

What is the port number used in http://127.0.0.1:8000?

Enter an exact answer, without rounding.
Read the URL.
✍️ Type your answer · 8 of 10

The five common HTTP methods are GET, POST, PUT, PATCH and DELETE.

HTTP methodsGETPOSTPUTPATCHDELETE
count = ?

How many methods is that?

Enter an exact answer, without rounding.
Count the list.
✍️ Type your answer · 9 of 10

An app has these decorators: @app.get("/"), @app.get("/items"), @app.post("/items") and @app.delete("/items").

Path operationsGET /GET /itemsPOST /itemsDELETE /items
count = ?

How many path operations does it define?

Enter an exact answer, without rounding.
Count decorators.
✍️ Type your answer · 10 of 10

The interactive docs are at /docs and /redoc.

Docs/docs/redoc
interfaces = ?

How many automatic documentation interfaces is that?

Enter an exact answer, without rounding.
Count the URLs.
📝 Lesson quiz · 5 questions
0 / 5
Re-read the lesson, then give it another go.
Question 1 of 5

FastAPI is a framework for building:

Question 2 of 5

The documentation is generated from:

Question 3 of 5

The server that runs FastAPI in development is:

Question 4 of 5

Which is a valid return value for a path operation function?

Question 5 of 5

Which decorator handles POST requests to /items?