Pagination
Learn how to implement pagination in mock APIs with moclojer. Offset/Limit, Cursor-based, Page-based and Link headers (RFC 5988).
Pagination is essential in APIs that return large lists of data. This guide shows how to implement different pagination strategies with moclojer.
Why Paginate?
β Performance: Don't load 10,000 items at once β UX: Better user experience β Bandwidth: Less data transferred β Cost: Less server processing
Without pagination:
GET /users β [10,000 users] π±With pagination:
GET /users?page=1&limit=20 β [20 users] β
Pagination Strategies
1. Offset/Limit (Most Common)
Concept: Skip X items, return Y items.
Parameters:
limit(orper_page): Quantity per pageoffset(orskip): How many to skip
Math:
Example:
Usage:
2. Page/Limit (More Intuitive)
Concept: Page number + items per page.
Parameters:
page: Page number (starts at 1)limit(orper_page): Items per page
Example:
Usage:
3. Cursor-Based (For Dynamic Feeds)
Concept: Uses a cursor (ID, timestamp) to mark position.
Advantages:
Consistent even with new items
Perfect for infinite scroll
Doesn't allow skipping pages (more secure)
Example:
Usage:
Pagination Metadata
Complete Structure
Pagination Headers
Practical Examples
1. E-commerce API (Products)
Usage:
2. Blog API (Posts with Cursor)
Usage:
3. Comments API (Nested Pagination)
Usage:
4. API with Default Values
Usage:
Link Headers (RFC 5988)
Standard for pagination navigation via headers:
Format:
Relations:
first: First pageprev: Previous pagenext: Next pagelast: Last pageself: Current page
GitHub API Example:
Sorting + Pagination
Usage:
Filters + Pagination
Usage:
Search + Pagination
Usage:
Best Practices
β
Do
Always return metadata
Use headers for totals
Provide navigation links
Maximum item limit
Consistency in names
β Avoid
Pagination without total
Very high limits
Offset without limit
Broken links
Troubleshooting
Problem: Client doesn't know total pages
Solution: Return totalPages or X-Total-Pages header
Problem: Broken navigation links
Solution: Use template vars correctly
Problem: Pagination + Lost filters
Solution: Preserve all query params in links
Strategy Comparison
Offset/Limit
Simple, allows jumping pages
Inconsistent with changes
Traditional APIs
Page/Limit
Intuitive, easy to understand
Same offset problems
UIs with pagination
Cursor
Consistent, perfect for feeds
Can't jump, more complex
Infinite scroll, feeds
Next Steps
Query Parameters - Filters and search
CRUD Operations - Complete operations
Authentication Mock - Simulate authentication
See Also
Last updated
Was this helpful?