Page-based pagination helps split large lists of data into smaller, easy-to-handle pages. This makes loading and viewing data faster and simpler.
0
0
Page-based pagination in Rest API
Introduction
When showing search results on a website with many items.
When displaying user comments or posts in chunks instead of all at once.
When loading product lists in an online store page by page.
When an API returns a large dataset and you want to limit how much data is sent at once.
When you want users to navigate through data step-by-step without overwhelming them.
Syntax
Rest API
GET /items?page=2&limit=10
page is the page number you want to see.
limit is how many items to show per page.
Examples
Get the first page of products, showing 20 items.
Rest API
GET /products?page=1&limit=20
Get the third page of comments, 5 comments per page.
Rest API
GET /comments?page=3&limit=5
Get the tenth page of users, 50 users per page.
Rest API
GET /users?page=10&limit=50
Sample Program
This small web app uses Flask to create an API endpoint /items. It accepts page and limit as query parameters. It returns a JSON with the requested page of items from a list of 100 numbers.
Rest API
from flask import Flask, request, jsonify app = Flask(__name__) # Sample data: list of 100 numbers items = list(range(1, 101)) @app.route('/items') def get_items(): # Get page and limit from query parameters, default to page=1, limit=10 page = int(request.args.get('page', 1)) limit = int(request.args.get('limit', 10)) # Calculate start and end indexes start = (page - 1) * limit end = start + limit # Slice the items list to get current page items page_items = items[start:end] # Prepare response with page info response = { 'page': page, 'limit': limit, 'total_items': len(items), 'items': page_items } return jsonify(response) if __name__ == '__main__': app.run(debug=True)
OutputSuccess
Important Notes
Page numbers usually start at 1, not 0.
If the requested page is beyond the available data, return an empty list.
Always validate and sanitize query parameters to avoid errors.
Summary
Page-based pagination splits data into pages using page number and limit.
It improves performance and user experience by loading data in chunks.
Use query parameters like page and limit to control pagination.