Skip to main content
MediumBackendBuild

Build a Filterable Product Catalog API

You are building the read side of a product catalog API. Clients need to browse products by category, filter by price range, and look up a single product by its ID. Implement two routes in the FastAPI app in `main.py`:...

What you will practice

PythonREST APIs / RoutesHTTP FundamentalsFastAPIPath ParametersQuery ParametersFiltering

Requirements

  • `GET /products` with no query parameters returns all products as a JSON array.
  • `GET /products?category=electronics` returns only products whose `category` matches the value (case-insensitive).
  • `GET /products?min_price=50` returns only products with `price >= 50.0`.
  • `GET /products?max_price=100` returns only products with `price <= 100.0`.
  • `GET /products?in_stock=true` returns only products where `in_stock` is `True`; `in_stock=false` returns only products where `in_stock` is `False`.
  • Multiple query parameters combine with AND logic — all active filters must match.
  • `GET /products/{product_id}` returns the matching product as a JSON object, or HTTP 404 if no product has that ID.

Starter files

main.pyEditable starter

What the judge checks

  • Runs in the python environment with the python-pytest runner.
  • Uses a 5000ms judge budget.
  • Behavior rules include: Get All Products, Filter By Category, Filter By Min Price, Filter By Max Price.

Constraints

  • Do not modify the `PRODUCTS` list in the starter file.
  • All query parameters are optional. Omitted parameters do not filter.
  • Category comparison is case-insensitive: `?category=Electronics` and `?category=electronics` must return the same results.

Example behavior

Input
GET /products?category=electronics&min_price=60
Output
[{"id": "p002", ...}, {"id": "p005", ...}]

Both filters apply: category must be 'electronics' AND price must be >= 60. Keyboard (149.99) also matches, but this example shows the pattern — actual results depend on the starter dataset.

Input
GET /products/p001
Output
{"id": "p001", "name": "Mechanical Keyboard", "category": "electronics", "price": 149.99, "in_stock": true}

Returns the single product with id 'p001'.

Follow-up

Right now filters are applied in Python after fetching all products. How would you restructure this if the product data were in a SQL database instead of an in-memory list?