Sample code for 30+ languages & platforms
AutoIt

Amazon SP-API Get Specific Order

See more Amazon SP-API Examples

Get a specific Amazon Seller order.

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 information for this 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}",
;        "dataElements": ["buyerInfo", "shippingAddress"]
;      }
;    ]
;  }

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

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

$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}"
$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

ConsoleWrite($oSbResponse.GetAsString() & @CRLF)

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

;  {
;    "payload": {
;      "AmazonOrderId": "902-1845936-5435065",
;      "PurchaseDate": "1970-01-19T03:58:30Z",
;      "LastUpdateDate": "1970-01-19T03:58:32Z",
;      "OrderStatus": "Unshipped",
;      "FulfillmentChannel": "MFN",
;      "SalesChannel": "Amazon.com",
;      "ShipServiceLevel": "Std US D2D Dom",
;      "OrderTotal": {
;        "CurrencyCode": "USD",
;        "Amount": "11.01"
;      },
;      "NumberOfItemsShipped": 0,
;      "NumberOfItemsUnshipped": 1,
;      "PaymentMethod": "Other",
;      "PaymentMethodDetails": [
;        "Standard"
;      ],
;      "IsReplacementOrder": false,
;      "MarketplaceId": "ATVPDKIKX0DER",
;      "ShipmentServiceLevelCategory": "Standard",
;      "OrderType": "StandardOrder",
;      "EarliestShipDate": "1970-01-19T03:59:27Z",
;      "LatestShipDate": "1970-01-19T04:05:13Z",
;      "EarliestDeliveryDate": "1970-01-19T04:06:39Z",
;      "LatestDeliveryDate": "1970-01-19T04:15:17Z",
;      "IsBusinessOrder": false,
;      "IsPrime": false,
;      "IsGlobalExpressEnabled": false,
;      "IsPremiumOrder": false,
;      "IsSoldByAB": false,
;      "IsIBA": false,
;      "DefaultShipFromLocationAddress": {
;        "Name": "MFNIntegrationTestMerchant",
;        "AddressLine1": "2201 WESTLAKE AVE",
;        "City": "SEATTLE",
;        "StateOrRegion": "WA",
;        "PostalCode": "98121-2778",
;        "CountryCode": "US",
;        "Phone": "+1 480-386-0930 ext. 73824",
;        "AddressType": "Commercial"
;      },
;      "FulfillmentInstruction": {
;        "FulfillmentSupplySourceId": "sampleSupplySourceId"
;      },
;      "IsISPU": false,
;      "IsAccessPointOrder": false,
;      "AutomatedShippingSettings": {
;        "HasAutomatedShippingSettings": false
;      },
;      "EasyShipShipmentStatus": "PendingPickUp",
;      "ElectronicInvoiceStatus": "NotRequired"
;    }
;  }

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

$oJson = ObjCreate("Chilkat.JsonObject")

$oJson.LoadSb($oSbResponse)

Local $strVal

Local $sAmazonOrderId = $oJson.StringOf("payload.AmazonOrderId")
Local $sPurchaseDate = $oJson.StringOf("payload.PurchaseDate")
Local $sLastUpdateDate = $oJson.StringOf("payload.LastUpdateDate")
Local $sOrderStatus = $oJson.StringOf("payload.OrderStatus")
Local $sFulfillmentChannel = $oJson.StringOf("payload.FulfillmentChannel")
Local $sSalesChannel = $oJson.StringOf("payload.SalesChannel")
Local $sShipServiceLevel = $oJson.StringOf("payload.ShipServiceLevel")
Local $sCurrencyCode = $oJson.StringOf("payload.OrderTotal.CurrencyCode")
Local $sAmount = $oJson.StringOf("payload.OrderTotal.Amount")
Local $iNumberOfItemsShipped = $oJson.IntOf("payload.NumberOfItemsShipped")
Local $iNumberOfItemsUnshipped = $oJson.IntOf("payload.NumberOfItemsUnshipped")
Local $sPaymentMethod = $oJson.StringOf("payload.PaymentMethod")
Local $bIsReplacementOrder = $oJson.BoolOf("payload.IsReplacementOrder")
Local $sMarketplaceId = $oJson.StringOf("payload.MarketplaceId")
Local $sShipmentServiceLevelCategory = $oJson.StringOf("payload.ShipmentServiceLevelCategory")
Local $sOrderType = $oJson.StringOf("payload.OrderType")
Local $sEarliestShipDate = $oJson.StringOf("payload.EarliestShipDate")
Local $sLatestShipDate = $oJson.StringOf("payload.LatestShipDate")
Local $sEarliestDeliveryDate = $oJson.StringOf("payload.EarliestDeliveryDate")
Local $sLatestDeliveryDate = $oJson.StringOf("payload.LatestDeliveryDate")
Local $bIsBusinessOrder = $oJson.BoolOf("payload.IsBusinessOrder")
Local $bIsPrime = $oJson.BoolOf("payload.IsPrime")
Local $bIsGlobalExpressEnabled = $oJson.BoolOf("payload.IsGlobalExpressEnabled")
Local $bIsPremiumOrder = $oJson.BoolOf("payload.IsPremiumOrder")
Local $bIsSoldByAB = $oJson.BoolOf("payload.IsSoldByAB")
Local $bIsIBA = $oJson.BoolOf("payload.IsIBA")
Local $sName = $oJson.StringOf("payload.DefaultShipFromLocationAddress.Name")
Local $sAddressLine1 = $oJson.StringOf("payload.DefaultShipFromLocationAddress.AddressLine1")
Local $sCity = $oJson.StringOf("payload.DefaultShipFromLocationAddress.City")
Local $sStateOrRegion = $oJson.StringOf("payload.DefaultShipFromLocationAddress.StateOrRegion")
Local $sPostalCode = $oJson.StringOf("payload.DefaultShipFromLocationAddress.PostalCode")
Local $sCountryCode = $oJson.StringOf("payload.DefaultShipFromLocationAddress.CountryCode")
Local $sPhone = $oJson.StringOf("payload.DefaultShipFromLocationAddress.Phone")
Local $sAddressType = $oJson.StringOf("payload.DefaultShipFromLocationAddress.AddressType")
Local $sFulfillmentSupplySourceId = $oJson.StringOf("payload.FulfillmentInstruction.FulfillmentSupplySourceId")
Local $bIsISPU = $oJson.BoolOf("payload.IsISPU")
Local $bIsAccessPointOrder = $oJson.BoolOf("payload.IsAccessPointOrder")
Local $bHasAutomatedShippingSettings = $oJson.BoolOf("payload.AutomatedShippingSettings.HasAutomatedShippingSettings")
Local $sEasyShipShipmentStatus = $oJson.StringOf("payload.EasyShipShipmentStatus")
Local $sElectronicInvoiceStatus = $oJson.StringOf("payload.ElectronicInvoiceStatus")
Local $i = 0
Local $iCount_i = $oJson.SizeOfArray("payload.PaymentMethodDetails")
While $i < $iCount_i
    $oJson.I = $i
    $strVal = $oJson.StringOf("payload.PaymentMethodDetails[i]")
    $i = $i + 1
Wend

ConsoleWrite("Success!" & @CRLF)