Bird
Raised Fist0
Rest APIprogramming~5 mins

Pagination with limit and offset in Rest API

Choose your learning style10 modes available
Introduction

Pagination helps to split large lists of data into smaller parts. This makes it easier and faster to get and show data step by step.

When showing a list of products on an online store page.
When loading user comments on a blog post little by little.
When fetching search results from a database in chunks.
When displaying messages in a chat app page by page.
When limiting data sent over the internet to save bandwidth.
Syntax
Rest API
GET /items?limit=10&offset=20

limit tells how many items to get.

offset tells where to start counting items from.

Examples
Get the first 5 users starting from the beginning.
Rest API
GET /users?limit=5&offset=0
Get 10 posts starting from the 11th post (offset 10 means skip first 10).
Rest API
GET /posts?limit=10&offset=10
Get 3 comments starting from the 7th comment.
Rest API
GET /comments?limit=3&offset=6
Sample Program

This small web app uses Flask to create an API endpoint /items. You can add limit and offset as query parameters to get parts of the list of numbers from 1 to 100.

For example, /items?limit=5&offset=10 returns 5 items starting from the 11th number.

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():
    try:
        limit = int(request.args.get('limit', 10))
        offset = int(request.args.get('offset', 0))
    except ValueError:
        return jsonify({'error': 'limit and offset must be integers'}), 400

    # Get the slice of items
    sliced_items = items[offset:offset+limit]

    return jsonify({
        'limit': limit,
        'offset': offset,
        'items': sliced_items
    })

if __name__ == '__main__':
    app.run(debug=True)
OutputSuccess
Important Notes

Always check that limit and offset are valid numbers to avoid errors.

Use reasonable limit values to avoid sending too much data at once.

Offset starts at 0, meaning the first item is at offset 0.

Summary

Pagination splits data into smaller parts using limit and offset.

limit controls how many items to get, offset controls where to start.

This helps apps load data faster and saves internet bandwidth.

Practice

(1/5)
1.

What does the limit parameter control in pagination?

easy
A. The starting position of items to return
B. The sorting order of the items
C. The total number of items in the database
D. The number of items to return in one page

Solution

  1. Step 1: Understand the role of limit

    The limit parameter sets how many items you want to get back in one page of results.
  2. Step 2: Differentiate from offset

    The offset controls where to start, not how many items to return.
  3. Final Answer:

    The number of items to return in one page -> Option D
  4. Quick Check:

    limit = number of items per page [OK]
Hint: Limit sets count of items per page, offset sets start point [OK]
Common Mistakes:
  • Confusing limit with offset
  • Thinking limit controls sorting
  • Assuming limit is total items
2.

Which of the following is the correct way to request the second page of results with 10 items per page using limit and offset?

GET /items?limit=10&offset=?
easy
A. 10
B. 20
C. 0
D. 1

Solution

  1. Step 1: Calculate offset for second page

    For page 1, offset is 0. For page 2, offset = limit * (page_number - 1) = 10 * (2 - 1) = 10.
  2. Step 2: Confirm limit remains 10

    Limit stays 10 to get 10 items per page.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    offset = limit * (page - 1) = 10 [OK]
Hint: Offset = limit x (page number - 1) [OK]
Common Mistakes:
  • Using offset = page number
  • Using offset = limit x page number
  • Setting offset to 0 for second page
3.

Given the following API call, what items will be returned if the full list is ["a", "b", "c", "d", "e", "f"]?

GET /items?limit=2&offset=3
medium
A. ["d", "e"]
B. ["c", "d"]
C. ["a", "b"]
D. ["e", "f"]

Solution

  1. Step 1: Understand offset and limit

    Offset 3 means start from the 4th item (0-based index), which is "d".
  2. Step 2: Select 2 items from offset

    Limit 2 means take 2 items starting at "d", so items are "d" and "e".
  3. Final Answer:

    ["d", "e"] -> Option A
  4. Quick Check:

    Start at index 3, take 2 items = ["d", "e"] [OK]
Hint: Offset is start index, limit is count from there [OK]
Common Mistakes:
  • Starting count at 1 instead of 0
  • Taking wrong slice of items
  • Confusing offset and limit roles
4.

Identify the error in this API call for pagination: GET /items?limit=5&offset=-10

medium
A. Offset should be a string
B. Limit cannot be 5
C. Negative offset is invalid
D. Limit must be greater than offset

Solution

  1. Step 1: Check offset value

    Offset represents the starting position and cannot be negative because you cannot start before the first item.
  2. Step 2: Validate limit and offset rules

    Limit can be any positive number; offset must be zero or positive.
  3. Final Answer:

    Negative offset is invalid -> Option C
  4. Quick Check:

    Offset ≥ 0 required [OK]
Hint: Offset must never be negative [OK]
Common Mistakes:
  • Allowing negative offset
  • Confusing limit and offset roles
  • Assuming offset can be string
5.

You have 53 items and want to paginate with limit=10. How many pages will you have, and what is the offset for the last page?

hard
A. 5 pages, offset 40
B. 6 pages, offset 50
C. 6 pages, offset 45
D. 7 pages, offset 60

Solution

  1. Step 1: Calculate total pages

    Total pages = ceiling of total items / limit = ceil(53 / 10) = 6 pages.
  2. Step 2: Calculate offset for last page

    Offset for last page = limit x (pages - 1) = 10 x (6 - 1) = 50.
  3. Final Answer:

    6 pages, offset 50 -> Option B
  4. Quick Check:

    Pages = 6, offset last = 50 [OK]
Hint: Pages = ceil(total/limit), offset = limit x (pages-1) [OK]
Common Mistakes:
  • Using floor instead of ceiling for pages
  • Calculating offset as limit x pages
  • Ignoring leftover items on last page
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard