Query String Filtering
What you'll learn: How to use URL query parameters to request specific subsets of data from a REST API.
What Is Query String Filtering?
Query string filtering is the practice of passing filter parameters directly in the URL to tell the server exactly which subset of data you want. Instead of retrieving all products or all users and then filtering client-side, you ask the server to do the filtering by including parameters after a ? in your URL.
Think of it like ordering at a restaurant: instead of getting the entire menu delivered to your table and then picking what you want, you tell the waiter upfront: "I'd like vegetarian dishes under $15." The kitchen only prepares what matches your criteria.
How It Works
You append key-value pairs to your API endpoint using query strings:
GET /api/products?category=electronics&price_max=500&in_stock=true
Here you're filtering products by:
- Category equals "electronics"
- Maximum price of 500
- Only items in stock
On the server side, the API reads these parameters and builds a database query to return only matching records. The response is smaller, faster, and exactly what you need.
Common Filter Patterns
Exact match: ?status=active
Comparison: ?age_min=18&age_max=65
Search: ?q=javascript
Multiple values: ?tags=tutorial,beginner or ?tags[]=tutorial&tags[]=beginner
Boolean: ?featured=true
You can combine filtering with pagination from previous lessons:
GET /api/articles?category=tech&page=2&limit=20
This requests page 2 of tech articles, 20 per page.
Key Takeaway: Query string filtering lets you request precisely the data you need by passing filter criteria in the URL, reducing bandwidth and improving performance by offloading filtering work to the server.