Skip to content
tezvyn:

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

Source: fastapi.tiangolo.comEasyHow cards are made

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's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

A 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.

Interview question

When an incoming GET request reaches a FastAPI app, how does @app.get("/") enable the correct function to run?

  • a.It delays route binding until the first request arrives to improve startup speed.
  • b.It acts as pure syntax sugar to make the code look cleaner without changing framework behavior.
  • c.It wraps the function so it can parse HTTP requests directly at runtime.
  • d.It registers the function in the app's route table at import time and generates OpenAPI metadata.Correct
Why?

The decorator actively registers the function for GET / in the app's internal route table when the module is imported and simultaneously populates the OpenAPI schema, enabling the ASGI layer to dispatch matching requests. Calling it pure syntax sugar is a common misconception because it fundamentally alters the application's routing registry and automatic documentation rather than leaving framework behavior unchanged.

Just read this? Test yourself on what you have been reading.

Read the original → fastapi.tiangolo.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.

See open roles