API Pagination, Filtering & Sorting
Make your SaaS API scale by returning large datasets in pages, letting clients filter and sort results, and exposing consistent query parameters.
API Pagination, Filtering & Sorting is a free AI Powered SaaS: Stripe + Auth + Billing + Deploy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Powered SaaS: Stripe + Auth + Billing + Deploy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Not Return Everything?
An endpoint that returns 50,000 rows is slow, heavy, and crashes clients. Pagination returns data in manageable chunks, while filtering and sorting let clients ask for exactly what they need.
Offset Pagination
The simplest approach uses page and limit. You skip (page - 1) * limit rows and take limit rows.
GET /api/users?page=2&limit=20Implementing Offset in Prisma
Prisma exposes skip and take for offset pagination.
const users = await prisma.user.findMany({
skip: (page - 1) * limit,
take: limit
});Returning Metadata
Clients need to know how many pages exist. Return the data plus a total count and computed page info.
const total = await prisma.user.count();
return Response.json({
data: users,
page,
totalPages: Math.ceil(total / limit)
});The Offset Problem
Large offsets are slow because the database still scans skipped rows, and inserts can shift items between pages. For big or live datasets, prefer cursor pagination.
Cursor Pagination
A cursor points to the last item seen. The next request fetches rows after that cursor — fast and stable even as data changes.
const users = await prisma.user.findMany({
take: limit,
skip: 1,
cursor: { id: lastId },
orderBy: { id: 'asc' }
});Filtering by Query Params
Read filter params from the URL and build a where clause. Use contains for search-style filters.
const q = searchParams.get('search');
const where = q ? { name: { contains: q, mode: 'insensitive' } } : {};Combining Multiple Filters
Build the where object conditionally so only provided params apply. Prisma combines them with AND by default.
const where = {};
if (status) where.status = status;
if (minPrice) where.price = { gte: Number(minPrice) };Sorting Safely
Accept a sort field and direction, but whitelist allowed fields so clients cannot order by arbitrary or sensitive columns.
const allowed = ['name', 'createdAt', 'price'];
const field = allowed.includes(sort) ? sort : 'createdAt';
const orderBy = { [field]: dir === 'desc' ? 'desc' : 'asc' };Validating Query Params
Coerce and clamp inputs: cap limit so a client cannot request a million rows, and default page to 1.
const limit = Math.min(Number(searchParams.get('limit')) || 20, 100);
const page = Math.max(Number(searchParams.get('page')) || 1, 1);Best Practices
Design scalable list endpoints:
- Always paginate large collections
- Use cursor pagination for big or live data
- Return metadata (total, pages)
- Whitelist sort fields and clamp limits
Quick Check
Test your pagination knowledge.
Recap
You learned to build scalable list APIs:
- Paginate with
skip/takeor cursors - Return total and page metadata
- Build dynamic
whereclauses for filtering - Whitelist sort fields and clamp the limit
Your API now handles large datasets efficiently.
Frequently asked questions
Is the “API Pagination, Filtering & Sorting” lesson free?
Yes — the full text of “API Pagination, Filtering & Sorting” is free to read here on the web, and the AI Powered SaaS: Stripe + Auth + Billing + Deploy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Powered SaaS: Stripe + Auth + Billing + Deploy course, upgrade to CoddyKit PRO.
What will I learn in “API Pagination, Filtering & Sorting”?
Make your SaaS API scale by returning large datasets in pages, letting clients filter and sort results, and exposing consistent query parameters. You practise AI Powered SaaS: Stripe + Auth + Billing + Deploy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Powered SaaS: Stripe + Auth + Billing + Deploy?
No prior experience is required. AI Powered SaaS: Stripe + Auth + Billing + Deploy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “API Pagination, Filtering & Sorting” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Powered SaaS: Stripe + Auth + Billing + Deploy lesson?
Yes. Every AI Powered SaaS: Stripe + Auth + Billing + Deploy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- RESTful API Design Principles
- Database Schema & ORM
- First API Endpoints
- API Pagination, Filtering & Sorting