Sample code for 30+ languages & platforms
SQL Server

Etsy: Get the Inventory for a Listing

See more Etsy Examples

Gets the inventory for a listing.

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 GET \
    --   https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING

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

    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING', @sbResponseBody
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    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 @http, 'LastStatus', @respStatusCode OUT

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

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        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)

    -- {
    --   "count": 1,
    --   "results": {
    --     "products": [
    --       {
    --         "product_id": 3361120103,
    --         "property_values": [
    --         ],
    --         "offerings": [
    --           {
    --             "offering_id": 3579642570,
    --             "price": {
    --               "amount": 16000,
    --               "divisor": 100,
    --               "currency_code": "USD",
    --               "currency_formatted_short": "$160.00",
    --               "currency_formatted_long": "$160.00 USD",
    --               "currency_formatted_raw": "160.00"
    --             },
    --             "quantity": 1
    --           }
    --         ]
    --       }
    --     ]
    --   },
    --   "params": {
    --     "listing_id": "720138253",
    --     "write_missing_inventory": false
    --   },
    --   "type": "ListingInventory",
    --   "pagination": {}
    -- }

    -- 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 @product_id int

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @offering_id int

    DECLARE @priceAmount int

    DECLARE @priceDivisor int

    DECLARE @priceCurrency_code nvarchar(4000)

    DECLARE @priceCurrency_formatted_short nvarchar(4000)

    DECLARE @priceCurrency_formatted_long nvarchar(4000)

    DECLARE @priceCurrency_formatted_raw nvarchar(4000)

    DECLARE @quantity int

    DECLARE @count int
    EXEC sp_OAMethod @jResp, 'IntOf', @count OUT, 'count'
    DECLARE @paramsListing_id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @paramsListing_id OUT, 'params.listing_id'
    DECLARE @paramsWrite_missing_inventory int
    EXEC sp_OAMethod @jResp, 'BoolOf', @paramsWrite_missing_inventory OUT, 'params.write_missing_inventory'
    DECLARE @v_type nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'type'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'results.products'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'IntOf', @product_id OUT, 'results.products[i].product_id'
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'results.products[i].property_values'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            SELECT @j = @j + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'results.products[i].offerings'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            EXEC sp_OAMethod @jResp, 'IntOf', @offering_id OUT, 'results.products[i].offerings[j].offering_id'
            EXEC sp_OAMethod @jResp, 'IntOf', @priceAmount OUT, 'results.products[i].offerings[j].price.amount'
            EXEC sp_OAMethod @jResp, 'IntOf', @priceDivisor OUT, 'results.products[i].offerings[j].price.divisor'
            EXEC sp_OAMethod @jResp, 'StringOf', @priceCurrency_code OUT, 'results.products[i].offerings[j].price.currency_code'
            EXEC sp_OAMethod @jResp, 'StringOf', @priceCurrency_formatted_short OUT, 'results.products[i].offerings[j].price.currency_formatted_short'
            EXEC sp_OAMethod @jResp, 'StringOf', @priceCurrency_formatted_long OUT, 'results.products[i].offerings[j].price.currency_formatted_long'
            EXEC sp_OAMethod @jResp, 'StringOf', @priceCurrency_formatted_raw OUT, 'results.products[i].offerings[j].price.currency_formatted_raw'
            EXEC sp_OAMethod @jResp, 'IntOf', @quantity OUT, 'results.products[i].offerings[j].quantity'
            SELECT @j = @j + 1
          END
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO