Sample code for 30+ languages & platforms
Xbase++

Faire - Update Inventory Levels

See more Faire Examples

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

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oJson
LOCAL oSbRequestBody
LOCAL oResp
LOCAL oSbResponseBody
LOCAL oJResp
LOCAL nRespStatusCode
LOCAL cId
LOCAL cProduct_id
LOCAL nActive
LOCAL nDeleted
LOCAL cName
LOCAL cSku
LOCAL nAvailable_quantity
LOCAL cCreated_at
LOCAL cUpdated_at
LOCAL nRetail_price_cents
LOCAL nWholesale_price_cents
LOCAL cBackordered_until
LOCAL j
LOCAL nCount_j
LOCAL cValue
LOCAL i
LOCAL nCount_i

nSuccess := 0

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oHttp := CreateObject("Chilkat.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
//      }
//    ]
//  }

oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("inventories[0].sku", "vanilla-candle")
oJson:UpdateInt("inventories[0].current_quantity", 24)
oJson:UpdateBool("inventories[0].discontinued", 0)
oJson:UpdateNull("inventories[0].backordered_until")
oJson:UpdateString("inventories[1].sku", "cinnamon-candle")
oJson:UpdateInt("inventories[1].current_quantity", 0)
oJson:UpdateBool("inventories[1].discontinued", 0)
oJson:UpdateString("inventories[1].backordered_until", "20190314T000915.000Z")
oJson:UpdateString("inventories[2].sku", "fall-candle")
oJson:UpdateInt("inventories[2].current_quantity", 0)
oJson:UpdateBool("inventories[2].discontinued", 1)
oJson:UpdateNull("inventories[2].backordered_until")
oJson:UpdateString("inventories[3].sku", "fall-candle")
oJson:UpdateNull("inventories[3].current_quantity")
oJson:UpdateBool("inventories[3].discontinued", 0)
oJson:UpdateNull("inventories[3].backordered_until")

oHttp:SetRequestHeader("Content-Type", "application/json")
oHttp:SetRequestHeader("X-FAIRE-ACCESS-TOKEN", "<access_token>")

oSbRequestBody := CreateObject("Chilkat.StringBuilder")
oJson:EmitSb(oSbRequestBody)

oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpSb("PATCH", "https://www.faire.com/api/v1/products/options/inventory-levels", oSbRequestBody, "utf-8", "application/json", oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oJson:destroy()
    oSbRequestBody:destroy()
    oResp:destroy()
    RETURN
ENDIF

oSbResponseBody := CreateObject("Chilkat.StringBuilder")
oResp:GetBodySb(oSbResponseBody)

oJResp := CreateObject("Chilkat.JsonObject")
oJResp:LoadSb(oSbResponseBody)
oJResp:EmitCompact := 0

? "Response Body:"
? oJResp:Emit()

nRespStatusCode := oResp:StatusCode
? "Response Status Code = " + Str(nRespStatusCode)
IF (nRespStatusCode >= 400)
    ? "Response Header:"
    ? oResp:Header
    ? "Failed."
    oHttp:destroy()
    oJson:destroy()
    oSbRequestBody:destroy()
    oResp:destroy()
    oSbResponseBody:destroy()
    oJResp:destroy()
    RETURN
ENDIF

//  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

i := 0
nCount_i := oJResp:SizeOfArray("options")
DO WHILE i < nCount_i
    oJResp:I := i
    cId := oJResp:StringOf("options[i].id")
    cProduct_id := oJResp:StringOf("options[i].product_id")
    nActive := oJResp:BoolOf("options[i].active")
    nDeleted := oJResp:BoolOf("options[i].deleted")
    cName := oJResp:StringOf("options[i].name")
    cSku := oJResp:StringOf("options[i].sku")
    nAvailable_quantity := oJResp:IntOf("options[i].available_quantity")
    cCreated_at := oJResp:StringOf("options[i].created_at")
    cUpdated_at := oJResp:StringOf("options[i].updated_at")
    nRetail_price_cents := oJResp:IntOf("options[i].retail_price_cents")
    nWholesale_price_cents := oJResp:IntOf("options[i].wholesale_price_cents")
    cBackordered_until := oJResp:StringOf("options[i].backordered_until")
    j := 0
    nCount_j := oJResp:SizeOfArray("options[i].variations")
    DO WHILE j < nCount_j
        oJResp:J := j
        cName := oJResp:StringOf("options[i].variations[j].name")
        cValue := oJResp:StringOf("options[i].variations[j].value")
        j := j + 1
    ENDDO
    i := i + 1
ENDDO

oHttp:destroy()
oJson:destroy()
oSbRequestBody:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()