0
0
Rest APIprogramming~30 mins

Cursor-based pagination in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Cursor-based Pagination in a REST API
📖 Scenario: You are building a simple REST API that returns a list of products. To handle large lists efficiently, you want to implement cursor-based pagination. This means the API will return a limited number of products per request and a cursor to fetch the next set.
🎯 Goal: Build a REST API endpoint that returns products with cursor-based pagination. You will create the data, set a page size, implement the pagination logic, and finally output the paginated products along with the next cursor.
📋 What You'll Learn
Create a list of products with exact names and IDs
Set a page size variable to control how many products are returned per page
Implement cursor-based pagination logic using the product ID as the cursor
Output the current page of products and the next cursor value
💡 Why This Matters
🌍 Real World
Cursor-based pagination is used in APIs to efficiently load large lists of data in small chunks, improving speed and user experience.
💼 Career
Understanding cursor-based pagination is important for backend developers building scalable APIs and for frontend developers consuming paginated data.
Progress0 / 4 steps
1
Create the product list
Create a list called products containing these dictionaries exactly: {'id': 1, 'name': 'Apple'}, {'id': 2, 'name': 'Banana'}, {'id': 3, 'name': 'Cherry'}, {'id': 4, 'name': 'Date'}, {'id': 5, 'name': 'Elderberry'}
Rest API
Need a hint?

Use a list of dictionaries. Each dictionary must have keys 'id' and 'name' with the exact values.

2
Set the page size
Create a variable called page_size and set it to 2 to limit the number of products returned per page
Rest API
Need a hint?

Just create a variable named page_size and assign it the number 2.

3
Implement cursor-based pagination logic
Create a variable called cursor and set it to 0. Then create a list called page that contains products with id greater than cursor, limited to page_size items
Rest API
Need a hint?

Use a list comprehension to filter products with id greater than cursor and slice the list to page_size.

4
Output the paginated products and next cursor
Print the page list and then print the next_cursor which is the id of the last product in page if page is not empty, otherwise print null
Rest API
Need a hint?

Print the page list first. Then find the last product's id in page for next_cursor. If page is empty, print null.