API Pagination Guide
This document provides comprehensive guidance on implementing pagination in DeployStack Backend APIs. Pagination is essential for handling large datasets efficiently and providing a good user experience.Overview
DeployStack uses offset-based pagination with standardized query parameters and response formats. This approach provides:- Consistent API Interface: All paginated endpoints use the same parameter names and response structure
- Performance: Reduces memory usage and response times for large datasets
- User Experience: Enables smooth navigation through large result sets
- Scalability: Handles growing datasets without performance degradation
Standard Pagination Parameters
Query Parameters Schema
All paginated endpoints should accept these standardized query parameters:Parameter Details
-
limit
(optional, default: 20)- Type: String (converted to Number in handler)
- Range: 1-100
- Description: Maximum number of items to return
- Validation: Must be a positive integer between 1 and 100
-
offset
(optional, default: 0)- Type: String (converted to Number in handler)
- Range: ≥ 0
- Description: Number of items to skip from the beginning
- Validation: Must be a non-negative integer
Parameter Validation in Handlers
Query parameters are always strings in HTTP. Convert and validate them in your route handlers:Standard Response Format
Response Schema
All paginated endpoints should return responses in this format:Response Example
Pagination Metadata Fields
total
: Total number of items available (across all pages)limit
: Number of items per page (echoes the request parameter)offset
: Current starting position (echoes the request parameter)has_more
: Boolean indicating if more items are available after this page
Implementation Pattern
1. Route Schema Definition
2. Route Handler Implementation
Database-Level Pagination (Advanced)
For better performance with large datasets, implement pagination at the database level:Using Drizzle ORM
Updated Route Handler
Client-Side Usage Examples
JavaScript/TypeScript
Vue.js Composable
Best Practices
1. Consistent Parameter Validation
Always use the same validation rules across all endpoints:2. Proper Error Handling
3. Performance Considerations
- Database Pagination: Use
LIMIT
andOFFSET
at the database level for large datasets - Indexing: Ensure proper database indexes on columns used for sorting
- Caching: Consider caching total counts for frequently accessed endpoints
- Reasonable Limits: Enforce maximum page sizes (e.g., 100 items)
4. OpenAPI Documentation
Include clear pagination documentation in your API specs:Common Pitfalls and Solutions
1. Inconsistent Response Formats
❌ Wrong: Different endpoints use different response structures2. Missing Validation
❌ Wrong: No parameter validation3. Performance Issues
❌ Wrong: Loading all data then slicing4. Incorrect Total Count
❌ Wrong: Using paginated results lengthReal-World Examples
Example 1: MCP Servers List (Current Implementation)
Example 2: Search Endpoint (Reference Implementation)
The search endpoint (/mcp/servers/search
) demonstrates the complete pagination pattern and can serve as a reference for implementing pagination in other endpoints.