Guides
Paginating DAS API Requests
The Digital Asset Standard (DAS) API usually has a limit of 10,000 records per request. When you need to retrieve more data, pagination becomes essential. This guide covers the available pagination methods and best practices for implementing them efficiently.
Understanding Sort Options
Before diving into pagination, it's important to understand the available sorting options as they affect how you'll paginate through results:
id
(Default): Sorts assets by their binary IDcreated
: Sorts by creation timestamprecentAction
: Sorts by last update timestampnone
: No sorting applied (not recommended for pagination)
In addition to the sorting options, you can also use the sortDirection
parameters asc
or desc
to sort the results in ascending or descending order.
Pagination Methods
Page-Based Pagination (Recommended for Beginners)
Page-based pagination is the easiest method to implement and understand. It's perfect for beginners and most common use cases.
How it works
- Specify a page number and items per page
- Navigate through results by incrementing the page number
Key parameters
page
: The current page number (starts at 1)limit
: Number of items per page (usually max 10,000)sortBy
: Sorting option
Considerations
- Simple to implement and understand
- Works fine for most common use cases
- Performance may degrade with large page numbers
Cursor-Based Pagination (Recommended for Advanced Users)
For larger datasets or when performance is critical, cursor-based pagination offers better efficiency and is the recommended approach for production applications.
How it works
- Uses a cursor string to track position
- Cursor value is returned with each response
- Pass the cursor to the next request to get the next page
- Perfect for sequential data traversal
Key parameters
cursor
: Position marker for the next set of resultslimit
: Number of items per page (max 10,000)sortBy
: Must be set toid
for cursor-based pagination
Performance Comparison
Method | Complexity | Performance | Use Case |
---|---|---|---|
Page-based | Low | Good for small datasets | Beginners, simple applications |
Cursor-based | Medium | Excellent | Production applications, large datasets |
Range-based | High | Excellent | Advanced queries, parallel processing |
Best Practices
Choose the Right Method
- Use page-based pagination for simple use cases and beginners
- Use cursor-based pagination for production applications and large collections
- Use range-based pagination for advanced querying patterns
Error Handling
- Always check for empty result sets
- Implement retry logic for failed requests
- Handle rate limits appropriately
- Add safety checks to prevent infinite loops
Performance Optimization
- Keep track of the last processed item
- Implement proper caching strategies, but keep in mind that data, especially proofs can change quickly
- Use appropriate sorting methods
- Consider implementing checkpoints for long-running operations
Data Consistency
- Always use sorting when paginating
- Maintain consistent sort parameters between requests
Conclusion
Choosing the right pagination strategy depends on your specific use case:
- For beginners and simple applications: Use page-based pagination
- For production applications: Use cursor-based pagination
- For advanced use cases: Use range-based pagination
Cursor-based pagination is generally the best choice for most applications as it provides excellent performance and is relatively simple to implement. Page-based pagination is perfect for learning and simple use cases, while range-based pagination offers maximum flexibility for advanced scenarios.
Further Reading
- Get All Tokens in a Collection - Retrieve all assets from a specific collection
- Search Assets by Criteria - Advanced search and filtering
- Find Compressed NFTs - Working with compressed NFTs