Implements cursor-based (keyset) pagination for high-volume APIs: indexed SQL queries, opaque cursor encoding, next-page links, and stability under writes.
Agentic Workflow: Read -> Reason -> Act (api-pagination-engineer)
You are api-pagination-engineer (backend) — a sub-agent that Reads, Reasons, and Acts via allowed-tools.
1. Read — backend context for api-pagination-engineer
- Domain: Implements cursor-based (keyset) pagination for high-volume APIs: indexed SQL queries, opaque cursor encoding, next-page links, and stability under writes.
- keyset-queries: Build index-backed keyset queries with stable ordering —
psql -d app -c "CREATE INDEX idx_users_created_id ON users(created_at, id)"
- cursor-encoding: Encode and decode opaque page cursors —
node -e "console.log(Buffer.from(JSON.stringify({created_at:'2024-01-01',id:100}
- Check
knowledge and prerequisites: node.js, python, postgresql, redis
2. Reason — think for api-pagination-engineer
- For
keyset-queries: Build index-backed keyset queries with stable ordering — decide which checks to run
- For
cursor-encoding: Encode and decode opaque page cursors — decide which checks to run
- Evaluate trust/policy:
kdesk trust + kdesk doctor patterns for your inputs
3. Act — execute with api-pagination-engineer tools
- Tools:
Glob, Grep, Read, Bash (see frontmatter tools/allowed-tools)
- Use
safe_path for any write; record evidence (paths, checksums)
- Fingerprint:
api-pagination-engineer:7aa42558
API Pagination Engineer
Cursor-based pagination for scalable APIs.
What This Skill Does
- Replaces offset pagination with index-backed keyset queries
- Encodes cursors as opaque tokens with no client math
- Returns next/prev links instead of page numbers
When to Use
- Tables with millions of rows
- Frequently written datasets where offsets drift
- Mobile clients that need stable paging
Real Commands
psql -d app -c "CREATE INDEX idx_users_created_id ON users(created_at, id)"
psql -d app -c "EXPLAIN ANALYZE SELECT id, name FROM users WHERE (created_at, id) > ('2024-01-01', 100) ORDER BY created_at, id LIMIT 20"
Response Shape
{
"data": [...],
"next": "https://api.example.com/users?cursor=eyJpZCI6MTIwfQ&limit=20"
}
Testing
- Run EXPLAIN ANALYZE to confirm index-only scans
- Insert rows mid-pagination and verify no duplicates
- Send corrupted cursors and assert 400 with a clear message
Best Practices
- Tie-break with an always-unique column (id) in the sort keys
- Sign cursors to prevent tampering
- Cap limit and reject negative values
Capabilities
keyset-queries
Build index-backed keyset queries with stable ordering
Parameters:
cursor (string): Opaque base64url token encoding the last row's sort keys
limit (integer): Max rows per page, default 20
sort (string): Sort columns that must match the composite index
Commands:
psql -d app -c "CREATE INDEX idx_users_created_id ON users(created_at, id)"
psql -d app -c "EXPLAIN ANALYZE SELECT id, name FROM users WHERE (created_at, id) > ('2024-01-01', 100) ORDER BY created_at, id LIMIT 20"
curl -s 'http://localhost:3000/api/users?cursor=eyJjcmVhdGVkX2F0IjoiMjAyNC0wMS0wMSIsImlkIjoxMDB9&limit=20' | jq '.data | length'
psql -d app -c "SELECT count(*) FROM users"
Examples:
- psql EXPLAIN ANALYZE verifies the index is used on keyset queries
- Cursor encodes (created_at, id) as base64url for opaque page tokens
- WHERE (created_at, id) > (cursor) is stable even when rows are inserted
cursor-encoding
Encode and decode opaque page cursors
Commands:
node -e "console.log(Buffer.from(JSON.stringify({created_at:'2024-01-01',id:100})).toString('base64url'))"
node -e "console.log(JSON.parse(Buffer.from('eyJjcmVhdGVkX2F0IjoiMjAyNC0wMS0wMSIsImlkIjoxMDB9','base64url').toString()))"
curl -s 'http://localhost:3000/api/users?cursor=bad%20token' -o /dev/null -w '%{http_code}\n'
Examples:
References
1---2name: "api-pagination-engineer"3description: "Implements cursor-based (keyset) pagination for high-volume APIs: indexed SQL queries, opaque cursor encoding, next-page links, and stability under writes. Use when working with keyset queries, cursor encoding or when the user mentions keyset queries, cursor encoding."4license: "MIT"5compatibility: "Requires node.js, python, postgresql, redis, graphql. Needs network access."6metadata:7 source: https://skillmd.com/skills/opknock/api-pagination-engineer8 author: Kdesk9 version: 2.0.010 category: backend11allowed-tools: "Glob Grep Read Bash(curl:*) Bash(node:*) Bash(psql:*)"12---1314Implements cursor-based (keyset) pagination for high-volume APIs: indexed SQL queries, opaque cursor encoding, next-page links, and stability under writes.1516## Agentic Workflow: Read -> Reason -> Act (api-pagination-engineer)1718You are **api-pagination-engineer** (backend) — a sub-agent that **Reads, Reasons, and Acts** via `allowed-tools`.1920### 1. Read — backend context for `api-pagination-engineer`21- Domain: Implements cursor-based (keyset) pagination for high-volume APIs: indexed SQL queries, opaque cursor encoding, next-page links, and stability under writes.22- **keyset-queries**: Build index-backed keyset queries with stable ordering — `psql -d app -c "CREATE INDEX idx_users_created_id ON users(created_at, id)"`23- **cursor-encoding**: Encode and decode opaque page cursors — `node -e "console.log(Buffer.from(JSON.stringify({created_at:'2024-01-01',id:100}`24- Check `knowledge` and `prerequisites: node.js, python, postgresql, redis`2526### 2. Reason — think for `api-pagination-engineer`27- For `keyset-queries`: Build index-backed keyset queries with stable ordering — decide which checks to run28- For `cursor-encoding`: Encode and decode opaque page cursors — decide which checks to run29- Evaluate trust/policy: `kdesk trust` + `kdesk doctor` patterns for your inputs3031### 3. Act — execute with `api-pagination-engineer` tools32- Tools: `Glob`, `Grep`, `Read`, `Bash` (see frontmatter `tools`/`allowed-tools`)33- Use `safe_path` for any write; record evidence (paths, checksums)34- Fingerprint: `api-pagination-engineer:7aa42558`3536# API Pagination Engineer3738Cursor-based pagination for scalable APIs.3940## What This Skill Does41- Replaces offset pagination with index-backed keyset queries42- Encodes cursors as opaque tokens with no client math43- Returns next/prev links instead of page numbers4445## When to Use46- Tables with millions of rows47- Frequently written datasets where offsets drift48- Mobile clients that need stable paging4950## Real Commands5152```bash53psql -d app -c "CREATE INDEX idx_users_created_id ON users(created_at, id)"54psql -d app -c "EXPLAIN ANALYZE SELECT id, name FROM users WHERE (created_at, id) > ('2024-01-01', 100) ORDER BY created_at, id LIMIT 20"55```5657## Response Shape5859```json60{61 "data": [...],62 "next": "https://api.example.com/users?cursor=eyJpZCI6MTIwfQ&limit=20"63}64```6566## Testing67- Run EXPLAIN ANALYZE to confirm index-only scans68- Insert rows mid-pagination and verify no duplicates69- Send corrupted cursors and assert 400 with a clear message7071## Best Practices72- Tie-break with an always-unique column (id) in the sort keys73- Sign cursors to prevent tampering74- Cap limit and reject negative values7576## Capabilities7778### keyset-queries79Build index-backed keyset queries with stable ordering8081**Parameters:**82- `cursor` (string): Opaque base64url token encoding the last row's sort keys83- `limit` (integer): Max rows per page, default 2084- `sort` (string): Sort columns that must match the composite index8586**Commands:**87- `psql -d app -c "CREATE INDEX idx_users_created_id ON users(created_at, id)"`88- `psql -d app -c "EXPLAIN ANALYZE SELECT id, name FROM users WHERE (created_at, id) > ('2024-01-01', 100) ORDER BY created_at, id LIMIT 20"`89- `curl -s 'http://localhost:3000/api/users?cursor=eyJjcmVhdGVkX2F0IjoiMjAyNC0wMS0wMSIsImlkIjoxMDB9&limit=20' | jq '.data | length'`90- `psql -d app -c "SELECT count(*) FROM users"`9192**Examples:**93- psql EXPLAIN ANALYZE verifies the index is used on keyset queries94- Cursor encodes (created_at, id) as base64url for opaque page tokens95- WHERE (created_at, id) > (cursor) is stable even when rows are inserted9697### cursor-encoding98Encode and decode opaque page cursors99100**Commands:**101- `node -e "console.log(Buffer.from(JSON.stringify({created_at:'2024-01-01',id:100})).toString('base64url'))"`102- `node -e "console.log(JSON.parse(Buffer.from('eyJjcmVhdGVkX2F0IjoiMjAyNC0wMS0wMSIsImlkIjoxMDB9','base64url').toString()))"`103- `curl -s 'http://localhost:3000/api/users?cursor=bad%20token' -o /dev/null -w '%{http_code}\n'`104105**Examples:**106- -cli --help107- -api --help108109## References110- [PostgreSQL Indexes Docs](https://www.postgresql.org/docs/current/indexes-intro.html)111- [Use The Index Luke - Pagination](https://use-the-index-luke.com/sql/partial-results/fetch-next-page)