Skip to content
tezvyn:

Path Parameters: Turning URL Parts into Variables

Source: fastapi.tiangolo.comEasyHow cards are made

Path Parameters: Turning URL Parts into Variables

Path parameters turn parts of a URL, like /users/123, into typed function arguments. FastAPI uses this to create endpoints for specific resources, like fetching a user by their ID. The footgun is forgetting type hints; without int, 123 is just a string.

Why it exists

Web applications need to handle requests for specific, individual items. Hardcoding an endpoint for every single item (/items/1, /items/2, etc.) is impossible. Path parameters solve this by creating a template for an endpoint that can handle any unique identifier.

The mental model

Think of a path parameter as a fill-in-the-blank in your URL. You define a route like /users/{user_id}. When a request comes in for /users/42, FastAPI takes the 42, sees it corresponds to the {user_id} blank, and passes it as an argument to your handler function. It connects the URL structure directly to your Python function's signature.

How it works

In FastAPI, you declare a path parameter using curly braces in the decorator string, like @app.get("/items/{item_id}"). Then, you add a parameter with the exact same name to your function definition: def read_item(item_id):. FastAPI automatically links them. By adding a type hint, like item_id: int, FastAPI will also validate that the incoming path segment is a valid integer and convert it from a string before calling your function. If it's not a valid integer (e.g., /items/foo), FastAPI returns a 422 Unprocessable Entity error automatically.

When to use it

Use path parameters to identify a unique resource. This is the cornerstone of RESTful API design. Common examples include fetching a user by ID (/users/{user_id}), retrieving a specific product (/products/{product_id}), or accessing a blog post by its unique slug (/posts/{post_slug}).

When not to use it

Do not use path parameters for optional filtering, sorting, or pagination. Those belong in query parameters (e.g., /items?limit=10&sort=desc). A path parameter identifies the resource itself; query parameters describe how you want to view or filter a collection of resources. Using a path parameter for optional data makes your URL structure rigid and non-standard.

One canonical example

In a FastAPI app, you define a route with @app.get("/users/{user_id}"). Your function signature must match: def read_user(user_id: int):. A request to the URL /users/123 will execute the read_user function, with the integer 123 automatically validated and passed in as the user_id argument. The function could then return a JSON response like {"user_id": 123}.

Interview question

In FastAPI, what is the primary consequence of defining a path parameter without a type hint (e.g., item_id instead of item_id: int)?

  • a.FastAPI will automatically infer the correct data type from the incoming URL segment.
  • b.FastAPI will return a 422 Unprocessable Entity error for any request to that endpoint.
  • c.The path parameter will always be treated as a string, regardless of its content.Correct
  • d.The application will fail to start due to a type declaration error.
Why?

The card explicitly states that 'without int, 123 is just a string,' meaning FastAPI treats untyped path parameters as strings by default. FastAPI does not infer types for path parameters; it requires explicit type hints for validation and conversion.

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