Sample code for 30+ languages & platforms
SQL Server

Amazon SP-API Get Order Items

See more Amazon SP-API Examples

Returns detailed order item information for the order that you specify.

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 requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- Gets detailed order item information for a specified order ID
    -- The order ID is something like "902-1845936-5435065" and it is the AmazonOrderId returned in the JSON when getting the list of orders.  For example:
    -- {
    --   "payload": {
    --     "CreatedBefore": "1.569521782042E9",
    --     "Orders": [
    --       {
    --         "AmazonOrderId": "902-1845936-5435065",
    --         "PurchaseDate": "1970-01-19T03:58:30Z",
    -- ...

    -- However, when using the sandbox, instead use the explicit keyword TEST_CASE_200
    DECLARE @orderId nvarchar(4000)
    SELECT @orderId = 'TEST_CASE_200'

    DECLARE @authAws int
    EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @authAws OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @authAws, 'AccessKey', 'AWS_ACCESS_KEY'
    EXEC sp_OASetProperty @authAws, 'SecretKey', 'AWS_SECRET_KEY'
    EXEC sp_OASetProperty @authAws, 'ServiceName', 'execute-api'
    -- Use the region that is correct for your needs.
    EXEC sp_OASetProperty @authAws, 'Region', 'eu-west-1'

    -- First get a restricted data token for the given order ID.
    -- This requires an LWA access token which cannot be more than 1 hour old.
    -- See Fetch SP-API LWA Access Token
    DECLARE @jsonLwaToken int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonLwaToken OUT

    EXEC sp_OAMethod @jsonLwaToken, 'LoadFile', @success OUT, 'qa_data/tokens/sp_api_lwa_token.json'
    IF @success = 0
      BEGIN

        PRINT 'Failed to load LWA access token.'
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @jsonLwaToken
        RETURN
      END

    -- Must use the non-sandbox domain for getting the RDT.
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT

    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'sellingpartnerapi-eu.amazon.com', 443, 1, 1
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @jsonLwaToken
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @authAws

    -- Add the x-amz-access-token request header.
    DECLARE @lwa_token nvarchar(4000)
    EXEC sp_OAMethod @jsonLwaToken, 'StringOf', @lwa_token OUT, 'access_token'
    EXEC sp_OAMethod @rest, 'ClearAllHeaders', @success OUT
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'x-amz-access-token', @lwa_token

    -- We're going to send a POST with the following JSON body:

    -- {
    --   "restrictedResources": [
    --     {
    --       "method": "GET",
    --       "path": "/orders/v0/orders/{orderId}/orderItems",
    --       "dataElements": ["buyerInfo"]
    --     }
    --   ]
    -- }

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

    EXEC sp_OAMethod @sbPath, 'Append', @success OUT, '/orders/v0/orders/'
    EXEC sp_OAMethod @sbPath, 'Append', @success OUT, @orderId
    EXEC sp_OAMethod @sbPath, 'Append', @success OUT, '/orderItems'

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

    EXEC sp_OAMethod @jsonRc, 'UpdateString', @success OUT, 'restrictedResources[0].method', 'GET'
    EXEC sp_OAMethod @sbPath, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @jsonRc, 'UpdateString', @success OUT, 'restrictedResources[0].path', @sTmp0
    EXEC sp_OAMethod @jsonRc, 'UpdateString', @success OUT, 'restrictedResources[0].dataElements[0]', 'buyerInfo'

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

    EXEC sp_OAMethod @jsonRc, 'EmitSb', @success OUT, @sbRequest

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

    DECLARE @uri nvarchar(4000)
    SELECT @uri = '/tokens/2021-03-01/restrictedDataToken'
    EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', @uri, @sbRequest, @sbResponse
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @jsonLwaToken
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbPath
        EXEC @hr = sp_OADestroy @jsonRc
        EXEC @hr = sp_OADestroy @sbRequest
        EXEC @hr = sp_OADestroy @sbResponse
        RETURN
      END

    -- Examine the response status.
    DECLARE @statusCode int
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @statusCode OUT
    IF @statusCode <> 200
      BEGIN

        PRINT 'Response status code: ' + @statusCode

        EXEC sp_OAGetProperty @rest, 'ResponseStatusText', @sTmp0 OUT
        PRINT 'Response status text: ' + @sTmp0

        PRINT 'Response body: '
        EXEC sp_OAMethod @sbResponse, 'GetAsString', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @jsonLwaToken
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbPath
        EXEC @hr = sp_OADestroy @jsonRc
        EXEC @hr = sp_OADestroy @sbRequest
        EXEC @hr = sp_OADestroy @sbResponse
        RETURN
      END

    -- Get the restricted data token.
    DECLARE @jsonResp int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonResp OUT

    EXEC sp_OAMethod @jsonResp, 'LoadSb', @success OUT, @sbResponse
    DECLARE @restrictedDataToken nvarchar(4000)
    EXEC sp_OAMethod @jsonResp, 'StringOf', @restrictedDataToken OUT, 'restrictedDataToken'

    PRINT 'Restricted Data Token: ' + @restrictedDataToken

    -- ------------------------------------------------------------------------------------------------------------
    -- ------------------------------------------------------------------------------------------------------------
    -- Now that we have the RDT, we can use it to get information about the order.
    -- 
    -- Yes, the SP-API is horribly tedious and painful.  You must use an RDT specifically tailored to each request requiring an RDT,
    -- the RDT must not be over an hour old, and if you need to get a new RDT you must get it using an LWA token that itself is not
    -- more than an hour old.  If the LWA is more than an hour old, you must get a new one.  Ughhh!!!!!
    -- ------------------------------------------------------------------------------------------------------------

    -- Disconnect from the non-sandbox domain.  This example will use the sandbox.
    -- (The RDT was obtained using the non-sandbox domain.  For some reason, the sandbox domain does not work for getting the RDT.)
    EXEC sp_OAMethod @rest, 'Disconnect', @success OUT, 100

    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'sandbox.sellingpartnerapi-eu.amazon.com', 443, 1, 1
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @jsonLwaToken
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbPath
        EXEC @hr = sp_OADestroy @jsonRc
        EXEC @hr = sp_OADestroy @sbRequest
        EXEC @hr = sp_OADestroy @sbResponse
        EXEC @hr = sp_OADestroy @jsonResp
        RETURN
      END

    EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @authAws

    EXEC sp_OAMethod @rest, 'ClearAllHeaders', @success OUT
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'x-amz-access-token', @restrictedDataToken

    EXEC sp_OAMethod @rest, 'ClearAllQueryParams', @success OUT
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'MarketplaceIds', 'ATVPDKIKX0DER'

    EXEC sp_OAMethod @rest, 'ClearAllPathParams', @success OUT
    EXEC sp_OAMethod @rest, 'AddPathParam', @success OUT, '{orderId}', @orderId

    SELECT @uri = '/orders/v0/orders/{orderId}/orderItems'
    EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', @uri, @sbResponse
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @jsonLwaToken
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbPath
        EXEC @hr = sp_OADestroy @jsonRc
        EXEC @hr = sp_OADestroy @sbRequest
        EXEC @hr = sp_OADestroy @sbResponse
        EXEC @hr = sp_OADestroy @jsonResp
        RETURN
      END

    -- Examine the response status.
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @statusCode OUT
    IF @statusCode <> 200
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ResponseStatusText', @sTmp0 OUT
        PRINT 'Response status text: ' + @sTmp0

        PRINT 'Response body: '
        EXEC sp_OAMethod @sbResponse, 'GetAsString', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @jsonLwaToken
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbPath
        EXEC @hr = sp_OADestroy @jsonRc
        EXEC @hr = sp_OADestroy @sbRequest
        EXEC @hr = sp_OADestroy @sbResponse
        EXEC @hr = sp_OADestroy @jsonResp
        RETURN
      END

    -- If successful, gets a JSON response such as the following:

    -- {
    --   "payload": {
    --     "AmazonOrderId": "902-1845936-5435065",
    --     "OrderItems": [
    --       {
    --         "ASIN": "B00551Q3CS",
    --         "OrderItemId": "05015851154158",
    --         "SellerSKU": "NABetaASINB00551Q3CS",
    --         "Title": "B00551Q3CS [Card Book]",
    --         "QuantityOrdered": 1,
    --         "QuantityShipped": 0,
    --         "ProductInfo": {
    --           "NumberOfItems": 1
    --         },
    --         "ItemPrice": {
    --           "CurrencyCode": "USD",
    --           "Amount": "10.00"
    --         },
    --         "ItemTax": {
    --           "CurrencyCode": "USD",
    --           "Amount": "1.01"
    --         },
    --         "PromotionDiscount": {
    --           "CurrencyCode": "USD",
    --           "Amount": "0.00"
    --         },
    --         "IsGift": false,
    --         "ConditionId": "New",
    --         "ConditionSubtypeId": "New",
    --         "IsTransparency": false,
    --         "SerialNumberRequired": false,
    --         "IossNumber": "",
    --         "DeemedResellerCategory": "IOSS",
    --         "StoreChainStoreId": "ISPU_StoreId",
    --         "BuyerRequestedCancel": {
    --           "IsBuyerRequestedCancel": true,
    --           "BuyerCancelReason": "Found cheaper somewhere else."
    --         }
    --       }
    --     ]
    --   }
    -- }

    -- Use this online tool to generate parsing code from sample JSON: 
    -- Generate Parsing Code from JSON

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

    EXEC sp_OAMethod @json, 'LoadSb', @success OUT, @sbResponse
    EXEC sp_OASetProperty @json, 'EmitCompact', 0
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @ASIN nvarchar(4000)

    DECLARE @OrderItemId nvarchar(4000)

    DECLARE @SellerSKU nvarchar(4000)

    DECLARE @Title nvarchar(4000)

    DECLARE @QuantityOrdered int

    DECLARE @QuantityShipped int

    DECLARE @NumberOfItems int

    DECLARE @CurrencyCode nvarchar(4000)

    DECLARE @Amount nvarchar(4000)

    DECLARE @ItemTaxCurrencyCode nvarchar(4000)

    DECLARE @ItemTaxAmount nvarchar(4000)

    DECLARE @PromotionDiscountCurrencyCode nvarchar(4000)

    DECLARE @PromotionDiscountAmount nvarchar(4000)

    DECLARE @IsGift int

    DECLARE @ConditionId nvarchar(4000)

    DECLARE @ConditionSubtypeId nvarchar(4000)

    DECLARE @IsTransparency int

    DECLARE @SerialNumberRequired int

    DECLARE @IossNumber nvarchar(4000)

    DECLARE @DeemedResellerCategory nvarchar(4000)

    DECLARE @StoreChainStoreId nvarchar(4000)

    DECLARE @IsBuyerRequestedCancel int

    DECLARE @BuyerCancelReason nvarchar(4000)

    DECLARE @AmazonOrderId nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @AmazonOrderId OUT, 'payload.AmazonOrderId'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'payload.OrderItems'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @ASIN OUT, 'payload.OrderItems[i].ASIN'
        EXEC sp_OAMethod @json, 'StringOf', @OrderItemId OUT, 'payload.OrderItems[i].OrderItemId'
        EXEC sp_OAMethod @json, 'StringOf', @SellerSKU OUT, 'payload.OrderItems[i].SellerSKU'
        EXEC sp_OAMethod @json, 'StringOf', @Title OUT, 'payload.OrderItems[i].Title'
        EXEC sp_OAMethod @json, 'IntOf', @QuantityOrdered OUT, 'payload.OrderItems[i].QuantityOrdered'
        EXEC sp_OAMethod @json, 'IntOf', @QuantityShipped OUT, 'payload.OrderItems[i].QuantityShipped'
        EXEC sp_OAMethod @json, 'IntOf', @NumberOfItems OUT, 'payload.OrderItems[i].ProductInfo.NumberOfItems'
        EXEC sp_OAMethod @json, 'StringOf', @CurrencyCode OUT, 'payload.OrderItems[i].ItemPrice.CurrencyCode'
        EXEC sp_OAMethod @json, 'StringOf', @Amount OUT, 'payload.OrderItems[i].ItemPrice.Amount'
        EXEC sp_OAMethod @json, 'StringOf', @ItemTaxCurrencyCode OUT, 'payload.OrderItems[i].ItemTax.CurrencyCode'
        EXEC sp_OAMethod @json, 'StringOf', @ItemTaxAmount OUT, 'payload.OrderItems[i].ItemTax.Amount'
        EXEC sp_OAMethod @json, 'StringOf', @PromotionDiscountCurrencyCode OUT, 'payload.OrderItems[i].PromotionDiscount.CurrencyCode'
        EXEC sp_OAMethod @json, 'StringOf', @PromotionDiscountAmount OUT, 'payload.OrderItems[i].PromotionDiscount.Amount'
        EXEC sp_OAMethod @json, 'BoolOf', @IsGift OUT, 'payload.OrderItems[i].IsGift'
        EXEC sp_OAMethod @json, 'StringOf', @ConditionId OUT, 'payload.OrderItems[i].ConditionId'
        EXEC sp_OAMethod @json, 'StringOf', @ConditionSubtypeId OUT, 'payload.OrderItems[i].ConditionSubtypeId'
        EXEC sp_OAMethod @json, 'BoolOf', @IsTransparency OUT, 'payload.OrderItems[i].IsTransparency'
        EXEC sp_OAMethod @json, 'BoolOf', @SerialNumberRequired OUT, 'payload.OrderItems[i].SerialNumberRequired'
        EXEC sp_OAMethod @json, 'StringOf', @IossNumber OUT, 'payload.OrderItems[i].IossNumber'
        EXEC sp_OAMethod @json, 'StringOf', @DeemedResellerCategory OUT, 'payload.OrderItems[i].DeemedResellerCategory'
        EXEC sp_OAMethod @json, 'StringOf', @StoreChainStoreId OUT, 'payload.OrderItems[i].StoreChainStoreId'
        EXEC sp_OAMethod @json, 'BoolOf', @IsBuyerRequestedCancel OUT, 'payload.OrderItems[i].BuyerRequestedCancel.IsBuyerRequestedCancel'
        EXEC sp_OAMethod @json, 'StringOf', @BuyerCancelReason OUT, 'payload.OrderItems[i].BuyerRequestedCancel.BuyerCancelReason'
        SELECT @i = @i + 1
      END


    PRINT 'Success!'

    EXEC @hr = sp_OADestroy @authAws
    EXEC @hr = sp_OADestroy @jsonLwaToken
    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @sbPath
    EXEC @hr = sp_OADestroy @jsonRc
    EXEC @hr = sp_OADestroy @sbRequest
    EXEC @hr = sp_OADestroy @sbResponse
    EXEC @hr = sp_OADestroy @jsonResp
    EXEC @hr = sp_OADestroy @json


END
GO