python-amazon-product-api logo

Previous topic

Basic usage

Next topic

Use your own XML parsing library

This Page

Operations

class amazonproduct.API(access_key_id, secret_access_key, locale, associate_tag=None, processor=None)

Wrapper class for the Amazon Product Advertising API. You will need both an AWS access key id and the secret counterpart.

Example:

AWS_KEY = '...'
SECRET_KEY = '...'

api = ProductAdvertisingAPI(AWS_KEY, SECRET_KEY, 'us')
root = api.item_lookup('9783836214063', IdType='ISBN',
            SearchIndex='Books', ResponseGroup='Reviews', ReviewPage=1)

rating = root.Items.Item.CustomerReviews.AverageRating.pyval
total_reviews = root.Items.Item.CustomerReviews.TotalReviews.pyval
review_pages = root.Items.Item.CustomerReviews.TotalReviewPages.pyval

It is possible to use a different module for parsing the XML response. For instance, you can use xml.minidom instead of lxml by defining a custom result processor:

def minidom_response_parser(fp):
    root = parse(fp)
    # parse errors
    for error in root.getElementsByTagName('Error'):
        code = error.getElementsByTagName('Code')[0].firstChild.nodeValue
        msg = error.getElementsByTagName('Message')[0].firstChild.nodeValue
        raise AWSError(code, msg)
    return root
api = API(AWS_KEY, SECRET_KEY, processor=minidom_response_parser)
root = api.item_lookup('0718155157')
print root.toprettyxml()
# ...

Just make sure it raises an AWSError with the appropriate error code and message. For a more complex example, have a look at the default LxmlObjectifyResponseProcessor class.

browse_node_lookup(browse_node_id, response_group=None, **params)

Given a browse node ID, BrowseNodeLookup returns the specified browse node’s name, children, and ancestors. The names and browse node IDs of the children and ancestor browse nodes are also returned. BrowseNodeLookup enables you to traverse the browse node hierarchy to find a browse node.

As you traverse down the hierarchy, you refine your search and limit the number of items returned. For example, you might traverse the following hierarchy: DVD>Used DVDs>Kids and Family, to select out of all the DVDs offered by Amazon only those that are appropriate for family viewing. Returning the items associated with Kids and Family produces a much more targeted result than a search based at the level of Used DVDs.

Alternatively, by traversing up the browse node tree, you can determine the root category of an item. You might do that, for example, to return the top seller of the root product category using the TopSeller response group in an ItemSearch request.

You can use BrowseNodeLookup iteratively to navigate through the browse node hierarchy to reach the node that most appropriately suits your search. Then you can use the browse node ID in an ItemSearch request. This response would be far more targeted than, for example, searching through all of the browse nodes in a search index.

Parameters:
  • browse_node_id (str) – A positive integer assigned by Amazon that uniquely identifies a product category. Default: None Valid Values: A positive integer.
  • response_group – Specifies the types of values to return. You can specify multiple response groups in one request by separating them with commas. Default: BrowseNodeInfo Valid Values: MostGifted, NewReleases, MostWishedFor, TopSellers
call(**qargs)

Builds a signed URL for the operation, fetches the result from Amazon and parses the XML. If you want to customise things at any stage, simply override the respective method(s):

  • _build_url(**query_parameters)
  • _fetch(url)
  • _parse(fp)
cart_add(cart_id, hmac, items, **params)

The CartAdd operation enables you to add items to an existing remote shopping cart. CartAdd can only be used to place a new item in a shopping cart. It cannot be used to increase the quantity of an item already in the cart. If you would like to increase the quantity of an item that is already in the cart, you must use the CartModify operation.

You add an item to a cart by specifying the item’s OfferListingId, or ASIN and ListItemId. Once in a cart, an item can only be identified by its CartItemId. That is, an item in a cart cannot be accessed by its ASIN or OfferListingId. CartItemId is returned by CartCreate, CartGet, and CartAdd.

To add items to a cart, you must specify the cart using the CartId and HMAC values, which are returned by the CartCreate operation.

If the associated CartCreate request specified an AssociateTag, all CartAdd requests must also include a value for Associate Tag otherwise the request will fail.

