Skip to content
tezvyn:

How do you define and access a FastAPI path parameter?

Source: fastapi.tiangolo.comEasyHow cards are made

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

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

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

Interview question

Which approach correctly defines a FastAPI endpoint that captures an integer item_id from a URL like /items/42?

  • a.Use app.get("/items/{item_id}") and define async def read_item(item_id: int): then use item_id directly inside the functionCorrect
  • b.Use app.get("/items/{item_id}") and define async def read_item(id: int): then use id directly inside the function
  • c.Use app.get("/items/item_id") and define async def read_item(item_id: int): then read request.path_params["item_id"] inside the function
  • d.Use app.get("/items/<item_id>") and define def read_item(item_id): then manually cast with int(item_id) inside the function
Why?

FastAPI binds curly-braced path segments to function arguments with matching names and type hints, automatically converting and injecting the value. Option B is tempting because the route syntax is correct, but the mismatched argument name breaks the default binding unless you use a Path alias.

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