Sample code for 30+ languages & platforms
Tcl

Faire - Update Inventory Levels

See more Faire Examples

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

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

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

set http [new_CkHttp]

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

set json [new_CkJsonObject]

CkJsonObject_UpdateString $json "inventories[0].sku" "vanilla-candle"
CkJsonObject_UpdateInt $json "inventories[0].current_quantity" 24
CkJsonObject_UpdateBool $json "inventories[0].discontinued" 0
CkJsonObject_UpdateNull $json "inventories[0].backordered_until"
CkJsonObject_UpdateString $json "inventories[1].sku" "cinnamon-candle"
CkJsonObject_UpdateInt $json "inventories[1].current_quantity" 0
CkJsonObject_UpdateBool $json "inventories[1].discontinued" 0
CkJsonObject_UpdateString $json "inventories[1].backordered_until" "20190314T000915.000Z"
CkJsonObject_UpdateString $json "inventories[2].sku" "fall-candle"
CkJsonObject_UpdateInt $json "inventories[2].current_quantity" 0
CkJsonObject_UpdateBool $json "inventories[2].discontinued" 1
CkJsonObject_UpdateNull $json "inventories[2].backordered_until"
CkJsonObject_UpdateString $json "inventories[3].sku" "fall-candle"
CkJsonObject_UpdateNull $json "inventories[3].current_quantity"
CkJsonObject_UpdateBool $json "inventories[3].discontinued" 0
CkJsonObject_UpdateNull $json "inventories[3].backordered_until"

CkHttp_SetRequestHeader $http "Content-Type" "application/json"
CkHttp_SetRequestHeader $http "X-FAIRE-ACCESS-TOKEN" "<access_token>"

set sbRequestBody [new_CkStringBuilder]

CkJsonObject_EmitSb $json $sbRequestBody

set resp [new_CkHttpResponse]

set success [CkHttp_HttpSb $http "PATCH" "https://www.faire.com/api/v1/products/options/inventory-levels" $sbRequestBody "utf-8" "application/json" $resp]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkJsonObject $json
    delete_CkStringBuilder $sbRequestBody
    delete_CkHttpResponse $resp
    exit
}

set sbResponseBody [new_CkStringBuilder]

CkHttpResponse_GetBodySb $resp $sbResponseBody

set jResp [new_CkJsonObject]

CkJsonObject_LoadSb $jResp $sbResponseBody
CkJsonObject_put_EmitCompact $jResp 0

puts "Response Body:"
puts [CkJsonObject_emit $jResp]

set respStatusCode [CkHttpResponse_get_StatusCode $resp]
puts "Response Status Code = $respStatusCode"
if {$respStatusCode >= 400} then {
    puts "Response Header:"
    puts [CkHttpResponse_header $resp]
    puts "Failed."
    delete_CkHttp $http
    delete_CkJsonObject $json
    delete_CkStringBuilder $sbRequestBody
    delete_CkHttpResponse $resp
    delete_CkStringBuilder $sbResponseBody
    delete_CkJsonObject $jResp
    exit
}

# 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

set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "options"]
while {$i < $count_i} {
    CkJsonObject_put_I $jResp $i
    set id [CkJsonObject_stringOf $jResp "options[i].id"]
    set product_id [CkJsonObject_stringOf $jResp "options[i].product_id"]
    set active [CkJsonObject_BoolOf $jResp "options[i].active"]
    set deleted [CkJsonObject_BoolOf $jResp "options[i].deleted"]
    set name [CkJsonObject_stringOf $jResp "options[i].name"]
    set sku [CkJsonObject_stringOf $jResp "options[i].sku"]
    set available_quantity [CkJsonObject_IntOf $jResp "options[i].available_quantity"]
    set created_at [CkJsonObject_stringOf $jResp "options[i].created_at"]
    set updated_at [CkJsonObject_stringOf $jResp "options[i].updated_at"]
    set retail_price_cents [CkJsonObject_IntOf $jResp "options[i].retail_price_cents"]
    set wholesale_price_cents [CkJsonObject_IntOf $jResp "options[i].wholesale_price_cents"]
    set backordered_until [CkJsonObject_stringOf $jResp "options[i].backordered_until"]
    set j 0
    set count_j [CkJsonObject_SizeOfArray $jResp "options[i].variations"]
    while {$j < $count_j} {
        CkJsonObject_put_J $jResp $j
        set name [CkJsonObject_stringOf $jResp "options[i].variations[j].name"]
        set value [CkJsonObject_stringOf $jResp "options[i].variations[j].value"]
        set j [expr $j + 1]
    }
    set i [expr $i + 1]
}

delete_CkHttp $http
delete_CkJsonObject $json
delete_CkStringBuilder $sbRequestBody
delete_CkHttpResponse $resp
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $jResp