Sample code for 30+ languages & platforms
SQL Server

Faire - Update Inventory Levels

See more Faire Examples

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

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    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
    --     }
    --   ]
    -- }

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'inventories[0].sku', 'vanilla-candle'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'inventories[0].current_quantity', 24
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'inventories[0].discontinued', 0
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'inventories[0].backordered_until'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'inventories[1].sku', 'cinnamon-candle'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'inventories[1].current_quantity', 0
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'inventories[1].discontinued', 0
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'inventories[1].backordered_until', '20190314T000915.000Z'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'inventories[2].sku', 'fall-candle'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'inventories[2].current_quantity', 0
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'inventories[2].discontinued', 1
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'inventories[2].backordered_until'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'inventories[3].sku', 'fall-candle'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'inventories[3].current_quantity'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'inventories[3].discontinued', 0
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'inventories[3].backordered_until'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-FAIRE-ACCESS-TOKEN', '<access_token>'

    DECLARE @sbRequestBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbRequestBody OUT

    EXEC sp_OAMethod @json, 'EmitSb', @success OUT, @sbRequestBody

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpSb', @success OUT, 'PATCH', 'https://www.faire.com/api/v1/products/options/inventory-levels', @sbRequestBody, 'utf-8', 'application/json', @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbRequestBody
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbRequestBody
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        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

    DECLARE @id nvarchar(4000)

    DECLARE @product_id nvarchar(4000)

    DECLARE @active int

    DECLARE @deleted int

    DECLARE @name nvarchar(4000)

    DECLARE @sku nvarchar(4000)

    DECLARE @available_quantity int

    DECLARE @created_at nvarchar(4000)

    DECLARE @updated_at nvarchar(4000)

    DECLARE @retail_price_cents int

    DECLARE @wholesale_price_cents int

    DECLARE @backordered_until nvarchar(4000)

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @value nvarchar(4000)

    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'options'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'options[i].id'
        EXEC sp_OAMethod @jResp, 'StringOf', @product_id OUT, 'options[i].product_id'
        EXEC sp_OAMethod @jResp, 'BoolOf', @active OUT, 'options[i].active'
        EXEC sp_OAMethod @jResp, 'BoolOf', @deleted OUT, 'options[i].deleted'
        EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'options[i].name'
        EXEC sp_OAMethod @jResp, 'StringOf', @sku OUT, 'options[i].sku'
        EXEC sp_OAMethod @jResp, 'IntOf', @available_quantity OUT, 'options[i].available_quantity'
        EXEC sp_OAMethod @jResp, 'StringOf', @created_at OUT, 'options[i].created_at'
        EXEC sp_OAMethod @jResp, 'StringOf', @updated_at OUT, 'options[i].updated_at'
        EXEC sp_OAMethod @jResp, 'IntOf', @retail_price_cents OUT, 'options[i].retail_price_cents'
        EXEC sp_OAMethod @jResp, 'IntOf', @wholesale_price_cents OUT, 'options[i].wholesale_price_cents'
        EXEC sp_OAMethod @jResp, 'StringOf', @backordered_until OUT, 'options[i].backordered_until'
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'options[i].variations'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'options[i].variations[j].name'
            EXEC sp_OAMethod @jResp, 'StringOf', @value OUT, 'options[i].variations[j].value'
            SELECT @j = @j + 1
          END
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbRequestBody
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO