Sample code for 30+ languages & platforms
Xbase++

Amazon SP-API Get Specific Order

See more Amazon SP-API Examples

Get a specific Amazon Seller order.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL cOrderId
LOCAL oAuthAws
LOCAL oJsonLwaToken
LOCAL oRest
LOCAL cLwa_token
LOCAL oSbPath
LOCAL oJsonRc
LOCAL oSbRequest
LOCAL oSbResponse
LOCAL cUri
LOCAL nStatusCode
LOCAL oJsonResp
LOCAL cRestrictedDataToken
LOCAL oJson
LOCAL cStrVal
LOCAL cAmazonOrderId
LOCAL cPurchaseDate
LOCAL cLastUpdateDate
LOCAL cOrderStatus
LOCAL cFulfillmentChannel
LOCAL cSalesChannel
LOCAL cShipServiceLevel
LOCAL cCurrencyCode
LOCAL cAmount
LOCAL nNumberOfItemsShipped
LOCAL nNumberOfItemsUnshipped
LOCAL cPaymentMethod
LOCAL nIsReplacementOrder
LOCAL cMarketplaceId
LOCAL cShipmentServiceLevelCategory
LOCAL cOrderType
LOCAL cEarliestShipDate
LOCAL cLatestShipDate
LOCAL cEarliestDeliveryDate
LOCAL cLatestDeliveryDate
LOCAL nIsBusinessOrder
LOCAL nIsPrime
LOCAL nIsGlobalExpressEnabled
LOCAL nIsPremiumOrder
LOCAL nIsSoldByAB
LOCAL nIsIBA
LOCAL cName
LOCAL cAddressLine1
LOCAL cCity
LOCAL cStateOrRegion
LOCAL cPostalCode
LOCAL cCountryCode
LOCAL cPhone
LOCAL cAddressType
LOCAL cFulfillmentSupplySourceId
LOCAL nIsISPU
LOCAL nIsAccessPointOrder
LOCAL nHasAutomatedShippingSettings
LOCAL cEasyShipShipmentStatus
LOCAL cElectronicInvoiceStatus
LOCAL i
LOCAL nCount_i

nSuccess := 0

//  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
cOrderId := "TEST_CASE_200"

