Sample code for 30+ languages & platforms
React Native Requires Chilkat v11.0.0+

Shopify Query a variant for its inventory item ID

See more Shopify Examples

Query a product variant to find the ID of its inventory item.

Chilkat React Native Downloads

React Native
import { Http, HttpResponse, JsonObject } from '@chilkat/react-native'

async function chilkatExample() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const http = new Http();

  http.login = 'SHOPIFY_PRIVATE_API_KEY';
  http.password = 'SHOPIFY_PRIVATE_API_KEY';

  http.accept = 'application/json';

  const resp = new HttpResponse();
  try {
    await http.httpNoBodyAsync('GET', 'https://{shop}.myshopify.com/admin/api/2020-04/products/{product_id}/variants/{variant_id}.json', resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  console.log(`Response Status Code: ${resp.statusCode}`);

  const jsonResponse = new JsonObject();
  jsonResponse.load(resp.bodyStr);
  jsonResponse.emitCompact = false;
  console.log(jsonResponse.emit());

  if (resp.statusCode !== 200) {
    console.log('Failed.');
    return;
  }

  // Sample output...
  // (See the parsing code below..)
  // 
  // Use the this online tool to generate parsing code from sample JSON: 
  // Generate Parsing Code from JSON

  // {
  //   "id": 12195009364024,
  //   "product_id": 1321541042232,
  //   "title": "xs",
  // ...
  //   "inventory_item_id": 12250274365496,
  // ...
  //   "admin_graphql_api_id": "gid://shopify/ProductVariant/12195009364024"
  // }
  // 

  const id = jsonResponse.intOf('id');
  const productId = jsonResponse.intOf('product_id');
  const title = jsonResponse.stringOf('title');
  const inventoryItemId = jsonResponse.intOf('inventory_item_id');
  const adminGraphqlApiId = jsonResponse.stringOf('admin_graphql_api_id');
}