top of page

Financial information search with Python x SP-API | Amazon Selling partner API

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

ree

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 Finances

The Finances API allows you to retrieve financial information related to your business. You can retrieve financial events for a specified order, financial event group, or date range without waiting for the end of the line period. You can also get financial event groups for a specified date range.


Environmental preparation

To use SP-API with Python, you need to install the python-amazon-sp-api library. I run python with aws lambda, so I have the option "-t." to install the library in the lambda target project

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": refresh token,
     "lwa_app_id": client id,
     "lwa_client_secret": client secret,
     "aws_access_key": AWS access key,
     "aws_secret_key": AWS secret key
}

API execution

listFinancialEvents: Returns financial events in the specified data range

listFinancialEventsByGroupId: Returns all financial events for the specified financial event group

listFinancialEventsByOrderId: Returns all financial events for a given order

listFinancialEventGroups: Returns financial event groups within the specified date range


listFinancialEvent

from sp_api.api import Finances
from sp_api.base.marketplaces import Marketplaces
def lambda_handler(event, context):
	#Object creation for using FinancesAPI
	obj = Finances(marketplace=Marketplaces.JP,
	                   credentials=event['credentials'])
	#Set values required for search to retrieve financial event information
	result = obj.list_financial_events(PostedAfter='2023-01-01T00:00:00',
	                                  PostedBefore='2023-01-31T23:59:59')
	return result.payload

If you are getting NextToken from Finances API, you can pass only NextToken as a parameter.

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

Parameter description



Execution result (example)

{ "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 Returns all financial events for the specified financial event group.

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

Parameter description


listFinancialEventsByOrderId Returns financial events for the specified order.

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

Parameter description


financialEventGroups Returns financial event groups within the specified date range.

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

If you are getting NextToken from Finances API, you can pass only NextToken as a parameter.

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

Parameter description




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

 
 
 

© Copyright ROBIN planning LLC.

bottom of page