Working example of Catelog List_Items to share #63
brickbuilderbob
started this conversation in
Show and tell
Replies: 2 comments
-
That is indeed very helpful for someone that starts to develop with the SP-API. Thank you very much, highly appreciated! |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thank you, this was very helpful and exactly what I needed |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I thought it would be helpful to post a working example of one of the more common Amazon Seller API calls that I've been working through so far. I'm new to Python and the Amazon Seller APIs, so I thought this would be a helpful starter example for others to follow. I'm also certainly open to feedback on any of the techniques I've used in this example. I feel like my JSON parsing could use some improvement (it seemed very clunky and "hard coded" to get it to work as I wanted it to). I'm sure there are better and more dynamic methods to parse JSON.
Prerequisite: You'll need to set up an Amazon Seller API account and an AWS account to get started. You'll also need to update the credentials.yml file (or use environmental variables as described in the documentation) to set up the API connection. You'll also need to install this library and import it for your Python code to work.
Working example of Catalog List Items API call
#Import external libraries
from sp_api.api import Catalog
from sp_api.base import SellingApiException, Marketplaces
import json
import ast
print('Start API')
#Example working UPC
UPCString = '673419335058'
#Call the Catelog "List_Items" endpoint for the US Marketplace and the sample UPC
Response = Catalog().list_items(MarketplaceId = str('ATVPDKIKX0DER'), UPC = UPCString)
#Print the raw JSON API response
print(Response.payload)
#Convert the JSON response into a Python compliant variant with double quotes instead of single quotes
JSONCompliantString = json.dumps(ast.literal_eval(str(Response.payload)))
#Load the compliant JSON response to a list variable
JSONResponse = json.loads(JSONCompliantString)
#Print out various components of the JSON list variable
print('UPC: ' + UPCString[0])
print('ASIN: ' + JSONResponse['Items'][0]['Identifiers']['MarketplaceASIN']['ASIN'])
print('Part Number: ' + JSONResponse['Items'][0]['AttributeSets'][0]['PartNumber'])
print('Title: ' + JSONResponse['Items'][0]['AttributeSets'][0]['Title'])
print('Price: ' + str(JSONResponse['Items'][0]['AttributeSets'][0]['ListPrice']['Amount']))
print('Release Date: ' + str(JSONResponse['Items'][0]['AttributeSets'][0]['ReleaseDate']))
print('Sales Ranking: ' + str(JSONResponse['Items'][0]['SalesRankings'][0]['Rank']))
print('Product Category: ' + str(JSONResponse['Items'][0]['SalesRankings'][0]['ProductCategoryId']))
Beta Was this translation helpful? Give feedback.
All reactions