0

Get Product

by
Published Nov 8, 2023

Get a product with its options, variants and media.

Script shopify Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Get Product
5
 * Get a product by numeric ID or GID, with its options, its first 100 variants (price, SKU, inventory item) and its first 10 media.
6
 */
7
export async function main(auth: RT.Shopify, product_id: string) {
8
  const id = product_id.startsWith("gid://")
9
    ? product_id
10
    : `gid://shopify/Product/${product_id}`
11

12
  const response = await fetch(
13
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
14
    {
15
      method: "POST",
16
      headers: {
17
        "X-Shopify-Access-Token": auth.token,
18
        "Content-Type": "application/json",
19
        Accept: "application/json",
20
      },
21
      body: JSON.stringify({
22
        query: `query ($id: ID!) { product(id: $id) { id legacyResourceId title handle status vendor productType tags descriptionHtml createdAt updatedAt publishedAt onlineStoreUrl totalInventory tracksInventory hasOnlyDefaultVariant seo { title description } category { id fullName } priceRangeV2 { minVariantPrice { amount currencyCode } maxVariantPrice { amount currencyCode } } options { id name position optionValues { id name hasVariants } } variantsCount { count } variants(first: 100) { nodes { id legacyResourceId title displayName sku price compareAtPrice position inventoryQuantity inventoryPolicy availableForSale taxable selectedOptions { name value } barcodes(first: 5) { nodes { value type } } inventoryItem { id tracked requiresShipping measurement { weight { value unit } } } } pageInfo { hasNextPage endCursor } } media(first: 10) { nodes { id alt mediaContentType status preview { image { url width height } } } } } }`,
23
        variables: { id },
24
      }),
25
    }
26
  )
27

28
  if (!response.ok) {
29
    throw new Error(`${response.status} ${await response.text()}`)
30
  }
31

32
  const { data, errors } = await response.json()
33
  if (errors) {
34
    throw new Error(JSON.stringify(errors))
35
  }
36

37
  return data.product
38
}
39

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Retrieves a single product
    7
     * Retrieves a single product.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      product_id: string,
    13
      fields: string | undefined
    14
    ) {
    15
      const url = new URL(
    16
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/products/${product_id}.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