top of page

用 Python × SP-API 检索财务信息 | Amazon Selling Partner API

  • 2小时前
  • 讀畢需時 4 分鐘

Selling Partner API(SP-API)是面向Amazon卖家合作伙伴的自动化功能的新一代API套件,是Amazon Marketplace Web Service(MWS)API的演进版本。MWS已提供对Amazon重要功能的程序化访问超过十年。SP-API公开后,将可在目前支持MWS的所有Amazon商店的生产环境中使用。

目前使用MWS的开发者,需要在2024年4月1日之前迁移到SP-API。(截至2023年1月23日的信息)

Selling Partner API for Finances

Finances的API可以获取与卖家业务相关的财务信息。无需等待结算周期结束,即可获取指定订单、财务事件组或日期范围的财务事件。也可以获取指定日期范围内的财务事件组。

环境准备

要在Python中使用SP-API,需要安装python-amazon-sp-api库。我在AWS Lambda中执行Python,因此为了把库安装到Lambda的目标项目中,加上了「-t .」选项

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

执行SP-API需要凭据。把凭据统一存放在credentials中会比较方便。各个值的获取方法将在另一篇文章中介绍。

credentials : {
    "refresh_token": 刷新令牌,
    "lwa_app_id": 客户端ID,
    "lwa_client_secret": 客户端密钥,
    "aws_access_key": AWS访问密钥,
    "aws_secret_key": AWS私有密钥
}

执行API

listFinancialEvents:返回指定日期范围内的财务事件

listFinancialEventsByGroupId:返回指定财务事件组的所有财务事件

listFinancialEventsByOrderId:返回指定订单的所有财务事件

listFinancialEventGroups:返回指定日期范围内的财务事件组

listFinancialEvents

from sp_api.api import Finances
from sp_api.base.marketplaces import Marketplaces
def lambda_handler(event, context):
	#创建用于使用Finances API的对象
	obj = Finances(marketplace=Marketplaces.JP,
	                   credentials=event['credentials'])
	#设置检索所需的值并获取财务事件信息
	result = obj.list_financial_events(PostedAfter='2023-01-01T00:00:00',
	                                  PostedBefore='2023-01-31T23:59:59')
	return result.payload

若已从Finances API获取到NextToken,则参数中只传NextToken即可

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

参数说明

PostedAfter:以该日期时间之后入账的财务事件为对象

PostedBefore:以该日期时间之前入账的财务事件为对象

NextToken:用于获取下一页的令牌

执行结果(示例)

