Retrieve product listings that are published to your app
One script reply has been approved by the moderators Verified

Retrieve product listings that are published to your app. Note: As of version 2019-07, this endpoint implements pagination by using links that are provided in the response header. Sending the page parameter will return an error. To learn more, see Making requests to paginated REST Admin API endpoints.

Created by hugo697 883 days ago Picked 1 time
Submitted by hugo697 Typescript (fetch-only)
Verified 337 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Retrieve product listings that are published to your app
7
 * Retrieve product listings that are published to your app. Note: As of version 2019-07, this endpoint implements pagination by using links that are provided in the response header. Sending the page parameter will return an error. To learn more, see Making requests to paginated REST Admin API endpoints.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  product_ids: string | undefined,
13
  limit: string | undefined,
14
  page: string | undefined,
15
  collection_id: string | undefined,
16
  updated_at_min: string | undefined,
17
  handle: string | undefined
18
) {
19
  const url = new URL(
20
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/product_listings.json`
21
  );
22
  for (const [k, v] of [
23
    ["product_ids", product_ids],
24
    ["limit", limit],
25
    ["page", page],
26
    ["collection_id", collection_id],
27
    ["updated_at_min", updated_at_min],
28
    ["handle", handle],
29
  ]) {
30
    if (v !== undefined && v !== "") {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  const response = await fetch(url, {
35
    method: "GET",
36
    headers: {
37
      "X-Shopify-Access-Token": auth.token,
38
    },
39
    body: undefined,
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47