# URL Parameters vs Query Strings in Express.js

When building APIs with Express.js, understanding how data flows through URLs is fundamental. Two of the most common ways to pass data in a request URL are **URL parameters (route params)** and **query strings**.

They may look similar at first glance, but they serve very different purposes in API design.

* * *

## 🔹 What Are URL Parameters?

URL parameters (also called **route parameters**) are dynamic values embedded directly in the URL path.

They are typically used to **identify a specific resource**.

### Example:

```plaintext
/users/123
```

Here:

*   `123` is a **URL parameter**
    
*   It represents a **specific user ID**
    

### Express Example:

```js
app.get('/users/:id', (req, res) => {
  const userId = req.params.id;
  res.send(`User ID is ${userId}`);
});
```

### Key Characteristics:

*   Part of the URL path
    
*   Used as **identifiers**
    
*   Required to match the route
    
*   Clean and RESTful
    

* * *

## 🔹 What Are Query Parameters?

Query parameters are key-value pairs appended to the end of a URL after a `?`.

They are used to **modify or filter data**, not identify it.

### Example:

```plaintext
/users?age=25&city=Kolkata
```

Here:

*   `age=25` and `city=Kolkata` are **query parameters**
    
*   They act as **filters**
    

### Express Example:

```js
app.get('/users', (req, res) => {
  const age = req.query.age;
  const city = req.query.city;

  res.send(`Filtering users by age ${age} and city ${city}`);
});
```

### Key Characteristics:

*   Appear after `?` in the URL
    
*   Used as **filters, sorting, or modifiers**
    
*   Optional by nature
    
*   Can have multiple values
    

Mind map -

![](https://cdn.hashnode.com/uploads/covers/69517129b3f42beb72514f64/b326bf9d-5238-4b40-8bf4-10557b063269.png align="center")

* * *

## 🔹 Core Difference: Params vs Query

| Feature | URL Parameters | Query Parameters |
| --- | --- | --- |
| Purpose | Identify a resource | Filter or modify results |
| Location | Inside URL path | After `?` |
| Example | `/users/123` | `/users?age=25` |
| Express Access | `req.params` | `req.query` |
| Nature | Required | Optional |

* * *

### Mind map -

![](https://cdn.hashnode.com/uploads/covers/69517129b3f42beb72514f64/03386d8d-8f7c-4431-80ae-a805493a15f4.png align="center")

## 🔹 Practical Examples

### 1\. User Profile (Identifier → Params)

```plaintext
GET /users/123
```

```js
app.get('/users/:id', (req, res) => {
  const id = req.params.id;
  res.send(`Fetching user with ID ${id}`);
});
```

✔ Use params because you're targeting **one specific user**

* * *

### 2\. Search or Filtering (Modifier → Query)

```plaintext
GET /users?age=25&city=Kolkata
```

```js
app.get('/users', (req, res) => {
  const { age, city } = req.query;
  res.send(`Filtering users with age ${age} in ${city}`);
});
```

✔ Use query because you're **refining results**

* * *

### 3\. Combining Both

You can use both together:

```plaintext
GET /users/123/posts?sort=latest
```

```js
app.get('/users/:id/posts', (req, res) => {
  const userId = req.params.id;
  const sort = req.query.sort;

  res.send(`Posts of user ${userId}, sorted by ${sort}`);
});
```

* * *

## 🔹 When to Use Params vs Query

### Use URL Parameters when:

*   You are accessing a **specific resource**
    
*   The value is **mandatory**
    
*   It represents an **identity**
    

✔ Examples:

*   `/products/456`
    
*   `/orders/789`
    

* * *

### Use Query Parameters when:

*   You are **filtering, sorting, or paginating**
    
*   The values are **optional**
    
*   You want flexible API behavior
    

✔ Examples:

*   `/products?category=electronics`
    
*   `/orders?status=delivered&page=2`
    

* * *

## 🔹 Best Practices

*   Treat **params as identifiers**, not filters
    
*   Keep URLs **clean and meaningful**
    
*   Avoid mixing responsibilities (don’t use query for IDs)
    
*   Use query params for:
    
    *   Pagination (`page=1`)
        
    *   Sorting (`sort=price`)
        
    *   Filtering (`category=books`)
        

* * *

## 🔹 Mental Model (Important)

*   **Params = “Which exact thing?”**
    
*   **Query = “How should I view or filter it?”**
    

* * *

## 🔹 Final Takeaway

If you're designing APIs:

*   Think of **URL params as primary keys**
    
*   Think of **query strings as SQL WHERE clauses**
    

This distinction leads to cleaner, more scalable, and RESTful API design.

* * *

If you want, I can next help you turn this into a **perfect Hashnode blog with visuals + diagrams + SEO optimization**, or even align it with your cohort grading rubric.
