Sample code for 30+ languages & platforms
DataFlex

Faire - Update Inventory Levels

See more Faire Examples

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

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Handle hoJson
    Variant vSbRequestBody
    Handle hoSbRequestBody
    Variant vResp
    Handle hoResp
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Handle hoJResp
    Integer iRespStatusCode
    String sId
    String sProduct_id
    Boolean iActive
    Boolean iDeleted
    String sName
    String sSku
    Integer iAvailable_quantity
    String sCreated_at
    String sUpdated_at
    Integer iRetail_price_cents
    Integer iWholesale_price_cents
    String sBackordered_until
    Integer j
    Integer iCount_j
    String sValue
    Integer i
    Integer iCount_i
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

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

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "inventories[0].sku" "vanilla-candle" To iSuccess
    Get ComUpdateInt Of hoJson "inventories[0].current_quantity" 24 To iSuccess
    Get ComUpdateBool Of hoJson "inventories[0].discontinued" False To iSuccess
    Get ComUpdateNull Of hoJson "inventories[0].backordered_until" To iSuccess
    Get ComUpdateString Of hoJson "inventories[1].sku" "cinnamon-candle" To iSuccess
    Get ComUpdateInt Of hoJson "inventories[1].current_quantity" 0 To iSuccess
    Get ComUpdateBool Of hoJson "inventories[1].discontinued" False To iSuccess
    Get ComUpdateString Of hoJson "inventories[1].backordered_until" "20190314T000915.000Z" To iSuccess
    Get ComUpdateString Of hoJson "inventories[2].sku" "fall-candle" To iSuccess
    Get ComUpdateInt Of hoJson "inventories[2].current_quantity" 0 To iSuccess
    Get ComUpdateBool Of hoJson "inventories[2].discontinued" True To iSuccess
    Get ComUpdateNull Of hoJson "inventories[2].backordered_until" To iSuccess
    Get ComUpdateString Of hoJson "inventories[3].sku" "fall-candle" To iSuccess
    Get ComUpdateNull Of hoJson "inventories[3].current_quantity" To iSuccess
    Get ComUpdateBool Of hoJson "inventories[3].discontinued" False To iSuccess
    Get ComUpdateNull Of hoJson "inventories[3].backordered_until" To iSuccess

    Send ComSetRequestHeader To hoHttp "Content-Type" "application/json"
    Send ComSetRequestHeader To hoHttp "X-FAIRE-ACCESS-TOKEN" "<access_token>"

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbRequestBody
    If (Not(IsComObjectCreated(hoSbRequestBody))) Begin
        Send CreateComObject of hoSbRequestBody
    End
    Get pvComObject of hoSbRequestBody to vSbRequestBody
    Get ComEmitSb Of hoJson vSbRequestBody To iSuccess

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoSbRequestBody to vSbRequestBody
    Get pvComObject of hoResp to vResp
    Get ComHttpSb Of hoHttp "PATCH" "https://www.faire.com/api/v1/products/options/inventory-levels" vSbRequestBody "utf-8" "application/json" vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComGetBodySb Of hoResp vSbResponseBody To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
    If (Not(IsComObjectCreated(hoJResp))) Begin
        Send CreateComObject of hoJResp
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess
    Set ComEmitCompact Of hoJResp To False

    Showln "Response Body:"
    Get ComEmit Of hoJResp To sTemp1
    Showln sTemp1

    Get ComStatusCode Of hoResp To iRespStatusCode
    Showln "Response Status Code = " iRespStatusCode
    If (iRespStatusCode >= 400) Begin
        Showln "Response Header:"
        Get ComHeader Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Procedure_Return
    End

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

    Move 0 To i
    Get ComSizeOfArray Of hoJResp "options" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJResp To i
        Get ComStringOf Of hoJResp "options[i].id" To sId
        Get ComStringOf Of hoJResp "options[i].product_id" To sProduct_id
        Get ComBoolOf Of hoJResp "options[i].active" To iActive
        Get ComBoolOf Of hoJResp "options[i].deleted" To iDeleted
        Get ComStringOf Of hoJResp "options[i].name" To sName
        Get ComStringOf Of hoJResp "options[i].sku" To sSku
        Get ComIntOf Of hoJResp "options[i].available_quantity" To iAvailable_quantity
        Get ComStringOf Of hoJResp "options[i].created_at" To sCreated_at
        Get ComStringOf Of hoJResp "options[i].updated_at" To sUpdated_at
        Get ComIntOf Of hoJResp "options[i].retail_price_cents" To iRetail_price_cents
        Get ComIntOf Of hoJResp "options[i].wholesale_price_cents" To iWholesale_price_cents
        Get ComStringOf Of hoJResp "options[i].backordered_until" To sBackordered_until
        Move 0 To j
        Get ComSizeOfArray Of hoJResp "options[i].variations" To iCount_j
        While (j < iCount_j)
            Set ComJ Of hoJResp To j
            Get ComStringOf Of hoJResp "options[i].variations[j].name" To sName
            Get ComStringOf Of hoJResp "options[i].variations[j].value" To sValue
            Move (j + 1) To j
        Loop

        Move (i + 1) To i
    Loop



End_Procedure