How to get product_id and variant_id from inventoryItem_id of Shopify product via GraphQL API

Here’s a sample PHP code to make a GraphQL API request to get the product ID and variant ID of an InventoryItem using Shopify’s GraphQL API and PHP cURL:

  $inventory_item_id = "INVENTORY_ITEM_ID";
  $query = '
    query {
      node(id: "'.$inventory_item_id.'") {
        ... on InventoryItem {
          variant {
            product {

  $access_token = "SHOPIFY_ACCESS_TOKEN";
  $shopify_domain = "SHOPIFY_STORE_DOMAIN";

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://$shopify_domain/admin/api/2021-01/graphql.json");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(["query" => $query]));
  curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "X-Shopify-Access-Token: $access_token"

  $result = curl_exec($ch);
  $result = json_decode($result, true);

  $product_id = $result['data']['node']['variant']['product']['id'];
  $variant_id = $result['data']['node']['variant']['id'];
  echo "Product ID: $product_id\n";
  echo "Variant ID: $variant_id\n";

Replace SHOPIFY_ACCESS_TOKEN with your Shopify store’s access token and SHOPIFY_STORE_DOMAIN with the domain of your Shopify store. Replace INVENTORY_ITEM_ID with the actual ID of the InventoryItem you want to retrieve.

The code creates a cURL handle, sets its options, executes the API request, and decodes the JSON response. Finally, it retrieves the product ID and variant ID from the response and outputs them.


Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from Worldhook Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading