Api pagination

From aj-geddes/useful-ai-prompts · View interactive version

Api pagination is an AI agent skill from the GitHub repository aj-geddes/useful-ai-prompts. Implement efficient pagination strategies for large datasets using offset/limit, cursor-based, and keyset pagination. Use when returning collections, managing large result sets, or optimizing query performance.

How to use this skill

Skill file

API Pagination

Table of Contents

Overview

Implement scalable pagination strategies for handling large datasets with efficient querying, navigation, and performance optimization.

When to Use

Quick Start

Minimal working example:

// Node.js offset/limit implementation
app.get('/api/users', async (req, res) => {
  const page = parseInt(req.query.page) || 1;
  const limit = Math.min(parseInt(req.query.limit) || 20, 100); // Max 100
  const offset = (page - 1) * limit;

  try {
    const [users, total] = await Promise.all([
      User.find()
        .skip(offset)
        .limit(limit)
        .select('id email firstName lastName createdAt'),
      User.countDocuments()
    ]);

    const totalPages = Math.ceil(total / limit);

    res.json({
      data: users,
      pagination: {
        page,
        limit,
        total,
        totalPages,
        hasNext: page < totalPages,
// ... (see reference guides for full implementation)

Reference Guides

Detailed implementations in the references/ directory:

| Guide | Contents |

|---|---|

| Offset/Limit Pagination | Offset/Limit Pagination |

| Cursor-Based Pagination | Cursor-Based Pagination |

| Keyset Pagination | Keyset Pagination |

| Search Pagination | Search Pagination |

| Pagination Response Formats | Pagination Response Formats |

| Python Pagination (SQLAlchemy) | Python Pagination (SQLAlchemy) |

Best Practices

✅ DO

❌ DON'T

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.