REST- Limit and Offset Pagination Concepts
top of page

Tags

Archive

REST- Limit and Offset Pagination Concepts

In a REST API, "limit" and "offset" are commonly used parameters for pagination. They are used to control the number of records returned in a single query and to specify the starting point for the records that are returned.


"Limit" is a parameter that controls the number of records returned in a single query. For example, if a limit of 10 is specified, only the first 10 records matching the query will be returned.


"Offset" is a parameter that controls the starting point for the records that are returned. For example, if an offset of 10 is specified, the first 10 records matching the query will be skipped and the next records will be returned.


Here's an example:


Let's say you have a REST API that returns a list of products, and there are a total of 100 products in the database. If a client makes a request with a limit of 20 and an offset of 0, the API will return the first 20 products in the database. If the client then makes a request with a limit of 20 and an offset of 20, the API will return the next 20 products in the database, starting from 21th product. And so on.

Endpoints for these requests might look like:


  • /products?limit=20&offset=0 - returns first 20 products

  • /products?limit=20&offset=20 - returns next 20 products starting from 21th product

  • /products?limit=20&offset=40 - returns next 20 products starting from 41th product

This way, the client can page through the list of products one page at a time, rather than retrieving the entire list of 100 products all at once. This also helps to improve the performance of the application and reduce the load on the server.


It's also worth noting that some APIs may use page numbers and page size instead of limit and offset. The page number tells which page the client want to see and page size tells the number of records per page. This can be easier for clients to understand and reason about, but the implementation on the server-side needs to convert page number and page size to offset and limit.


Hope this Helps !

Happy Learning.


99 views0 comments

Recent Posts

See All

Template/Path and Query Parameters in REST

In a REST API, "template parameters" and "query parameters" are both used to pass data to the API as part of a request. Template parameters are used to specify resource identifiers in the URL of the A

REST HTTP Status Codes #API

Here is a list of some common HTTP status codes that you may encounter when working with REST APIs: 200 OK: Indicates that the request was successful and the requested information was returned. 201 Cr

Other Posts you may Interested 

bottom of page