ModernCalcs

API Pagination Calculator

Compute offset/limit, total pages, and item range from total records, page size, and current page.

Showing
4160 of 247
Total Pages
13
Current Page
3
Offset
40
Limit
20
prev: 2next: 4
GET /items?offset=40&limit=20

API Pagination Calculator: Offset/Limit Math, Solved

Converting a user-facing 'page 3 of 20' into the offset/limit values your database query or API call actually needs is simple arithmetic — but easy to get off-by-one wrong under deadline pressure. This calculator handles the conversion both directions: page number to offset/limit, and total records to page count.

Formula
offset = (page − 1) × pageSize; totalPages = ceil(totalRecords / pageSize)

Page numbers are clamped to the valid range [1, totalPages] — requesting page 0 or a page beyond the data isn't meaningful.

Why the −1 in the Offset Formula

Page 1 should start at the very first record (offset 0), not skip a page's worth of records — subtracting 1 from the page number before multiplying by page size is what makes page 1 map to offset 0 instead of offset pageSize.

Why Total Pages Rounds Up

247 records at 20 per page is 12.35 pages — but a partial 13th page (containing the remaining 7 records) is still a real page users need to see, so the calculation always rounds up (ceiling), never truncates.

Offset/Limit vs. Cursor Pagination

Offset/limit pagination is simple to reason about but can perform poorly on very large, frequently-changing datasets (a large offset means the database still has to skip over many rows) and can show duplicate/skipped items if data changes between page loads. Cursor-based pagination (tracking a stable pointer instead of a numeric offset) avoids both issues but doesn't support 'jump to page 5' the same way — this calculator covers the offset/limit style specifically.

Practical Examples

Converting a UI Page Number to a Query

User is viewing page 3 of a 20-per-page list.

  • 1.Total Records: 247, Page Size: 20, Current Page: 3
  • 2.Result: offset=40, limit=20, showing 41–60 of 247

What Gets Calculated

  • Offset and limit for the current page
  • Total page count
  • Item range shown ("Showing X–Y of Z")
  • Next/previous page availability

Good Use Cases

  • Converting a UI page number into a SQL LIMIT/OFFSET query
  • Debugging off-by-one errors in pagination logic
  • Documenting expected pagination behavior for an API
  • Sanity-checking a pagination component's page-count math

Frequently Asked Questions

How is offset calculated?

offset = (currentPage − 1) × pageSize — page 1 starts at offset 0, page 2 starts at offset pageSize, and so on. This matches the offset/limit convention used by most SQL-backed REST APIs.

How is total pages calculated?

totalPages = ceil(totalRecords / pageSize) — rounded up, since a partially-full last page still counts as a page. If totalRecords is 0, the calculator shows 1 page with an empty result range.

What if I enter a current page beyond the total pages?

The calculator clamps it down to the last valid page, since requesting a page beyond the data would return an empty result in most real APIs.

Does this match cursor-based pagination too?

Not directly — cursor-based pagination (used by APIs like Stripe's list endpoints) doesn't use numeric offsets at all; it uses opaque cursor tokens pointing at a specific record. This calculator specifically covers offset/limit-style (page-number-based) pagination, the more common pattern for internal and simpler REST APIs.