{
	"FinancialEvents": {
		"ShipmentEventList": [
			{
				"AmazonOrderId": "***-*******-*******",
				"SellerOrderId": "***-*******-*******",
				"MarketplaceName": "Amazon.co.jp",
				"PostedDate": "2023-01-01T08:44:40Z",
				"ShipmentItemList": [
					{
						"SellerSKU": "********",
						"OrderItemId": "*************",
						"QuantityShipped": 1,
						"ItemChargeList": [
							{
								"ChargeType": "Principal",
								"ChargeAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": 2000
								}
							},
							{
								"ChargeType": "Tax",
								"ChargeAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": 200
								}
							},
							{
								"ChargeType": "GiftWrap",
								"ChargeAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": 0
								}
							},
							{
								"ChargeType": "GiftWrapTax",
								"ChargeAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": 0
								}
							},
							{
								"ChargeType": "ShippingCharge",
								"ChargeAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": 186
								}
							},
							{
								"ChargeType": "ShippingTax",
								"ChargeAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": 19
								}
							}
						],
						"ItemFeeList": [
							{
								"FeeType": "FBAPerUnitFulfillmentFee",
								"FeeAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": -434
								}
							},
							{
								"FeeType": "Commission",
								"FeeAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": -242
								}
							},
							{
								"FeeType": "FixedClosingFee",
								"FeeAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": 0
								}
							},
							{
								"FeeType": "GiftwrapChargeback",
								"FeeAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": 0
								}
							},
							{
								"FeeType": "ShippingChargeback",
								"FeeAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": 0
								}
							},
							{
								"FeeType": "VariableClosingFee",
								"FeeAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": 0
								}
							}
						],
						"PromotionList": [
							{
								"PromotionType": "PromotionMetaDataDefinitionValue",
								"PromotionId": "JP Core Free Shipping  2016/03/29 6-38-40-593",
								"PromotionAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": 0
								}
							},
							{
								"PromotionType": "PromotionMetaDataDefinitionValue",
								"PromotionId": "JP Core Free Shipping  2016/03/29 6-38-40-593",
								"PromotionAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": 0
								}
							},
							{
								"PromotionType": "PromotionMetaDataDefinitionValue",
								"PromotionId": "JP Core Free Shipping  2016/03/29 6-38-40-593",
								"PromotionAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": 0
								}
							},
							{
								"PromotionType": "PromotionMetaDataDefinitionValue",
								"PromotionId": "JP Core Free Shipping  2016/03/29 6-38-40-593",
								"PromotionAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": 0
								}
							},
							{
								"PromotionType": "PromotionMetaDataDefinitionValue",
								"PromotionId": "JP Core Free Shipping  2016/03/29 6-38-40-593",
								"PromotionAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": -186
								}
							},
							{
								"PromotionType": "PromotionMetaDataDefinitionValue",
								"PromotionId": "JP Core Free Shipping  2016/03/29 6-38-40-593",
								"PromotionAmount": {
									"CurrencyCode": "JPY",
									"CurrencyAmount": -19
								}
							}
						]
					}
				]
			}
		],
		"SellerDealPaymentEventList": [],
		"DebtRecoveryEventList": [],
		"LoanServicingEventList": [],
		"AdjustmentEventList": [
			{
				"AdjustmentType": "PostageBilling_Postage",
				"PostedDate": "2023-01-16T15:54:33Z",
				"AdjustmentAmount": {
					"CurrencyCode": "JPY",
					"CurrencyAmount": -586
				}
			},
			{
				"AdjustmentType": "PostageBilling_DeliveryConfirmation",
				"PostedDate": "2023-01-16T15:54:33Z",
				"AdjustmentAmount": {
					"CurrencyCode": "JPY",
					"CurrencyAmount": 0
				}
			},
			{
				"AdjustmentType": "PostageBilling_VAT",
				"PostedDate": "2023-01-16T15:54:33Z",
				"AdjustmentAmount": {
					"CurrencyCode": "JPY",
					"CurrencyAmount": -59
				}
			},
			{
				"AdjustmentType": "REVERSAL_REIMBURSEMENT",
				"PostedDate": "2023-01-27T04:54:43Z",
				"AdjustmentAmount": {
					"CurrencyCode": "JPY",
					"CurrencyAmount": 12817
				},
				"AdjustmentItemList": [
					{
						"Quantity": "1",
						"PerUnitAmount": {
							"CurrencyCode": "JPY",
							"CurrencyAmount": 12817
						},
						"TotalAmount": {
							"CurrencyCode": "JPY",
							"CurrencyAmount": 12817
						},
						"SellerSKU": "2022RAKU392",
						"ProductDescription": "********************************"
					}
				]
			}
		],
		"SAFETReimbursementEventList": [],
		"SellerReviewEnrollmentPaymentEventList": [],
		"FBALiquidationEventList": [],
		"CouponPaymentEventList": [],
		"ImagingServicesFeeEventList": [],
		"NetworkComminglingTransactionEventList": [],
		"AffordabilityExpenseEventList": [],
		"AffordabilityExpenseReversalEventList": [],
		"RemovalShipmentEventList": [],
		"RemovalShipmentAdjustmentEventList": [],
		"TrialShipmentEventList": [],
		"TDSReimbursementEventList": [],
		"AdhocDisbursementEventList": [],
		"TaxWithholdingEventList": [],
		"ChargeRefundEventList": [],
		"FailedAdhocDisbursementEventList": [],
		"ValueAddedServiceChargeEventList": [],
		"CapacityReservationBillingEventList": []
	}
}

listFinancialEventsByGroupId

from sp_api.api import Finances
from sp_api.base.marketplaces import Marketplaces
def lambda_handler(event, context):
	#创建用于使用Finances API的对象
	obj = Finances(marketplace=Marketplaces.JP,
	                   credentials=event['credentials'])
	#设置检索所需的值并获取财务事件信息
	result = obj.list_financial_events_by_group_id(eventGroupId=event['GroupId'])
	return result.payload

参数说明

eventGroupId:目标财务事件组的ID

listFinancialEventsByOrderId

from sp_api.api import Finances
from sp_api.base.marketplaces import Marketplaces
def lambda_handler(event, context):
	#创建用于使用Finances API的对象
	obj = Finances(marketplace=Marketplaces.JP,
	                   credentials=event['credentials'])
	#设置检索所需的值并获取财务事件信息
	result = obj.list_financial_events_by_order_id(orderId='***-*******-*******')
	return result.payload

参数说明

orderId:目标订单的Amazon订单ID

listFinancialEventGroups

from sp_api.api import Finances
from sp_api.base.marketplaces import Marketplaces
def lambda_handler(event, context):
	#创建用于使用Finances API的对象
	obj = Finances(marketplace=Marketplaces.JP,
	                   credentials=event['credentials'])
	#设置检索所需的值并获取财务事件信息
	result = obj.list_financial_event_groups(FinancialEventGroupStartedAfter='2023-01-01T00:00:00',
	                                  FinancialEventGroupStartedBefore='2023-01-31T23:59:59')
	return result.payload

若已从Finances API获取到NextToken,则参数中只传NextToken即可

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

参数说明

FinancialEventGroupStartedAfter:以该日期时间之后开始的组为对象

FinancialEventGroupStartedBefore:以该日期时间之前开始的组为对象

NextToken:用于获取下一页的令牌

多设置几种参数,让自己能够获取所需的数据吧。

 
 
 

留言


© Copyright ROBIN planning LLC.

bottom of page