Note

Some manufacturers have a minimum advertised price (MAP) that can be displayed on Amazon’s retail web site. In these cases, when performing a Cart operation, the MAP Is returned instead of the actual price. The only way to see the actual price is to add the item to a remote shopping cart and follow the PurchaseURL. The actual price will be the MAP or lower.

cart_clear(cart_id, hmac, **params)

The CartClear operation enables you to remove all of the items in a remote shopping cart, including SavedForLater items. To remove only some of the items in a cart or to reduce the quantity of one or more items, use CartModify.

To delete all of the items from a remote shopping cart, you must specify the cart using the CartId and HMAC values, which are returned by the CartCreate operation. A value similar to the HMAC, URLEncodedHMAC, is also returned. This value is the URL encoded version of the HMAC. This encoding is necessary because some characters, such as + and /, cannot be included in a URL. Rather than encoding the HMAC yourself, use the U``RLEncodedHMAC`` value for the HMAC parameter.

CartClear does not work after the customer has used the PurchaseURL to either purchase the items or merge them with the items in their Amazon cart.

Carts exist even though they have been emptied. The lifespan of a cart is 7 days since the last time it was acted upon. For example, if a cart created 6 days ago is modified, the cart lifespan is reset to 7 days.

cart_create(items, **params)

The CartCreate operation enables you to create a remote shopping cart. A shopping cart is the metaphor used by most e-commerce solutions. It is a temporary data storage structure that resides on Amazon servers. The structure contains the items a customer wants to buy. In Product Advertising API, the shopping cart is considered remote because it is hosted by Amazon servers. In this way, the cart is remote to the vendor’s web site where the customer views and selects the items they want to purchase.

Once you add an item to a cart by specifying the item’s ListItemId and ASIN, or OfferListing ID, the item is assigned a CartItemId and accessible only by that value. That is, in subsequent requests, an item in a cart cannot be accessed by its ListItemId and ASIN, or OfferListingId. CartItemId is returned by CartCreate, CartGet, and CartAdd.

Because the contents of a cart can change for different reasons, such as item availability, you should not keep a copy of a cart locally. Instead, use the other cart operations to modify the cart contents. For example, to retrieve contents of the cart, which are represented by CartItemIds, use CartGet.

Available products are added as cart items. Unavailable items, for example, items out of stock, discontinued, or future releases, are added as SaveForLaterItems. No error is generated. The Amazon database changes regularly. You may find a product with an offer listing ID but by the time the item is added to the cart the product is no longer available. The checkout page in the Order Pipeline clearly lists items that are available and those that are SaveForLaterItems.

It is impossible to create an empty shopping cart. You have to add at least one item to a shopping cart using a single CartCreate request. You can add specific quantities (up to 999) of each item.

CartCreate can be used only once in the life cycle of a cart. To modify the contents of the cart, use one of the other cart operations.

Carts cannot be deleted. They expire automatically after being unused for 7 days. The lifespan of a cart restarts, however, every time a cart is modified. In this way, a cart can last for more than 7 days. If, for example, on day 6, the customer modifies a cart, the 7 day countdown starts over.

cart_get(cart_id, hmac, **params)

The CartGet operation enables you to retrieve the IDs, quantities, and prices of all of the items, including SavedForLater items in a remote shopping cart.

Because the contents of a cart can change for different reasons, such as availability, you should not keep a copy of a cart locally. Instead, use CartGet to retrieve the items in a remote shopping cart.

To retrieve the items in a cart, you must specify the cart using the CartId and HMAC values, which are returned in the CartCreate operation. A value similar to HMAC, URLEncodedHMAC, is also returned. This value is the URL encoded version of the HMAC. This encoding is necessary because some characters, such as + and /, cannot be included in a URL. Rather than encoding the HMAC yourself, use the URLEncodedHMAC value for the HMAC parameter.

CartGet does not work after the customer has used the PurchaseURL to either purchase the items or merge them with the items in their Amazon cart.

If the associated CartCreate request specified an AssociateTag, all CartGet requests must also include a value for AssociateTag otherwise the request will fail.

