0

Retrieves a list of assets for a theme

by
Published Nov 8, 2023

Retrieves a list of assets for a theme. Listing theme assets returns only metadata about each asset. To get an asset's contents, you need to retrieve the asset individually.

Script shopify Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 416 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Retrieves a list of assets for a theme
7
 * Retrieves a list of assets for a theme. Listing theme assets returns only metadata about each asset. To get an asset's contents, you need to retrieve the asset individually.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  theme_id: string,
13
  fields: string | undefined
14
) {
15
  const url = new URL(
16
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/themes/${theme_id}/assets.json`
17
  );
18
  for (const [k, v] of [["fields", fields]]) {
19
    if (v !== undefined && v !== "") {
20
      url.searchParams.append(k, v);
21
    }
22
  }
23
  const response = await fetch(url, {
24
    method: "GET",
25
    headers: {
26
      "X-Shopify-Access-Token": auth.token,
27
    },
28
    body: undefined,
29
  });
30
  if (!response.ok) {
31
    const text = await response.text();
32
    throw new Error(`${response.status} ${text}`);
33
  }
34
  return await response.json();
35
}
36