0
0
Rest-apiHow-ToBeginner ยท 3 min read

How to Design URL for Filter Endpoint in REST API

Design filter endpoints by using query parameters in the URL to specify filter criteria, like /items?color=red&size=medium. This keeps URLs simple, readable, and flexible for multiple filters without changing the endpoint path.
๐Ÿ“

Syntax

A filter endpoint URL typically uses query parameters after the base path. The syntax is:

  • /resource?key1=value1&key2=value2
  • resource is the main data collection.
  • key=value pairs define filter conditions.
  • Multiple filters are joined by &.
http
/items?color=red&size=medium&in_stock=true
๐Ÿ’ป

Example

This example shows a REST API endpoint URL filtering products by color, size, and availability.

http
GET /products?color=blue&size=large&available=true HTTP/1.1
Host: api.example.com

Response:
[
  {"id": 101, "name": "Blue Jacket", "size": "large", "available": true},
  {"id": 102, "name": "Blue Jeans", "size": "large", "available": true}
]
Output
[{"id": 101, "name": "Blue Jacket", "size": "large", "available": true}, {"id": 102, "name": "Blue Jeans", "size": "large", "available": true}]
โš ๏ธ

Common Pitfalls

Common mistakes when designing filter URLs include:

  • Using path segments for filters, which makes URLs rigid and less flexible.
  • Not encoding special characters in query values.
  • Using inconsistent parameter names or formats.
  • Ignoring case sensitivity or data types in filters.

Correct design uses query parameters consistently and encodes values properly.

http
Wrong:
GET /products/color/red/size/large

Right:
GET /products?color=red&size=large
๐Ÿ“Š

Quick Reference

ConceptDescriptionExample
Base URLMain resource path/products
Query ParameterFilter key-value pair?color=red
Multiple FiltersJoin with &?color=red&size=medium
Boolean FilterTrue/false values?available=true
EncodingEscape special chars?name=red%20shirt
โœ…

Key Takeaways

Use query parameters after the base URL to apply filters flexibly.
Keep filter keys consistent and encode special characters properly.
Avoid using path segments for filters to maintain URL simplicity.
Combine multiple filters with & to refine results easily.
Design URLs to be readable and intuitive for developers and users.