Sample code for 30+ languages & platforms
AutoIt

Amazon SP-API Get Order Items

See more Amazon SP-API Examples

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

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  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
Local $sOrderId = "TEST_CASE_200"

$oAuthAws = ObjCreate("Chilkat.AuthAws")
$oAuthAws.AccessKey = "AWS_ACCESS_KEY"
$oAuthAws.SecretKey = "AWS_SECRET_KEY"
$oAuthAws.ServiceName = "execute-api"
;  Use the region that is correct for your needs.
$oAuthAws.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
$oJsonLwaToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oJsonLwaToken.LoadFile("qa_data/tokens/sp_api_lwa_token.json")
If ($bSuccess = False) Then
    ConsoleWrite("Failed to load LWA access token." & @CRLF)
    Exit
EndIf

;  Must use the non-sandbox domain for getting the RDT.
$oRest = ObjCreate("Chilkat.Rest")
$bSuccess = $oRest.Connect("sellingpartnerapi-eu.amazon.com",443,True,True)
If ($bSuccess = False) Then
    ConsoleWrite($oRest.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oRest.SetAuthAws($oAuthAws)

;  Add the x-amz-access-token request header.
Local $sLwa_token = $oJsonLwaToken.StringOf("access_token")
$oRest.ClearAllHeaders()
$oRest.AddHeader("x-amz-access-token",$sLwa_token)

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

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

$oSbPath = ObjCreate("Chilkat.StringBuilder")
$oSbPath.Append("/orders/v0/orders/")
$oSbPath.Append($sOrderId)
$oSbPath.Append("/orderItems")

$oJsonRc = ObjCreate("Chilkat.JsonObject")
$oJsonRc.UpdateString("restrictedResources[0].method","GET")
$oJsonRc.UpdateString("restrictedResources[0].path",$oSbPath.GetAsString())
$oJsonRc.UpdateString("restrictedResources[0].dataElements[0]","buyerInfo")

$oSbRequest = ObjCreate("Chilkat.StringBuilder")
$oJsonRc.EmitSb($oSbRequest)

$oSbResponse = ObjCreate("Chilkat.StringBuilder")
Local $sUri = "/tokens/2021-03-01/restrictedDataToken"
$bSuccess = $oRest.FullRequestSb("POST",$sUri,$oSbRequest,$oSbResponse)
If ($bSuccess = False) Then
    ConsoleWrite($oRest.LastErrorText & @CRLF)
    Exit
EndIf

;  Examine the response status.
Local $iStatusCode = $oRest.ResponseStatusCode
If ($iStatusCode <> 200) Then
    ConsoleWrite("Response status code: " & $iStatusCode & @CRLF)
    ConsoleWrite("Response status text: " & $oRest.ResponseStatusText & @CRLF)
    ConsoleWrite("Response body: " & @CRLF)
    ConsoleWrite($oSbResponse.GetAsString() & @CRLF)
    ConsoleWrite("Failed." & @CRLF)
    Exit
EndIf

;  Get the restricted data token.
$oJsonResp = ObjCreate("Chilkat.JsonObject")
$oJsonResp.LoadSb($oSbResponse)
Local $sRestrictedDataToken = $oJsonResp.StringOf("restrictedDataToken")
ConsoleWrite("Restricted Data Token: " & $sRestrictedDataToken & @CRLF)

;  ------------------------------------------------------------------------------------------------------------
;  ------------------------------------------------------------------------------------------------------------
;  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.)
$oRest.Disconnect(100)

