Repaso
Español
Backend & APIsCoreAPI-42

In FastAPI, what does @app.get("/items/{item_id}") above a function do?

Options

  1. It downloads an item from the server to the client
  2. It registers a GET route; item_id is taken from the URL (Correct option)
  3. It creates a database table named items for the function
  4. It defines a test case for the function underneath it

Answer

Registers a route: GET requests to /items/<id> are handled by that function, with item_id taken from the URL

Why

Routing connects a URL + HTTP method to a Python function (an endpoint).

When a request GET /items/7 arrives, FastAPI calls your function with item_id=7, converting it to the type you declared (e.g. int).

Whatever the function returns (dict, Pydantic model) is turned into the JSON response.

@app.get("/items/{item_id}")
def read_item(item_id: int):
    return {"id": item_id}

Remember

Route = method + path → function.

Key terms

Practice this question in Repaso