How do you define and access a FastAPI path parameter?

Tests FastAPI route-to-function binding. Good answer: curly-brace syntax in the decorator path, a matching typed function argument, and awareness that FastAPI auto-extracts and converts the value.
WHAT THIS TESTS: This question checks whether you know FastAPI's core convention for binding URL path segments to function arguments. The interviewer wants to see that you understand declarative routing, type-driven data conversion, and automatic dependency injection without manual request parsing. Even at a senior level, this serves as a quick filter for whether you have actually built with FastAPI or only read about it, because the pattern is fundamental to every endpoint you will write.
A GOOD ANSWER COVERS: First, declare the parameter in the route using curly-brace syntax identical to Python format strings, for example app.get("/users/{user_id}"). Second, add a function argument with the exact same name and a type hint, such as async def get_user(user_id: int). FastAPI extracts the corresponding URL segment, converts it to the annotated type, and injects it as that argument. Third, mention that the value is then accessed directly inside the function body just like any normal variable, for instance return {"user_id": user_id}. Fourth, optionally note that type annotations drive both data conversion and automatic validation, so a non-integer path segment would yield a clear 422 error without any custom logic inside the endpoint.
COMMON WRONG ANSWERS: A major red flag is reaching for the raw request object and manually splitting the URL string. Another is omitting the type hint, which works in FastAPI but signals you do not understand how the framework uses annotations for conversion and OpenAPI schema generation. A third red flag is using a different argument name than the path template without explaining Path aliasing, since the default behavior requires names to match exactly. Finally, describing Flask-style angle-bracket syntax instead of curly braces reveals framework confusion.
LIKELY FOLLOW-UPS: The interviewer may ask how to constrain a path parameter to a predefined set of values, which is done with Python enums. They might also ask how to handle order when mixing path and query parameters, or how to capture paths that contain forward slashes using a path convertor like /files/{file_path:path}. They could also ask how to add extra validation or metadata using the Path function imported from FastAPI.
ONE CONCRETE EXAMPLE: Write app.get("/items/{item_id}") and define async def read_item(item_id: int). When a client calls GET /items/42, FastAPI converts the string 42 to the integer 42 and passes it as item_id. If the client calls GET /items/foo, FastAPI returns a 422 Unprocessable Entity because foo cannot be parsed as an integer. No manual casting or error handling is required in the endpoint code, which keeps the function clean and testable.
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.