Sample code for 30+ languages & platforms
Lua

Amazon SP-API Get Orders

See more Amazon SP-API Examples

Demonstrates Amazon SP-API Sellers API -- get orders. Returns orders created or updated during the time frame indicated by the specified parameters.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

-- IMPORTANT:
-- You'll need to make changes to use values specific to your own region and account.
--
-- Here is a list of values you'll need to change or provide:
--
-- 1) The access key. Change AWS_ACCESS_KEY to your actual access key.
-- 2) The secret key. Change AWS_SECRET_KEY to your actual access key.
-- 3) Your region.  Change "ew-west-1" to your region.
-- 4) The domain to which you connect.  Change sandbox.sellingpartnerapi-eu.amazon.com to non-sandbox by removing the "sandbox."
--    and change the "-eu" part of the domain to your region. See https://developer-docs.amazon.com/sp-api/docs/sp-api-endpoints
-- 5) Use the restricted data token (RDT) obtained from your code that previously obtained the RDT.
--     If you saved the RDT to a file, update the relative file path "qa_data/tokens/sp_api_rdt_token.json" to your actual file path.
-- 6) Update your Marketplace ID(s), such as ATVPDKIKX0DER
-- 7) If using the non-sandbox, change the CreatedAfter from "TEST_CASE_200" to an actual date, such as "2022-12-25"

local authAws = chilkat.AuthAws.new()
authAws.access_key = "AWS_ACCESS_KEY"
authAws.secret_key = "AWS_SECRET_KEY"
authAws.service_name = "execute-api"
-- Use the region that is correct for your needs.
authAws.region = "eu-west-1"

local rest = chilkat.Rest.new()
local bTls = true
local port = 443
local bAutoReconnect = true
-- Make sure to use the correct domain.
-- This example is using the sandbox
local success = rest:connect("sandbox.sellingpartnerapi-eu.amazon.com", port, bTls, bAutoReconnect)
if not success then
    print(rest.last_error_text)
    return
end

success = rest:set_auth_aws(authAws)

-- Here's the example to get the RDT access token: Get SP-API Restricted Data Token

-- Load the previously obtained RDT access token.
-- See Fetch SP-API RDT Access Token
local jsonToken = chilkat.JsonObject.new()
success = jsonToken:load_file("qa_data/tokens/sp_api_rdt_token.json")
if not success then
    print("Failed to load RDT access token.")
    return
end

-- Add the x-amz-access-token request header.
local rdt_token = jsonToken:string_of("restrictedDataToken")
rest:clear_all_headers()
rest:add_header("x-amz-access-token", rdt_token)

-- Indicate the marketplace IDs.  Use the marketplace ID for your needs. See https://developer-docs.amazon.com/sp-api/docs/marketplace-ids
rest:clear_all_query_params()
-- When using the sandbox, use these params literally and exactly has shown here:
rest:add_query_param("MarketplaceIds", "ATVPDKIKX0DER")
rest:add_query_param("CreatedAfter", "TEST_CASE_200")

local sbResponse = chilkat.StringBuilder.new()
local uri = "/orders/v0/orders"
success = rest:full_request_no_body_sb("GET", uri, sbResponse)
if not success then
    print(rest.last_error_text)
    return
end

-- Examine the response status.
local statusCode = rest.response_status_code
if statusCode ~= 200 then
    print("Response status text: " .. rest.response_status_text)
    print("Response body: ")
    print(sbResponse:get_as_string())
    print("Failed.")
    return
end

print(sbResponse:get_as_string())

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

-- {
--   "payload": {
--     "CreatedBefore": "1.569521782042E9",
--     "Orders": [
--       {
--         "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"
--       },
--       {
--         "AmazonOrderId": "902-8745147-1934268",
--         "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,
--         "IsAccessPointOrder": false,
--         "IsGlobalExpressEnabled": false,
--         "IsPremiumOrder": false,
--         "IsSoldByAB": false,
--         "IsIBA": false,
--         "EasyShipShipmentStatus": "PendingPickUp",
--         "ElectronicInvoiceStatus": "NotRequired"
--       }
--     ]
--   }
-- }

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

local json = chilkat.JsonObject.new()

json:load_sb(sbResponse)

local AmazonOrderId = ""
local PurchaseDate = ""
local LastUpdateDate = ""
local OrderStatus = ""
local FulfillmentChannel = ""
local SalesChannel = ""
local ShipServiceLevel = ""
local CurrencyCode = ""
local Amount = ""
local NumberOfItemsShipped = 0
local NumberOfItemsUnshipped = 0
local PaymentMethod = ""
local IsReplacementOrder = false
local MarketplaceId = ""
local ShipmentServiceLevelCategory = ""
local OrderType = ""
local EarliestShipDate = ""
local LatestShipDate = ""
local EarliestDeliveryDate = ""
local LatestDeliveryDate = ""
local IsBusinessOrder = false
local IsPrime = false
local IsGlobalExpressEnabled = false
local IsPremiumOrder = false
local IsSoldByAB = false
local IsIBA = false
local Name = ""
local AddressLine1 = ""
local City = ""
local StateOrRegion = ""
local PostalCode = ""
local CountryCode = ""
local Phone = ""
local AddressType = ""
local FulfillmentSupplySourceId = ""
local IsISPU = false
local IsAccessPointOrder = false
local HasAutomatedShippingSettings = false
local EasyShipShipmentStatus = ""
local ElectronicInvoiceStatus = ""
local j = 0
local count_j = 0
local strVal = ""

