tezvyn:

What is the purpose of @app.get("/") in FastAPI?

AI-drafted, machine-checkedSource: fastapi.tiangolo.combeginner
What is the purpose of @app.get("/") in FastAPI?

Tests your understanding of FastAPI routing. A strong answer explains that the decorator binds an HTTP method and path to a Python function, registers it in the app's route table, and builds OpenAPI metadata.

WHAT THIS TESTS: This question tests whether you understand the core routing mechanism in FastAPI and how the framework bridges HTTP semantics to Python functions. At the senior level, interviewers want to see that you recognize the decorator is not just stylistic sugar but an active registration hook that populates the application's internal route table at import time and drives automatic API documentation generation.

A GOOD ANSWER COVERS: A good answer hits four things in order. First, the decorator like app.get is a method on the FastAPI instance that takes a path string and returns a decorator. Second, when applied to a function, it registers that function as a handler for a specific HTTP method and URL path combination in the app's route registry. Third, when an incoming request arrives, FastAPI's ASGI layer matches the request method and path against this registry and dispatches to the registered function. Fourth, the registration also populates the OpenAPI schema, which powers the interactive docs.

COMMON WRONG ANSWERS: Red flags include describing the decorator as mere syntax sugar without explaining route registration, confusing it with general Python decorators like property or staticmethod, claiming the URL mapping happens at runtime per request rather than at import time, or failing to mention that the decorator method name corresponds to the HTTP verb. Another weak pattern is conflating FastAPI's approach with Flask's legacy add_url_rule without noting FastAPI's automatic type inference and OpenAPI integration.

LIKELY FOLLOW-UPS: Interviewers often follow up by asking how path parameters are extracted from the URL, what happens if two decorators register the same path and method, how you would handle multiple HTTP verbs for one endpoint, or what the difference is between app.get and app.api_route. They may also probe whether you understand that the decorator returns the original function unchanged so it can still be called directly in tests.

ONE CONCRETE EXAMPLE: Imagine you write app.get("/items/{item_id}") above a function read_item(item_id: int). At import time, FastAPI registers the path /items/{item_id} with the GET method pointing to read_item. It also parses the path template, sees that item_id is typed as int, and stores that validation rule. When a client GETs /items/42, FastAPI matches the route, validates that 42 is an integer, injects it into the function, and returns the result. The same registration automatically adds the endpoint to the openapi.json served at /docs.

Source: fastapi.tiangolo.com

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.