Sample code for 30+ languages & platforms
AutoIt

Faire - Update Inventory Levels

See more Faire Examples

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

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

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

$oHttp = ObjCreate("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 = ObjCreate("Chilkat.JsonObject")
$oJson.UpdateString("inventories[0].sku","vanilla-candle")
$oJson.UpdateInt("inventories[0].current_quantity",24)
$oJson.UpdateBool("inventories[0].discontinued",False)
$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",False)
$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",True)
$oJson.UpdateNull("inventories[2].backordered_until")
$oJson.UpdateString("inventories[3].sku","fall-candle")
$oJson.UpdateNull("inventories[3].current_quantity")
$oJson.UpdateBool("inventories[3].discontinued",False)
$oJson.UpdateNull("inventories[3].backordered_until")

$oHttp.SetRequestHeader "Content-Type","application/json"
$oHttp.SetRequestHeader "X-FAIRE-ACCESS-TOKEN","<access_token>"

$oSbRequestBody = ObjCreate("Chilkat.StringBuilder")
$oJson.EmitSb($oSbRequestBody)

$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpSb("PATCH","https://www.faire.com/api/v1/products/options/inventory-levels",$oSbRequestBody,"utf-8","application/json",$oResp)
If ($bSuccess = False) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Exit
EndIf

$oSbResponseBody = ObjCreate("Chilkat.StringBuilder")
$oResp.GetBodySb($oSbResponseBody)

$oJResp = ObjCreate("Chilkat.JsonObject")
$oJResp.LoadSb($oSbResponseBody)
$oJResp.EmitCompact = False

ConsoleWrite("Response Body:" & @CRLF)
ConsoleWrite($oJResp.Emit() & @CRLF)

Local $iRespStatusCode = $oResp.StatusCode
ConsoleWrite("Response Status Code = " & $iRespStatusCode & @CRLF)
If ($iRespStatusCode >= 400) Then
    ConsoleWrite("Response Header:" & @CRLF)
    ConsoleWrite($oResp.Header & @CRLF)
    ConsoleWrite("Failed." & @CRLF)
    Exit
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

Local $sId
Local $sProduct_id
Local $bActive
Local $bDeleted
Local $sName
Local $sku
Local $iAvailable_quantity
Local $sCreated_at
Local $sUpdated_at
Local $iRetail_price_cents
Local $iWholesale_price_cents
Local $sBackordered_until
Local $iJ
Local $iCount_j
Local $sValue

Local $i = 0
Local $iCount_i = $oJResp.SizeOfArray("options")
While $i < $iCount_i
    $oJResp.I = $i
    $sId = $oJResp.StringOf("options[i].id")
    $sProduct_id = $oJResp.StringOf("options[i].product_id")
    $bActive = $oJResp.BoolOf("options[i].active")
    $bDeleted = $oJResp.BoolOf("options[i].deleted")
    $sName = $oJResp.StringOf("options[i].name")
    $sku = $oJResp.StringOf("options[i].sku")
    $iAvailable_quantity = $oJResp.IntOf("options[i].available_quantity")
    $sCreated_at = $oJResp.StringOf("options[i].created_at")
    $sUpdated_at = $oJResp.StringOf("options[i].updated_at")
    $iRetail_price_cents = $oJResp.IntOf("options[i].retail_price_cents")
    $iWholesale_price_cents = $oJResp.IntOf("options[i].wholesale_price_cents")
    $sBackordered_until = $oJResp.StringOf("options[i].backordered_until")
    $iJ = 0
    $iCount_j = $oJResp.SizeOfArray("options[i].variations")
    While $iJ < $iCount_j
        $oJResp.J = $iJ
        $sName = $oJResp.StringOf("options[i].variations[j].name")
        $sValue = $oJResp.StringOf("options[i].variations[j].value")
        $iJ = $iJ + 1
    Wend
    $i = $i + 1
Wend