Make paginated requests to the REST Admin API
REST endpoints support cursor-based pagination. This guide illustrates how to request paginated data from the REST Admin API and access each page of results.
How it works
Anchor link to section titled "How it works"When you send a request to a REST endpoint that supports cursor-based pagination, the response body returns the first page of results. If applicable, then a response header returns links to the next page and the previous page of results. You can use the links in the response header to iterate through the pages of results.
Link header syntax
Anchor link to section titled "Link header syntax"Link headers use the following syntax:
The link header includes arel
parameter that describes the relation of the linked page to the current page of results. The value can either beprevious
ornext
. If your initial request doesn't return enough records to generate an additional page of results, then the response won't have a link header.
Parameters
Anchor link to section titled "Parameters"The URL in the link header can include the following parameters:
Parameter | Description |
---|---|
page_info |
A unique ID used to access a certain page of results. Thepage_info parameter can't be modified and must be used exactly as it appears in the link header URL. |
limit |
The maximum number of results to show on the page:
|
fields |
A comma-separated list of which fields to show in the results. This parameter only works for some endpoints. |
Supported endpoints
Anchor link to section titled "Supported endpoints"You can use cursor-based pagination on mostREST Admin APIGET endpoints that retrieve a list of resources.
If the REST resource includes thelimit
parameter on the GET endpoint, then you can use the endpoint to request paginated data. For example, the产品
resource includes thelimit
parameter on theGET endpoint that retrieves a list of products.
Make a request for paginated data
Anchor link to section titled "Make a request for paginated data"When you make a request to an endpoint that supports paginated results, you can set the number of results to return per page using thelimit
parameter. If you don't specify alimit
, then the endpoint will use itsdefault page limit. You can also set other parameters in this request to narrow down your results.
The following example request asks the product endpoint for all products that belong to a collection (id: 123
), with alimit
of 3 products per page of results:
The response header returns a link header that includes a URL for the next page of results. The response body returns the first page of results, which includes 3 products.
Retrieve the next page of results
Anchor link to section titled "Retrieve the next page of results"To get the next page of results, you can make a request to the URL stored in the link header of thelast response.
The response header includes a link to the previous page of results and a link to the next page. The response body includes the second page of results, which includes the next 3 products after the first page.
You can use the URLs in the link headers to iterate through each page of results. If you make a request to a page, and the response header only includes a link to the previous page, then you've reached the last page of results.
Change the number of results for a specific page
Anchor link to section titled "Change the number of results for a specific page"You can change thelimit
in a link header URL to return a different number of results from a specified point. For example, the following example request asks the product endpoint for all products, with alimit
parameter of 3:
If you change thelimit
in the URL in the link header to 6 and make a request to that URL, then the response body returns the 6 products after the first page of results:
Use the shopify-api npm library
Anchor link to section titled "Use the shopify-api npm library"The@shopify/shopify-api
libraryprovides backend support for JavaScript and TypeScript apps to access the REST Admin API.
下面的代码检索产品的商店by fetching them in batches of 10 until all products have been retrieved. You can store thepageInfo
data between requests using a session.
Limitations and considerations
Anchor link to section titled "Limitations and considerations"- A request that includes the
page_info
parameter can't include any other parameters except forlimit
andfields
(if it applies to the endpoint). If you want your results to be filtered by other parameters, then you need to include those parameters in the first request you make. - The link header URLs are temporary and we don't recommend saving them to use later. Use link header URLs only while working with the request that generated them.
- Any request that sends the
page
parameter will return an error.