Sample code for 30+ languages & platforms
React Native

Faire - Update Inventory Levels

See more Faire Examples

Update the inventory levels for multiple product options in one request.

Chilkat React Native Downloads

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

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

  const http = new Http();

  // Implements the following CURL command:

  // curl -X PATCH 
  //     -H "Content-Type: application/json" 
  //     -H "X-FAIRE-ACCESS-TOKEN: <access_token>"
  //     -d '{
  //   "inventories": [
  //     {
  //       "sku": "vanilla-candle",
  //       "current_quantity": 24,
  //       "discontinued": false,
  //       "backordered_until": null
  //     },
  //     {
  //       "sku": "cinnamon-candle",
  //       "current_quantity": 0,
  //       "discontinued": false,
  //       "backordered_until": "20190314T000915.000Z"
  //     },
  //     {
  //       "sku": "fall-candle",
  //       "current_quantity": 0,
  //       "discontinued": true,
  //       "backordered_until": null
  //     },
  //     {
  //       "sku": "fall-candle",
  //       "current_quantity": null,
  //       "discontinued": false,
  //       "backordered_until": null
  //     }
  //   ]
  // }' https://www.faire.com/api/v1/products/options/inventory-levels

  // Use the following online tool to generate HTTP code from a CURL command
  // Convert a cURL Command to HTTP Source Code

  // Use this online tool to generate code from sample JSON:
  // Generate Code to Create JSON

  // The following JSON is sent in the request body.

  // {
  //   "inventories": [
  //     {
  //       "sku": "vanilla-candle",
  //       "current_quantity": 24,
  //       "discontinued": false,
  //       "backordered_until": null
  //     },
  //     {
  //       "sku": "cinnamon-candle",
  //       "current_quantity": 0,
  //       "discontinued": false,
  //       "backordered_until": "20190314T000915.000Z"
  //     },
  //     {
  //       "sku": "fall-candle",
  //       "current_quantity": 0,
  //       "discontinued": true,
  //       "backordered_until": null
  //     },
  //     {
  //       "sku": "fall-candle",
  //       "current_quantity": null,
  //       "discontinued": false,
  //       "backordered_until": null
  //     }
  //   ]
  // }

  const json = new JsonObject();
  json.updateString('inventories[0].sku', 'vanilla-candle');
  json.updateInt('inventories[0].current_quantity', 24);
  json.updateBool('inventories[0].discontinued', false);
  json.updateNull('inventories[0].backordered_until');
  json.updateString('inventories[1].sku', 'cinnamon-candle');
  json.updateInt('inventories[1].current_quantity', 0);
  json.updateBool('inventories[1].discontinued', false);
  json.updateString('inventories[1].backordered_until', '20190314T000915.000Z');
  json.updateString('inventories[2].sku', 'fall-candle');
  json.updateInt('inventories[2].current_quantity', 0);
  json.updateBool('inventories[2].discontinued', true);
  json.updateNull('inventories[2].backordered_until');
  json.updateString('inventories[3].sku', 'fall-candle');
  json.updateNull('inventories[3].current_quantity');
  json.updateBool('inventories[3].discontinued', false);
  json.updateNull('inventories[3].backordered_until');

  http.setRequestHeader('Content-Type', 'application/json');
  http.setRequestHeader('X-FAIRE-ACCESS-TOKEN', '<access_token>');

  const sbRequestBody = new StringBuilder();
  json.emitSb(sbRequestBody);

  const resp = new HttpResponse();
  try {
    await http.httpSbAsync('PATCH', 'https://www.faire.com/api/v1/products/options/inventory-levels', sbRequestBody, 'utf-8', 'application/json', resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  const sbResponseBody = new StringBuilder();
  resp.getBodySb(sbResponseBody);

  const jResp = new JsonObject();
  jResp.loadSb(sbResponseBody);
  jResp.emitCompact = false;

  console.log('Response Body:');
  console.log(jResp.emit());

  const respStatusCode = resp.statusCode;
  console.log(`Response Status Code = ${respStatusCode}`);
  if (respStatusCode >= 400) {
    console.log('Response Header:');
    console.log(resp.header);
    console.log('Failed.');
    return;
  }

  // Sample JSON response:
  // (Sample code for parsing the JSON response is shown below)

  // {
  //   "options": [
  //     {
  //       "id": "po_012",
  //       "product_id": "p_ghi",
  //       "active": false,
  //       "deleted": false,
  //       "name": "Fall Scent",
  //       "sku": "fall-candle",
  //       "available_quantity": 0,
  //       "created_at": "20190313T000915.000Z",
  //       "updated_at": "20190315T000915.000Z",
  //       "variations": [
  //         {
  //           "name": "Scent",
  //           "value": "Fall"
  //         }
  //       ],
  //       "retail_price_cents": 599,
  //       "wholesale_price_cents": 300
  //     },
  //     {
  //       "id": "po_789",
  //       "product_id": "p_def",
  //       "active": false,
  //       "deleted": false,
  //       "name": "Cinnamon Scent",
  //       "sku": "cinnamon-candle",
  //       "available_quantity": 0,
  //       "created_at": "20190312T000915.000Z",
  //       "updated_at": "20190315T000915.000Z",
  //       "backordered_until": "20190314T000915.000Z",
  //       "variations": [
  //         {
  //           "name": "Scent",
  //           "value": "Cinnamon"
  //         }
  //       ],
  //       "retail_price_cents": 599,
  //       "wholesale_price_cents": 300
  //     },
  //     {
  //       "id": "po_456",
  //       "product_id": "p_abc",
  //       "active": true,
  //       "deleted": false,
  //       "name": "Vanilla Scent",
  //       "sku": "vanilla-candle",
  //       "available_quantity": 24,
  //       "created_at": "20190314T000915.000Z",
  //       "updated_at": "20190315T000915.000Z",
  //       "variations": [
  //         {
  //           "name": "Scent",
  //           "value": "Vanilla"
  //         }
  //       ],
  //       "retail_price_cents": 599,
  //       "wholesale_price_cents": 300
  //     }
  //   ]
  // }

  // Sample code for parsing the JSON response...
  // Use the following online tool to generate parsing code from sample JSON:
  // Generate Parsing Code from JSON

  let id = '';
  let productId = '';
  let name = '';
  let sku = '';
  let availableQuantity = 0;
  let createdAt = '';
  let updatedAt = '';
  let retailPriceCents = 0;
  let wholesalePriceCents = 0;
  let backorderedUntil = '';
  let j = 0;
  let countJ = 0;
  let value = '';

  let i = 0;
  const countI = jResp.sizeOfArray('options');
  while (i < countI) {
    jResp.i = i;
    id = jResp.stringOf('options[i].id');
    productId = jResp.stringOf('options[i].product_id');
    jResp.boolOf('options[i].active');
    jResp.boolOf('options[i].deleted');
    name = jResp.stringOf('options[i].name');
    sku = jResp.stringOf('options[i].sku');
    availableQuantity = jResp.intOf('options[i].available_quantity');
    createdAt = jResp.stringOf('options[i].created_at');
    updatedAt = jResp.stringOf('options[i].updated_at');
    retailPriceCents = jResp.intOf('options[i].retail_price_cents');
    wholesalePriceCents = jResp.intOf('options[i].wholesale_price_cents');
    backorderedUntil = jResp.stringOf('options[i].backordered_until');
    j = 0;
    countJ = jResp.sizeOfArray('options[i].variations');
    while (j < countJ) {
      jResp.j = j;
      name = jResp.stringOf('options[i].variations[j].name');
      value = jResp.stringOf('options[i].variations[j].value');
      j++;
    }

    i++;
  }
}