$bSuccess = $oRest.Connect("sandbox.sellingpartnerapi-eu.amazon.com",443,True,True)
If ($bSuccess = False) Then
    ConsoleWrite($oRest.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oRest.SetAuthAws($oAuthAws)

$oRest.ClearAllHeaders()
$oRest.AddHeader("x-amz-access-token",$sRestrictedDataToken)

$oRest.ClearAllQueryParams()
$oRest.AddQueryParam("MarketplaceIds","ATVPDKIKX0DER")

$oRest.ClearAllPathParams()
$oRest.AddPathParam("{orderId}",$sOrderId)

$sUri = "/orders/v0/orders/{orderId}/orderItems"
$bSuccess = $oRest.FullRequestNoBodySb("GET",$sUri,$oSbResponse)
If ($bSuccess = False) Then
    ConsoleWrite($oRest.LastErrorText & @CRLF)
    Exit
EndIf

;  Examine the response status.
$iStatusCode = $oRest.ResponseStatusCode
If ($iStatusCode <> 200) Then
    ConsoleWrite("Response status text: " & $oRest.ResponseStatusText & @CRLF)
    ConsoleWrite("Response body: " & @CRLF)
    ConsoleWrite($oSbResponse.GetAsString() & @CRLF)
    ConsoleWrite("Failed." & @CRLF)
    Exit
EndIf

;  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

$oJson = ObjCreate("Chilkat.JsonObject")

$oJson.LoadSb($oSbResponse)
$oJson.EmitCompact = False
ConsoleWrite($oJson.Emit() & @CRLF)

Local $sASIN
Local $sOrderItemId
Local $sSellerSKU
Local $sTitle
Local $iQuantityOrdered
Local $iQuantityShipped
Local $iNumberOfItems
Local $sCurrencyCode
Local $sAmount
Local $sItemTaxCurrencyCode
Local $sItemTaxAmount
Local $sPromotionDiscountCurrencyCode
Local $sPromotionDiscountAmount
Local $bIsGift
Local $sConditionId
Local $sConditionSubtypeId
Local $bIsTransparency
Local $bSerialNumberRequired
Local $sIossNumber
Local $sDeemedResellerCategory
Local $sStoreChainStoreId
Local $bIsBuyerRequestedCancel
Local $sBuyerCancelReason

Local $sAmazonOrderId = $oJson.StringOf("payload.AmazonOrderId")
Local $i = 0
Local $iCount_i = $oJson.SizeOfArray("payload.OrderItems")
While $i < $iCount_i
    $oJson.I = $i
    $sASIN = $oJson.StringOf("payload.OrderItems[i].ASIN")
    $sOrderItemId = $oJson.StringOf("payload.OrderItems[i].OrderItemId")
    $sSellerSKU = $oJson.StringOf("payload.OrderItems[i].SellerSKU")
    $sTitle = $oJson.StringOf("payload.OrderItems[i].Title")
    $iQuantityOrdered = $oJson.IntOf("payload.OrderItems[i].QuantityOrdered")
    $iQuantityShipped = $oJson.IntOf("payload.OrderItems[i].QuantityShipped")
    $iNumberOfItems = $oJson.IntOf("payload.OrderItems[i].ProductInfo.NumberOfItems")
    $sCurrencyCode = $oJson.StringOf("payload.OrderItems[i].ItemPrice.CurrencyCode")
    $sAmount = $oJson.StringOf("payload.OrderItems[i].ItemPrice.Amount")
    $sItemTaxCurrencyCode = $oJson.StringOf("payload.OrderItems[i].ItemTax.CurrencyCode")
    $sItemTaxAmount = $oJson.StringOf("payload.OrderItems[i].ItemTax.Amount")
    $sPromotionDiscountCurrencyCode = $oJson.StringOf("payload.OrderItems[i].PromotionDiscount.CurrencyCode")
    $sPromotionDiscountAmount = $oJson.StringOf("payload.OrderItems[i].PromotionDiscount.Amount")
    $bIsGift = $oJson.BoolOf("payload.OrderItems[i].IsGift")
    $sConditionId = $oJson.StringOf("payload.OrderItems[i].ConditionId")
    $sConditionSubtypeId = $oJson.StringOf("payload.OrderItems[i].ConditionSubtypeId")
    $bIsTransparency = $oJson.BoolOf("payload.OrderItems[i].IsTransparency")
    $bSerialNumberRequired = $oJson.BoolOf("payload.OrderItems[i].SerialNumberRequired")
    $sIossNumber = $oJson.StringOf("payload.OrderItems[i].IossNumber")
    $sDeemedResellerCategory = $oJson.StringOf("payload.OrderItems[i].DeemedResellerCategory")
    $sStoreChainStoreId = $oJson.StringOf("payload.OrderItems[i].StoreChainStoreId")
    $bIsBuyerRequestedCancel = $oJson.BoolOf("payload.OrderItems[i].BuyerRequestedCancel.IsBuyerRequestedCancel")
    $sBuyerCancelReason = $oJson.StringOf("payload.OrderItems[i].BuyerRequestedCancel.BuyerCancelReason")
    $i = $i + 1
Wend

ConsoleWrite("Success!" & @CRLF)