cart_modify(cart_id, hmac, item_ids, **params)

The CartModify operation enables you to change the quantity of items that are already in a remote shopping cart and move items from the active area of a cart to the SaveForLater area or the reverse.

To modify the number of items in a cart, you must specify the cart using the CartId and HMAC values that are returned in the CartCreate operation. A value similar to HMAC, URLEncodedHMAC, is also returned. This value is the URL encoded version of the HMAC. This encoding is necessary because some characters, such as + and /, cannot be included in a URL. Rather than encoding the HMAC yourself, use the URLEncodedHMAC value for the HMAC parameter.

You can use CartModify to modify the number of items in a remote shopping cart by setting the value of the Quantity parameter appropriately. You can eliminate an item from a cart by setting the value of the Quantity parameter to zero. Or, you can double the number of a particular item in the cart by doubling its Quantity . You cannot, however, use CartModify to add new items to a cart.

If the associated CartCreate request specified an AssociateTag, all CartModify requests must also include a value for Associate Tag otherwise the request will fail.

customer_content_lookup(*args, **kwargs)

Some operations are deprecated and will be answered with HTTP 410. To avoid unnecessary API calls, a DeprecatedOperation exception is thrown straight-away.

Some operations are deprecated and will be answered with HTTP 410. To avoid unnecessary API calls, a DeprecatedOperation exception is thrown straight-away.

deprecated_operation(*args, **kwargs)

Some operations are deprecated and will be answered with HTTP 410. To avoid unnecessary API calls, a DeprecatedOperation exception is thrown straight-away.

help(*args, **kwargs)

Some operations are deprecated and will be answered with HTTP 410. To avoid unnecessary API calls, a DeprecatedOperation exception is thrown straight-away.

item_lookup(item_id, **params)

Given an Item identifier, the ItemLookup operation returns some or all of the item attributes, depending on the response group specified in the request. By default, ItemLookup returns an item’s ASIN, DetailPageURL, Manufacturer, ProductGroup, and Title of the item.

ItemLookup supports many response groups, so you can retrieve many different kinds of product information, called item attributes, including product reviews, variations, similar products, pricing, availability, images of products, accessories, and other information.

To look up more than one item at a time, separate the item identifiers by commas.

list_lookup(*args, **kwargs)

Some operations are deprecated and will be answered with HTTP 410. To avoid unnecessary API calls, a DeprecatedOperation exception is thrown straight-away.

Some operations are deprecated and will be answered with HTTP 410. To avoid unnecessary API calls, a DeprecatedOperation exception is thrown straight-away.

multi_operation

MultiOperation is supported outside this API

similarity_lookup(*ids, **params)

The SimilarityLookup operation returns up to ten products per page that are similar to one or more items specified in the request. This operation is typically used to pique a customer’s interest in buying something similar to what they’ve already ordered.

If you specify more than one item, SimilarityLookup returns the intersection of similar items each item would return separately. Alternatively, you can use the SimilarityType parameter to return the union of items that are similar to any of the specified items. A maximum of ten similar items are returned; the operation does not return additional pages of similar items. If there are more than ten similar items, running the same request can result in different answers because the ten that are included in the response are picked randomly. The results are picked randomly only when you specify multiple items and the results include more than ten similar items.

tag_lookup(*args, **kwargs)

Some operations are deprecated and will be answered with HTTP 410. To avoid unnecessary API calls, a DeprecatedOperation exception is thrown straight-away.

transaction_lookup(*args, **kwargs)

Some operations are deprecated and will be answered with HTTP 410. To avoid unnecessary API calls, a DeprecatedOperation exception is thrown straight-away.

vehicle_part_lookup(*args, **kwargs)

Some operations are deprecated and will be answered with HTTP 410. To avoid unnecessary API calls, a DeprecatedOperation exception is thrown straight-away.

Some operations are deprecated and will be answered with HTTP 410. To avoid unnecessary API calls, a DeprecatedOperation exception is thrown straight-away.

Some operations are deprecated and will be answered with HTTP 410. To avoid unnecessary API calls, a DeprecatedOperation exception is thrown straight-away.