Querying the Amazon Product Data API using Python

Querying the Amazon Product Data API using Python

·

6 min read

I've dabbled quite a bit in the Amazon Product Data API, so I wanted to share an example of how to query is using Python3 and the Amazon Product Data API version 5.

You'll need to go through the steps to sign up for a product API account yourself.

Once you have an account, you can begin. Create your pipenv and install the plugin python-amazon-paapi

pipenv --three
pipenv install python-amazon-paapi

Open this folder in vscode. Fun tip, if you're working on Windows, you can simply type

code .

You'll need at least two files for this project. The first is an Amazon credentials file, the second is the script we'll use to query the API.

We'll import the credentials file into the script, that way we can keep them separate.

Your credentials file can be called amazonCred.py and should look like this:

AWS_ACCESS_KEY_ID = 'YOURACCESSKEYID'
AWS_SECRET_ACCESS_KEY = 'yoUrSecReTACcessKeY'
AWS_ASSOCIATE_TAG = 'ATAG-00'

These values can be retrieved when you create your Amazon API account.

Next, create another python script called amazonData.py

First we'll import our values from the amazonCred.py file, as well as our api.

from amazonCred import AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ASSOCIATE_TAG
from amazon.paapi import AmazonAPI

Create a new instance of the AmazonAPI, passing in the values we imported in the format of amazon = AmazonAPI(KEY, SECRET, TAG, COUNTRY)

amazon = AmazonAPI(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ASSOCIATE_TAG, 'US')

The product API in it's simplest form will take an ASIN and query data from it. An ASIN is an identifier that is unique to Amazon. It can be found as part of the URL of the Amazon item, or under the "Product information" portion of the item page. We'll use Avenger's Endgame 4K as an example.

image.png

In our code, we can query this item with the following syntax.

product = amazon.get_product('B07RH665FZ')
print(product.title)

Run the code with pipenv

pipenv run python amazonData.py
AVENGERS: ENDGAME [Blu-ray]

Let's break down this product object.

The type is a parse class from the plugin, which we can view here

Looking through the code, we have several hundred options, including product images in various formats, brand, manufacturer, external IDs like ISBN and UPC and lots more.

Let's see if we can search by UPC, a more common ID used by nearly all forms of media. To do this, we'll use the search_products function of our amazon object. In this case, the UPC for Avengers is 786936863680

upc='786936863680'
product = amazon.search_products(item_count=1, keywords=upc)[0]                
print(product.title)

Same results. Great, but let's output the ASIN and the UPC regardless of how we query it.

print("ASIN: %s, UPC: %s" % (product.asin,product.info.external_ids.upc[0])

Output

ASIN: B07RH665FZ, UPC: 786936863680
AVENGERS: ENDGAME [Blu-ray]

This is good so far, but let's add to the usability. We'll take input from the user and use that as the search format for either the ASIN or the ISBN.

First, get the input from the user.

try:
        id = sys.argv[1]
        print ('Querying for item ' + id)
except:
        print('ERROR: ASIN argument required. USAGE: >python amazonData.py [ABCD1234|012345678910')
        sys.exit(1)

Since we're using the sys library, remember to import that at the top of the file

import sys

Now we'll test the users input. For this, we could use regex, but we'll keep it extra simple and just check the length of the input to determine if it's a UPC or an ASIN. Asin is 10 characters or less, UPCs are typically 12 characters or more.

if len(id) <= 10:
    isAsin = True
elif 11 <= len(id) <= 13:
    isAsin = False
else:
    print('Invalid entry')
    sys.exit(1)

Now test against "isAsin" and determine how the product should be searched.

if isAsin:
    product = amazon.get_product(id)
else:
    product = amazon.search_products(item_count=1, keywords=id)[0]                
print("ASIN: %s, UPC: %s" % (product.asin,product.info.external_ids.upc[0]))
print(product.title)

One last thing... let's grab the price as well. Amazon is a shopping site, after all.

print("ASIN: %s, UPC: %s" % (product.asin,product.info.external_ids.upc[0]))
print("Title: %s is $%s" % (product.title, product.prices.price.value))

The final output:

Querying for item 786936863680
ASIN: B07RH665FZ, UPC: 786936863680
Title: AVENGERS: ENDGAME [Blu-ray] is $15.79

This code could use a lot of checks and balances to make sure users are putting in the proper values and that Amazon is returning what's expected, but I hope this can springboard you into creating a more sophisticated program using the Amazon product data API.

That's all for today.

View this project on Gitlab

-AJ