local CreatedBefore = json:string_of("payload.CreatedBefore")
local i = 0
local count_i = json:size_of_array("payload.Orders")
while i < count_i do
    json.i = i
    AmazonOrderId = json:string_of("payload.Orders[i].AmazonOrderId")
    PurchaseDate = json:string_of("payload.Orders[i].PurchaseDate")
    LastUpdateDate = json:string_of("payload.Orders[i].LastUpdateDate")
    OrderStatus = json:string_of("payload.Orders[i].OrderStatus")
    FulfillmentChannel = json:string_of("payload.Orders[i].FulfillmentChannel")
    SalesChannel = json:string_of("payload.Orders[i].SalesChannel")
    ShipServiceLevel = json:string_of("payload.Orders[i].ShipServiceLevel")
    CurrencyCode = json:string_of("payload.Orders[i].OrderTotal.CurrencyCode")
    Amount = json:string_of("payload.Orders[i].OrderTotal.Amount")
    NumberOfItemsShipped = json:int_of("payload.Orders[i].NumberOfItemsShipped")
    NumberOfItemsUnshipped = json:int_of("payload.Orders[i].NumberOfItemsUnshipped")
    PaymentMethod = json:string_of("payload.Orders[i].PaymentMethod")
    IsReplacementOrder = json:bool_of("payload.Orders[i].IsReplacementOrder")
    MarketplaceId = json:string_of("payload.Orders[i].MarketplaceId")
    ShipmentServiceLevelCategory = json:string_of("payload.Orders[i].ShipmentServiceLevelCategory")
    OrderType = json:string_of("payload.Orders[i].OrderType")
    EarliestShipDate = json:string_of("payload.Orders[i].EarliestShipDate")
    LatestShipDate = json:string_of("payload.Orders[i].LatestShipDate")
    EarliestDeliveryDate = json:string_of("payload.Orders[i].EarliestDeliveryDate")
    LatestDeliveryDate = json:string_of("payload.Orders[i].LatestDeliveryDate")
    IsBusinessOrder = json:bool_of("payload.Orders[i].IsBusinessOrder")
    IsPrime = json:bool_of("payload.Orders[i].IsPrime")
    IsGlobalExpressEnabled = json:bool_of("payload.Orders[i].IsGlobalExpressEnabled")
    IsPremiumOrder = json:bool_of("payload.Orders[i].IsPremiumOrder")
    IsSoldByAB = json:bool_of("payload.Orders[i].IsSoldByAB")
    IsIBA = json:bool_of("payload.Orders[i].IsIBA")
    Name = json:string_of("payload.Orders[i].DefaultShipFromLocationAddress.Name")
    AddressLine1 = json:string_of("payload.Orders[i].DefaultShipFromLocationAddress.AddressLine1")
    City = json:string_of("payload.Orders[i].DefaultShipFromLocationAddress.City")
    StateOrRegion = json:string_of("payload.Orders[i].DefaultShipFromLocationAddress.StateOrRegion")
    PostalCode = json:string_of("payload.Orders[i].DefaultShipFromLocationAddress.PostalCode")
    CountryCode = json:string_of("payload.Orders[i].DefaultShipFromLocationAddress.CountryCode")
    Phone = json:string_of("payload.Orders[i].DefaultShipFromLocationAddress.Phone")
    AddressType = json:string_of("payload.Orders[i].DefaultShipFromLocationAddress.AddressType")
    FulfillmentSupplySourceId = json:string_of("payload.Orders[i].FulfillmentInstruction.FulfillmentSupplySourceId")
    IsISPU = json:bool_of("payload.Orders[i].IsISPU")
    IsAccessPointOrder = json:bool_of("payload.Orders[i].IsAccessPointOrder")
    HasAutomatedShippingSettings = json:bool_of("payload.Orders[i].AutomatedShippingSettings.HasAutomatedShippingSettings")
    EasyShipShipmentStatus = json:string_of("payload.Orders[i].EasyShipShipmentStatus")
    ElectronicInvoiceStatus = json:string_of("payload.Orders[i].ElectronicInvoiceStatus")
    j = 0
    count_j = json:size_of_array("payload.Orders[i].PaymentMethodDetails")
    while j < count_j do
        json.j = j
        strVal = json:string_of("payload.Orders[i].PaymentMethodDetails[j]")
        j = j + 1
    end

    i = i + 1
end

print("Success!")