top of page

Python × SP-API로 재무 정보 검색 | Amazon Selling Partner API

  • 1일 전
  • 4분 분량

셀링 파트너 API(SP-API)는 Amazon의 셀링 파트너를 위한 자동화 기능의 차세대 API 스위트로, Amazon 마켓플레이스 웹 서비스(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: 지정된 날짜 범위 내의 재무 이벤트 그룹을 반환합니다

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만 넘겨주면 OK

	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만 넘겨주면 OK

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

파라미터 설명

FinancialEventGroupStartedAfter: 이 일시 이후에 시작된 그룹을 대상으로 함

FinancialEventGroupStartedBefore: 이 일시 이전에 시작된 그룹을 대상으로 함

NextToken: 다음 페이지를 가져오기 위한 토큰

파라미터를 여러 가지로 설정해, 필요한 데이터를 가져올 수 있게 됩시다.

 
 
 

댓글


© Copyright ROBIN planning LLC.

bottom of page