oAuthAws := CreateObject("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 := CreateObject("Chilkat.JsonObject")
nSuccess := oJsonLwaToken:LoadFile("qa_data/tokens/sp_api_lwa_token.json")
IF (nSuccess == 0)
    ? "Failed to load LWA access token."
    oAuthAws:destroy()
    oJsonLwaToken:destroy()
    RETURN
ENDIF

//  Must use the non-sandbox domain for getting the RDT.
oRest := CreateObject("Chilkat.Rest")
nSuccess := oRest:Connect("sellingpartnerapi-eu.amazon.com", 443, 1, 1)
IF (nSuccess == 0)
    ? oRest:LastErrorText
    oAuthAws:destroy()
    oJsonLwaToken:destroy()
    oRest:destroy()
    RETURN
ENDIF

nSuccess := oRest:SetAuthAws(oAuthAws)

//  Add the x-amz-access-token request header.
cLwa_token := oJsonLwaToken:StringOf("access_token")
oRest:ClearAllHeaders()
oRest:AddHeader("x-amz-access-token", cLwa_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 := CreateObject("Chilkat.StringBuilder")
oSbPath:Append("/orders/v0/orders/")
oSbPath:Append(cOrderId)

oJsonRc := CreateObject("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 := CreateObject("Chilkat.StringBuilder")
oJsonRc:EmitSb(oSbRequest)

oSbResponse := CreateObject("Chilkat.StringBuilder")
cUri := "/tokens/2021-03-01/restrictedDataToken"
nSuccess := oRest:FullRequestSb("POST", cUri, oSbRequest, oSbResponse)
IF (nSuccess == 0)
    ? oRest:LastErrorText
    oAuthAws:destroy()
    oJsonLwaToken:destroy()
    oRest:destroy()
    oSbPath:destroy()
    oJsonRc:destroy()
    oSbRequest:destroy()
    oSbResponse:destroy()
    RETURN
ENDIF

//  Examine the response status.
nStatusCode := oRest:ResponseStatusCode
IF (nStatusCode != 200)
    ? "Response status code: " + Str(nStatusCode)
    ? "Response status text: " + oRest:ResponseStatusText
    ? "Response body: "
    ? oSbResponse:GetAsString()
    ? "Failed."
    oAuthAws:destroy()
    oJsonLwaToken:destroy()
    oRest:destroy()
    oSbPath:destroy()
    oJsonRc:destroy()
    oSbRequest:destroy()
    oSbResponse:destroy()
    RETURN
ENDIF

//  Get the restricted data token.
oJsonResp := CreateObject("Chilkat.JsonObject")
oJsonResp:LoadSb(oSbResponse)
cRestrictedDataToken := oJsonResp:StringOf("restrictedDataToken")
? "Restricted Data Token: " + cRestrictedDataToken

//  ------------------------------------------------------------------------------------------------------------
//  ------------------------------------------------------------------------------------------------------------
//  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)

nSuccess := oRest:Connect("sandbox.sellingpartnerapi-eu.amazon.com", 443, 1, 1)
IF (nSuccess == 0)
    ? oRest:LastErrorText
    oAuthAws:destroy()
    oJsonLwaToken:destroy()
    oRest:destroy()
    oSbPath:destroy()
    oJsonRc:destroy()
    oSbRequest:destroy()
    oSbResponse:destroy()
    oJsonResp:destroy()
    RETURN
ENDIF

nSuccess := oRest:SetAuthAws(oAuthAws)

oRest:ClearAllHeaders()
oRest:AddHeader("x-amz-access-token", cRestrictedDataToken)

oRest:ClearAllQueryParams()
oRest:AddQueryParam("MarketplaceIds", "ATVPDKIKX0DER")

oRest:ClearAllPathParams()
oRest:AddPathParam("{orderId}", cOrderId)

cUri := "/orders/v0/orders/{orderId}"
nSuccess := oRest:FullRequestNoBodySb("GET", cUri, oSbResponse)
IF (nSuccess == 0)
    ? oRest:LastErrorText
    oAuthAws:destroy()
    oJsonLwaToken:destroy()
    oRest:destroy()
    oSbPath:destroy()
    oJsonRc:destroy()
    oSbRequest:destroy()
    oSbResponse:destroy()
    oJsonResp:destroy()
    RETURN
ENDIF

//  Examine the response status.
nStatusCode := oRest:ResponseStatusCode
IF (nStatusCode != 200)
    ? "Response status text: " + oRest:ResponseStatusText
    ? "Response body: "
    ? oSbResponse:GetAsString()
    ? "Failed."
    oAuthAws:destroy()
    oJsonLwaToken:destroy()
    oRest:destroy()
    oSbPath:destroy()
    oJsonRc:destroy()
    oSbRequest:destroy()
    oSbResponse:destroy()
    oJsonResp:destroy()
    RETURN
ENDIF

? oSbResponse:GetAsString()

//  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 := CreateObject("Chilkat.JsonObject")

oJson:LoadSb(oSbResponse)

cAmazonOrderId := oJson:StringOf("payload.AmazonOrderId")
cPurchaseDate := oJson:StringOf("payload.PurchaseDate")
cLastUpdateDate := oJson:StringOf("payload.LastUpdateDate")
cOrderStatus := oJson:StringOf("payload.OrderStatus")
cFulfillmentChannel := oJson:StringOf("payload.FulfillmentChannel")
cSalesChannel := oJson:StringOf("payload.SalesChannel")
cShipServiceLevel := oJson:StringOf("payload.ShipServiceLevel")
cCurrencyCode := oJson:StringOf("payload.OrderTotal.CurrencyCode")
cAmount := oJson:StringOf("payload.OrderTotal.Amount")
nNumberOfItemsShipped := oJson:IntOf("payload.NumberOfItemsShipped")
nNumberOfItemsUnshipped := oJson:IntOf("payload.NumberOfItemsUnshipped")
cPaymentMethod := oJson:StringOf("payload.PaymentMethod")
nIsReplacementOrder := oJson:BoolOf("payload.IsReplacementOrder")
cMarketplaceId := oJson:StringOf("payload.MarketplaceId")
cShipmentServiceLevelCategory := oJson:StringOf("payload.ShipmentServiceLevelCategory")
cOrderType := oJson:StringOf("payload.OrderType")
cEarliestShipDate := oJson:StringOf("payload.EarliestShipDate")
cLatestShipDate := oJson:StringOf("payload.LatestShipDate")
cEarliestDeliveryDate := oJson:StringOf("payload.EarliestDeliveryDate")
cLatestDeliveryDate := oJson:StringOf("payload.LatestDeliveryDate")
nIsBusinessOrder := oJson:BoolOf("payload.IsBusinessOrder")
nIsPrime := oJson:BoolOf("payload.IsPrime")
nIsGlobalExpressEnabled := oJson:BoolOf("payload.IsGlobalExpressEnabled")
nIsPremiumOrder := oJson:BoolOf("payload.IsPremiumOrder")
nIsSoldByAB := oJson:BoolOf("payload.IsSoldByAB")
nIsIBA := oJson:BoolOf("payload.IsIBA")
cName := oJson:StringOf("payload.DefaultShipFromLocationAddress.Name")
cAddressLine1 := oJson:StringOf("payload.DefaultShipFromLocationAddress.AddressLine1")
cCity := oJson:StringOf("payload.DefaultShipFromLocationAddress.City")
cStateOrRegion := oJson:StringOf("payload.DefaultShipFromLocationAddress.StateOrRegion")
cPostalCode := oJson:StringOf("payload.DefaultShipFromLocationAddress.PostalCode")
cCountryCode := oJson:StringOf("payload.DefaultShipFromLocationAddress.CountryCode")
cPhone := oJson:StringOf("payload.DefaultShipFromLocationAddress.Phone")
cAddressType := oJson:StringOf("payload.DefaultShipFromLocationAddress.AddressType")
cFulfillmentSupplySourceId := oJson:StringOf("payload.FulfillmentInstruction.FulfillmentSupplySourceId")
nIsISPU := oJson:BoolOf("payload.IsISPU")
nIsAccessPointOrder := oJson:BoolOf("payload.IsAccessPointOrder")
nHasAutomatedShippingSettings := oJson:BoolOf("payload.AutomatedShippingSettings.HasAutomatedShippingSettings")
cEasyShipShipmentStatus := oJson:StringOf("payload.EasyShipShipmentStatus")
cElectronicInvoiceStatus := oJson:StringOf("payload.ElectronicInvoiceStatus")
i := 0
nCount_i := oJson:SizeOfArray("payload.PaymentMethodDetails")
DO WHILE i < nCount_i
    oJson:I := i
    cStrVal := oJson:StringOf("payload.PaymentMethodDetails[i]")
    i := i + 1
ENDDO

? "Success!"

oAuthAws:destroy()
oJsonLwaToken:destroy()
oRest:destroy()
oSbPath:destroy()
oJsonRc:destroy()
oSbRequest:destroy()
oSbResponse:destroy()
oJsonResp:destroy()
oJson:destroy()