Page-based pagination helps split large lists of data into smaller, easy-to-handle pages. This makes loading and viewing data faster and simpler.
Page-based pagination in Rest API
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
Rest API
GET /products?page=1&limit=20
Rest API
GET /comments?page=3&limit=5
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)
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.
Practice
1. What is the main purpose of
page-based pagination in REST APIs?easy
Solution
Step 1: Understand pagination concept
Pagination divides data into smaller parts called pages to avoid sending everything at once.Step 2: Identify purpose in REST APIs
Page-based pagination uses page number and limit to load data in chunks, improving performance and user experience.Final Answer:
To split large data into smaller pages for easier loading -> Option AQuick Check:
Pagination = split data into pages [OK]
Hint: Pagination means splitting data into pages [OK]
Common Mistakes:
- Thinking pagination sorts data
- Confusing pagination with encryption
- Assuming pagination combines all data
2. Which of the following is the correct way to request page 3 with 10 items per page using query parameters?
easy
Solution
Step 1: Identify standard query parameters
Page-based pagination commonly usespagefor page number andlimitfor items per page.Step 2: Match parameters to values
Requesting page 3 with 10 items meanspage=3andlimit=10.Final Answer:
/items?page=3&limit=10 -> Option DQuick Check:
page=3 and limit=10 [OK]
Hint: Use page=number and limit=items per page [OK]
Common Mistakes:
- Swapping page and limit values
- Using wrong parameter names like size
- Mixing up page number with limit count
3. Given the API endpoint
/products?page=2&limit=5 and a total of 12 products, how many products will be returned in the response?medium
Solution
Step 1: Calculate items per page
The limit is 5, so each page should have up to 5 products.Step 2: Determine products on page 2
Page 1 has products 1-5, page 2 has products 6-10, so page 2 returns 5 products.Final Answer:
5 -> Option CQuick Check:
limit = 5 products per page [OK]
Hint: Page 2 with limit 5 returns 5 items if available [OK]
Common Mistakes:
- Counting all 12 products instead of page limit
- Assuming leftover products on page 2
- Confusing page number with total items
4. You have this code snippet for pagination parameters:
What is the error if
page = int(request.GET.get('page', 1))
limit = int(request.GET.get('limit', 10))
start = (page - 1) * limit
end = page * limit
items = data[start:end]What is the error if
page is 0?medium
Solution
Step 1: Calculate start index with page=0
start = (0 - 1) * limit = -1 * limit = negative number.Step 2: Understand slicing with negative start
Negative start index in slicing returns items from the end, causing wrong data to be returned.Final Answer:
It causes negative start index, returning wrong items -> Option AQuick Check:
page=0 causes negative start index [OK]
Hint: Page must be >= 1 to avoid negative start index [OK]
Common Mistakes:
- Assuming page=0 is valid
- Expecting syntax error instead of logic error
- Ignoring negative slicing effects
5. You want to implement page-based pagination for an API returning 23 items with a limit of 7 per page. How many pages will the client need to request to get all items?
hard
Solution
Step 1: Calculate full pages
Each page holds 7 items, so 3 full pages hold 21 items (3 * 7 = 21).Step 2: Calculate remaining items
23 total items - 21 = 2 items remain, needing one more page.Step 3: Total pages needed
3 full pages + 1 partial page = 4 pages total.Final Answer:
4 -> Option BQuick Check:
23 items / 7 per page = 4 pages [OK]
Hint: Divide total items by limit, round up for pages [OK]
Common Mistakes:
- Ignoring leftover items needing extra page
- Rounding down instead of up
- Assuming pages equal limit count
