List All Expenses

Add a GET endpoint that returns all stored expenses

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

The problem

Right now, your API can create expenses but has no way to retrieve them. You can add data, but you cannot see what you added. A GET endpoint solves this by returning everything stored in the dictionary.

How it works

The expenses dictionary stores each expense as a value. Calling expenses.values() returns all the values, but FastAPI needs a list to serialize the response as JSON. Wrapping it in list() converts it to a format FastAPI can return.

list(expenses.values())  # [{"id": 1, "description": "Lunch", ...}, ...]

Instructions

  1. Add the @app.get("/expenses") decorator above the function.
  2. Define a function named list_expenses that takes no parameters.
  3. Return list(expenses.values()).