
セリングパートナーAPI(SP-API)は、Amazonのセリングパートナー向けの自動化機能の次世代APIスイートで、AmazonマーケットプレイスWebサービス(MWS)APIが進化したものです。MWSはAmazonの重要な機能へのプログラムによるアクセスを10年以上提供してきました。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 指定された日付範囲内の財務イベントグループを返します
listFinancialEvent
from sp_api.api import Finances
from sp_api.base.marketplaces import Marketplaces
def lambda_handler(event, context):
#FinancesAPI利用のためのオブジェクト生成
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だけを渡してあげればOK
result = obj.list_financial_events(NextToken=event['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):
#FinancesAPI利用のためのオブジェクト生成
obj = Finances(marketplace=Marketplaces.JP,
credentials=event['credentials'])
#検索に必要な値を設定し財務イベント情報を取得
result = obj.list_financial_events_by_group_id(eventGroupId=event['GroupId'])
return result.payload
パラメータの説明
listFinancialEventsByOrderId 指定された注文に関する金融イベントを返します。
from sp_api.api import Finances
from sp_api.base.marketplaces import Marketplaces
def lambda_handler(event, context):
#FinancesAPI利用のためのオブジェクト生成
obj = Finances(marketplace=Marketplaces.JP,
credentials=event['credentials'])
#検索に必要な値を設定し財務イベント情報を取得
result = obj.list_financial_events_by_order_id(orderId='***-*******-*******')
return result.payload
パラメータの説明
financialEventGroups 指定された日付範囲内の財務イベントグループを返します。
from sp_api.api import Finances
from sp_api.base.marketplaces import Marketplaces
def lambda_handler(event, context):
#FinancesAPI利用のためのオブジェクト生成
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だけを渡してあげればOK
result = obj.list_financial_event_groups(NextToken=event['NextToken'])
パラメータの説明
パラメータをいろいろ設定し、必要なデータを取得できるようになりましょう。
Comments