Cursor-based pagination helps you get data in small parts without missing or repeating items. It uses a marker (cursor) to remember where you left off.
Cursor-based pagination in Rest API
GET /items?limit=10&cursor=abc123limit sets how many items to get per page.
cursor is a token that points to where to start the next page.
GET /products?limit=5GET /products?limit=5&cursor=eyJpZCI6MTAwfQ==This is a simple web server using Flask. It returns items in pages. You ask for a page with a limit and an optional cursor. The server finds where to start, sends that many items, and gives a cursor for the next page.
from flask import Flask, request, jsonify app = Flask(__name__) # Sample data: list of items with IDs items = [{'id': i, 'name': f'Item {i}'} for i in range(1, 101)] @app.route('/items') def get_items(): limit = int(request.args.get('limit', 10)) cursor = request.args.get('cursor') # Find start index based on cursor start_index = 0 if cursor: try: start_id = int(cursor) # Find the index of the item with id == start_id for idx, item in enumerate(items): if item['id'] == start_id: start_index = idx + 1 break except ValueError: pass # Get the slice of items page_items = items[start_index:start_index + limit] # Prepare next cursor if len(page_items) == limit and (start_index + limit) < len(items): next_cursor = str(page_items[-1]['id']) else: next_cursor = None return jsonify({ 'items': page_items, 'next_cursor': next_cursor }) if __name__ == '__main__': app.run(debug=True)
Cursor-based pagination is more reliable than page-number pagination when data changes often.
The cursor is usually an encoded value like an ID or timestamp.
Always return the next cursor in the response so the client can request the next page.
Cursor-based pagination uses a marker to get the next set of data.
It helps avoid missing or repeating items when data changes.
Clients send the cursor to get the next page, and servers return a new cursor if more data exists.