-
Notifications
You must be signed in to change notification settings - Fork 14
Docs: add API pagination guide #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # API Pagination | ||
| ## | ||
|
|
||
| All Private Packagist API endpoints that return lists support pagination using query parameters. This allows you to efficiently retrieve large result sets by fetching data in smaller chunks. | ||
|
|
||
| > **✨ Automatic Pagination**: If you're using the [PHP API client](https://github.com/packagist/private-packagist-api-client), pagination is handled automatically! The client fetches all pages transparently and returns complete results. You don't need to manually handle pagination, Link headers, or page loops - just call the endpoint method and get all results. | ||
|
|
||
| If you were previously using list endpoints without pagination: Your existing code will continue to work, but the hard limit is set to 10,000 items, which covers most use cases. | ||
|
|
||
| ## Query Parameters | ||
|
|
||
| Paginated list endpoints (all that do get a collection of items) accept the following query parameters: | ||
|
|
||
| * `page` (integer, optional): Page number to retrieve (1-10,000). Default: 1 | ||
| * `limit` (integer, optional): Number of items per page (1-10,000). Default: 10,000 | ||
|
|
||
| ### Example Request | ||
|
|
||
| ```bash | ||
| GET /api/packages/?page=2&limit=300 | ||
| ``` | ||
|
|
||
| This fetches the second page of packages with 300 packages per page. | ||
|
|
||
| ## Response Format | ||
|
|
||
| ### Link Header | ||
|
|
||
| Paginated responses include an [RFC 5988](https://tools.ietf.org/html/rfc5988) Link header containing pagination navigation URLs: | ||
|
|
||
| ``` | ||
| Link: <https://packagist.com/api/packages/?page=3&limit=300>; rel="next", | ||
| <https://packagist.com/api/packages/?page=1&limit=300>; rel="prev", | ||
| <https://packagist.com/api/packages/?page=1&limit=300>; rel="first", | ||
| <https://packagist.com/api/packages/?page=10&limit=300>; rel="last" | ||
| ``` | ||
|
|
||
| The Link header includes up to four relations: | ||
|
|
||
| * `next`: URL to the next page (if available) | ||
| * `prev`: URL to the previous page (if available) | ||
| * `first`: URL to the first page | ||
| * `last`: URL to the last page | ||
|
|
||
| ### Response Body | ||
|
|
||
| The response body contains a JSON array of resources, just like non-paginated responses: | ||
|
|
||
| ```json | ||
| [ | ||
| { | ||
| "id": 1, | ||
| "name": "acme/package", | ||
| ... | ||
| }, | ||
| ... | ||
| ] | ||
| ``` | ||
|
|
||
| ## Using the PHP Client | ||
pscheit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| The PHP client includes an AutoPaginator plugin that: | ||
pscheit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 1. **Automatically detects pagination**: Checks for Link headers with `rel="next"` in API responses | ||
| 2. **Fetches all pages**: Makes additional requests to retrieve all pages (using 500 items per page) | ||
| 3. **Merges results**: Combines all pages into a single array | ||
| 4. **Returns complete data**: Your code receives all results, no matter how many pages exist | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.