API-Based Products
Querying, Filtering and Pagination Pravin Y Pawar
Learning Outcomes:
At the end of this video, you will be able to:
• Design and implement RESTful APIs with efficient
filtering and pagination functionalities.
Faculty
Querying, Filtering, and Pagination
Enterprises use REST APIs to provide access to their
data and services
• Large datasets returned by REST APIs can cause
bandwidth and processing issues
• User interfaces typically show limited data, requiring
efficient data management
REST APIs should support querying, filtering, and
pagination for better data handling
• Consumers can specify parameters and criteria for
data retrieval and define response ranges
• can be based on elements, date and time, or offset
and limit values
Image source: https://kemicaldev.hashnode.dev/filtering-sorting-and-paging-for-aspnet-core-rest-apis-part-1
Limiting via Query-String Parameters
Filtering and pagination in an API can be effectively
managed using query-string parameters
• The API interface can be designed with parameters
such as offset and limit
• Offset specifies the starting item number in a
collection,
• Limit indicates the maximum number of items to
return
For instance, Faculty
GET http://www.foo.com/books?offset=0&limit=25,
• Meaning retrieve the first 25 books
• Subsequent items require adjusting the offset value
Limiting via Query-String Parameters
The parameters, offset and limit, are not standardized
• Can vary among API providers
• Other examples of similar parameters include start,
count, page, and rpp (records per page)
API designers have flexibility in naming these
parameters based on business requirements
Faculty
Filtering
Filtering restricts the results returned in an API
response by specifying additional search criteria
• Complex filtering may be necessary for APIs
supporting intricate sets of search criteria based on
resource attributes
• Simple filtering criteria like starts-with or contains
can help manage complexity
Filtering criteria are typically specified using a filter
query-string with delimiter-separated name/value pairs
• Example: GET
http://www.foo.com/users?filter="name::mahesh|city
::mumbai"
Image source: https://www.atatus.com/blog/rest-api-design-filtering-sorting-and-pagination/
Filter Implementation
Three approaches
Mapping filter criteria to backend database SQL
queries to retrieve matching data with minimal
messaging
Implementing filter criteria in the service layer,
applying them to fetched data, especially for complex
criteria or business logic
Implementing filters on the API's intermediary layer,
useful when database or service layers remain Faculty
unchanged, although this may be complex due to
limited programming support
Deciding on the approach should prioritize
implementing filtering as close to the resource data
store as possible for optimal efficiency.
Reference
API Management:
An Architect's Guide to Developing and Managing APIs
for Your Organization
- by Brajesh De (Author)
Faculty
Summary
In this video, you have learned about:
• Effective API design which involves implementing
features
• Like filtering and pagination
Faculty
MCQ1
Which delimiter is commonly used to separate
individual filter phrases in a query-string parameter for
filtering in RESTful APIs?
a) Comma (,)
b) Vertical bar (|)
c) Colon (:)
d) Ampersand (&)
Faculty
Correct answer: b) Vertical bar (|)
MCQ2
What is the recommended approach when deciding
where to implement filtering in an API architecture?
a) Implement filtering in the API's intermediary layer for
simplicity.
b) Apply filtering in the service implementation layer to
ensure flexibility.
c) Map filter criteria directly to the front-end for faster
processing. Faculty
d) Implement filtering as close to the resource data
store as possible.
Correct answer: d) Implement filtering as close to the
resource data store as possible.
MCQ3
In a REST API call, what does the "limit" query-string
parameter signify?
a) The maximum number of data sources allowed for
filtering.
b) The maximum number of attributes to be returned in
the response.
c) The beginning item number in a collection.
d) The maximum number of items to be returned in the Faculty
response.
Correct answer: d) The maximum number of items to
be returned in the response.