top of page

Order management information retrieval with Python x SP-API | Amazon Selling partner API

  • robin05156
  • 2023年7月6日
  • 読了時間: 2分

The Selling Partner API (SP-API) is Amazon's next-generation API suite of automation capabilities for Selling Partners and is an evolution of the Amazon Marketplace Web Service (MWS) API. MWS has provided programmatic access to important Amazon functionality for over a decade. Once the SP-API is live, it will be available in production in all Amazon stores that currently support MWS.

Developers currently using MWS must migrate to SP-API by April 1, 2024. (Information as of January 23, 2023)


Selling Partner API for Orders

Order information can be obtained programmatically by using the Orders API. These APIs enable the development of flexible custom applications in areas such as order information management, order status research, and demand-based decision support tools.


Environment Preparation

To use SP-API with Python, you need to install the python-amazon-sp-api library. I run python on aws lambda, so I used -t .< /span> option

pip install python-amazon-sp-api -t .

Credentials are required to run the SP-API. Credentials are conveniently stored together in credentials. How to get each value will be introduced in another article.

credentials : {
    "refresh_token":リフレッシュトークン,
    "lwa_app_id":クライアントID,
    "lwa_client_secret": クライアント秘密情報,
    "aws_access_key":AWSアクセスキー,
    "aws_secret_key":AWSシークレットキー
}

API execution

getOrders Returns information about orders created or updated during the time period indicated by the specified parameters. You can apply various filtering criteria to narrow down the order information returned. If NextToken is present, NextToken will be used to search for order information instead of other conditions.

getOrders Returns the orders indicated by the given order id.

getOrderBuyerInfo Returns buyer information for the specified order.

getOrderAddress Returns the shipping address for the specified order.

getOrderItems Returns detailed order item information for the order identified by the specified order ID. If NextToken is specified, you can get the order items for the next page.

getOrderItemsBuyerInfo Returns buyer information for items ordered in the specified order.


getOrders (Sample)

from sp_api.api import Orders
from sp_api.base.marketplaces import Marketplaces
def lambda_handler(event, context):
	#OrdersAPI利用のためのオブジェクト生成
	obj = Orders(marketplace=Marketplaces.JP,
	                   credentials=event['credentials'])
	#検索に必要な値を設定し財務イベント情報を取得
	result = obj.get_orders(CreatedAfter='2023-01-01T00:00:00',
	                        CreatedBefore='2023-01-31T23:59:59',
	                        FulfillmentChannels=["AFN"],
	                        OrderStatuses=["Shipped,Canceled"])
	return result.payload

If you are getting NextToken from the Orders API, you can pass only NextToken as a parameter

	result = obj.get_orders(NextToken=event['NextToken'])

Parameter description


Execution result (sample)


{
	"Orders": [
		{
			"BuyerInfo": {
				"BuyerEmail": "*****@****.***"
			},
			"AmazonOrderId": "***-******-******",
			"EarliestShipDate": "2023-01-04T14:59:59Z",
			"SalesChannel": "Amazon.co.jp",
			"OrderStatus": "Shipped",
			"NumberOfItemsShipped": 1,
			"OrderType": "StandardOrder",
			"IsPremiumOrder": false,
			"IsPrime": false,
			"FulfillmentChannel": "AFN",
			"NumberOfItemsUnshipped": 0,
			"HasRegulatedItems": false,
			"IsReplacementOrder": "false",
			"IsSoldByAB": false,
			"LatestShipDate": "2023-01-04T14:59:59Z",
			"ShipServiceLevel": "Expedited",
			"IsISPU": false,
			"MarketplaceId": "A1VC38T7YXB528",
			"PurchaseDate": "2023-01-03T07:21:21Z",
			"ShippingAddress": {
				"StateOrRegion": "◯◯県",
				"PostalCode": "000-0000",
				"CountryCode": "JP"
			},
			"IsAccessPointOrder": false,
			"SellerOrderId": "***-******-******",
			"PaymentMethod": "Other",
			"IsBusinessOrder": false,
			"OrderTotal": {
				"CurrencyCode": "JPY",
				"Amount": "2180.00"
			},
			"PaymentMethodDetails": [
				"Standard"
			],
			"IsGlobalExpressEnabled": false,
			"LastUpdateDate": "2023-01-05T09:02:23Z",
			"ShipmentServiceLevelCategory": "Expedited"
		},
		{
			"BuyerInfo": {},
			"AmazonOrderId": "***-******-******",
			"EarliestShipDate": "2023-01-05T14:59:59Z",
			"SalesChannel": "Amazon.co.jp",
			"OrderStatus": "Canceled",
			"NumberOfItemsShipped": 0,
			"OrderType": "StandardOrder",
			"IsPremiumOrder": false,
			"IsPrime": false,
			"FulfillmentChannel": "AFN",
			"NumberOfItemsUnshipped": 0,
			"HasRegulatedItems": false,
			"IsReplacementOrder": "false",
			"IsSoldByAB": false,
			"LatestShipDate": "2023-01-05T14:59:59Z",
			"ShipServiceLevel": "Expedited",
			"IsISPU": false,
			"MarketplaceId": "A1VC38T7YXB528",
			"PurchaseDate": "2023-01-03T11:51:29Z",
			"IsAccessPointOrder": false,
			"SellerOrderId": "***-******-******",
			"PaymentMethod": "Other",
			"IsBusinessOrder": true,
			"PaymentMethodDetails": [
				"Standard"
			],
			"IsGlobalExpressEnabled": false,
			"LastUpdateDate": "2023-01-03T11:53:35Z",
			"ShipmentServiceLevelCategory": "Expedited"
		}
	],
	"CreatedBefore": "2023-01-04T23:59:59Z"
}





Let's set some parameters and get the data we need.

 
 
 

© Copyright ROBIN planning LLC.

bottom of page