FastAPI: Configure Endpoints with Decorators

FastAPI's path operation decorators configure an endpoint's metadata and behavior. Use them to set status codes (status_code=201), group endpoints with tags, or mark them as deprecated.
Why it exists
Path operation decorators provide a clean, declarative way to attach metadata and configuration to an API endpoint. This separates the API protocol details, like status codes or documentation text, from the business logic inside the endpoint function, keeping the code cleaner.
The mental model
Think of a path operation decorator (@app.get, @app.post) as a label maker for your API endpoint function. You're not just giving it a URL path; you're attaching extra, machine-readable labels that FastAPI uses to generate documentation and configure the HTTP response.
How it works
When you define a route like @app.post("/items/", status_code=201, tags=["items"]), FastAPI reads these arguments directly from the decorator. It uses status_code to set the default HTTP status for successful responses. It uses tags to group this endpoint with others under the "items" heading in the auto-generated OpenAPI docs. Other parameters like summary, description, and deprecated also directly populate the OpenAPI schema, making your API self-documenting.
When to use it
Use decorator arguments any time you need to customize an endpoint's static configuration. Common uses include: setting a creation status code (status_code=201 for a POST), organizing a large API with tags, providing a clear summary for the docs, or gracefully retiring an old endpoint with deprecated=True.
When not to use it
These parameters are for static configuration. Do not use them for logic that needs to change based on the request's input. For example, if a function might return a 200 or a 202 status code based on some condition, you should handle that logic inside your function by returning a Response object directly. The decorator only sets the default for the "happy path".
One canonical example
To create an endpoint that adds a new item, returns a 201 Created status, and is grouped under "items" in the API docs, you would write:
from fastapi import FastAPI, status
app = FastAPI()@app.post("/items/", status_code=status.HTTP_201_CREATED, tags=["items"])
async def create_item(name: str):
return {"name": name}This configures the endpoint's documentation and default success response without adding any logic inside the create_item function itself.
Interview question
Which scenario represents an inappropriate use of a FastAPI path operation decorator's arguments?
- a.Grouping related API endpoints together in the generated documentation.
- b.Setting the default HTTP status code for a successful response.
- c.Dynamically changing the HTTP status code based on specific input conditions.Correct
- d.Declaring an endpoint as no longer recommended for new development.
Why? this is the answer
FastAPI decorator arguments are designed for static configuration, such as setting a default status code or grouping endpoints. Dynamic logic, like returning different status codes based on request input, should be handled within the endpoint function itself, not via decorator arguments.
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.
We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.
See open roles