What You'll Learn
- The difference between path parameters and query parameters
- When to use each one
- How they appear in API documentation
- Common mistakes that break APIs
Why This Matters
Every API designer and API caller has to decide where to put each value — in the path or in the query string. Pick wrong and your API becomes inconsistent, hard to cache, and confusing to use. This lesson gives you a simple rule that works for almost every case.
Simple Explanation
Both path parameters and query parameters are values inside the URL. The difference is where they live and what they mean:
GET /users/123/posts?status=published&sort=date
123is a path parameter — embedded in the path, identifies a specific resource.status=publishedandsort=dateare query parameters — after the?, modify how the resource is returned.
Path Parameters
A path parameter is a value embedded in the URL path. It usually identifies a specific resource or a sub-resource.
Examples
/users/123 → 123 is the user ID
/users/123/posts/456 → 123 is the user ID, 456 is the post ID
/orgs/acme/repos/api → acme is the org name, api is the repo name
How APIs document them
API docs usually write path parameters with curly braces or a colon:
GET /users/{userId}
GET /users/:userId/posts/:postId
When to use a path parameter
- To identify a specific resource by ID or name.
- To identify a hierarchy (user → posts → comments).
- When the value is required — without it, the URL makes no sense.
Query Parameters
A query parameter is a key-value pair after the ? in the URL. It modifies how the server returns the resource — filters, sorts, pagination, options.
Examples
/users?role=admin → filter by role
/users?sort=name&order=desc → sort by name descending
/posts?status=published&page=2 → filter + paginate
/search?q=hello+world → search query
When to use a query parameter
- To filter a collection (only show some items).
- To sort the results.
- To paginate (page, limit, offset).
- When the value is optional.
- For search queries.
The Simple Rule
If the value identifies what resource you want, use a path parameter.
If the value modifies how the resource is returned, use a query parameter.
Quick test
Ask yourself: "Could I remove this value and the URL still makes sense?"
/users/123without the 123 →/users(different resource, but still meaningful). Path parameter./users?role=adminwithoutrole=admin→/users(returns all users instead of just admins). Query parameter.
Side-by-Side Comparison
| Aspect | Path parameter | Query parameter |
|---|---|---|
| Position | Inside the path | After ? |
| Identifies | A specific resource | How to filter/sort/paginate |
| Required? | Usually yes | Usually optional |
| Example | /users/123 | /users?role=admin |
| Doc syntax | {userId} or :userId | ?role=string |
| Caching | Each path = separate cache entry | Each query combo = separate cache entry |
Combining Both
Most real API calls use both at once:
GET /users/123/posts?status=published&sort=date&page=2&limit=10
Translation: "On user 123's posts, give me page 2 (10 per page) of published posts, sorted by date."
123— path param, identifies the user.status,sort,page,limit— query params, modify the result.
Common Mistakes
- Using query params for resource IDs.
/users?id=123instead of/users/123. It works, but it breaks REST conventions and makes caching harder. - Using path params for filters.
/users/activelooks like "get user with ID 'active'". Use/users?status=activeinstead. - Mixing case.
?Role=adminand?role=adminare different parameters. Pick lowercase and stick with it. - Not URL-encoding query values.
?q=Anita & Ravibreaks the URL. Use?q=Anita%20%26%20Ravi. - Putting optional fields in the path. If
includeis optional, it belongs in the query, not the path:/users?include=posts, not/users/include/posts.
Practical Exercise (5 minutes)
For each scenario, decide: path parameter or query parameter?
- Get user with ID 42.
- Get all users who are admins.
- Get the third page of posts, 20 per page.
- Get post 99's comments.
- Search posts for the word "API".
- Get user 42's posts that are published, sorted by date.
Answers: 1. Path (/users/42). 2. Query (/users?role=admin). 3. Query (/posts?page=3&limit=20). 4. Path (/posts/99/comments). 5. Query (/posts?q=API). 6. Both (/users/42/posts?status=published&sort=date).
Mini Challenge
Design the URL for: "Get the 2nd page of comments on post 456, sorted by newest first, 10 per page, only showing comments with at least 5 likes." Write the full URL with path and query parameters.
Key Takeaways
- Path parameters identify what resource you want (
/users/123). - Query parameters modify how the resource is returned (
?role=admin&sort=name). - Path params are usually required; query params are usually optional.
- They combine in real API calls:
/users/123/posts?status=published. - Never use query params for resource IDs, and never use path params for filters.
Previously: Lesson 08 covered URL anatomy.
Today: You learned the two ways to pass values inside a URL.
Next: In lesson 10 — Endpoints and Resources, you'll see how URLs group into collections and resources that make up an API.
FAQ
Can a path parameter be optional?
Technically yes, but it's awkward. APIs usually handle this by having two endpoints: /users (list) and /users/{id} (single). Optional values belong in the query string.
Can I have the same parameter in both path and query?
Yes, but it's confusing. For example, /users/123?userId=456 is technically valid but no one will know which one you mean. Avoid it.
Comments
Comments
Post a Comment