O'Reilly Book Excerpts: Amazon Hacks

Hacking Amazon, Part 2

Related Reading

Amazon Hacks
100 Industrial-Strength Tips & Tools
By Paul Bausch

by Paul Bausch

Author's note: Amazon has a history of embracing the nature of the Web by opening its platform to outsiders. Its founders realized early that any web site can link to any other web site, and that means hefty competition for attention and links. Amazon turned this apparent chaos to its advantage by creating its Associates Program. The program pays web site owners a percentage of the sales made from referred customers. With a proliferation of competition from sites selling used books, Amazon opened its sales infrastructure to allow anyone to sell the same items it was selling--alongside Amazon's listings. Even Amazon competitors can sell their books on Amazon.com.

With this history in mind, it's not surprising that Amazon has packaged all of these features into a developer-friendly environment that allows web site authors to integrate even more closely with Amazon to take advantage of these features. With an open API based on standard Web services (and a bit of hacking for those features that aren't included in the API yet), developers can integrate Amazon features with their businesses (see Hack #54); cell phones (see Hack #90); desktop applications (see Hack #11); and web sites (see Hack #69). Amazon Hacks gives you 100 concrete examples just like these, showing exactly what features are available, how to use them, and how to make this kind of integration happen.

Hack #54: List Your Items for Sale on Your Web Site

If you have your own web site, you can mirror your Amazon listings there so you can reach your audience as well.

Amazon has millions of customers at their site looking for items, and getting your listing in front of them is important. If you have a web site, though, letting your own audience know about your sale items could be just as important. It's not only the number of people that see a listing that matters, but getting the right person to see the listing.

The easiest way to point your web visitors to your sale items is to link to a list of all your open Marketplace items on Amazon. To find the link, visit your seller account (http://www.amazon.com/seller-account/) and click "View your Member Profile."

TIP: The URL of your Member Profile page contains your Seller ID. The alphanumeric string following customer-glance is your unique ID number. Jot it down while you're there because it's necessary for the following hack.

On your member profile page, click "View Open Marketplace Listings." The page lists all of your open sale items. This page is publicly viewable, so just copy the URL from the address bar of your browser and include it in an HTML link on your site like this:

<a href="http://s1.amazon.com/exec/varzea/ts/&return;
customer-open-marketplace-items/your seller ID/">Buy my stuff!</a>

If you want to integrate beyond a link, you can list every item you're selling on your site.

Included in every listing confirmation email [Hack #49] you'll find a link that leads to a page for your specific product listing. If you want to include all of the items you have for sale on your web site, you could copy each of these links and paste them into an HTML file. You'd also need to copy the name of the product and how much you've listed it for. You'd have to revisit this HTML every time an item sells or anytime you make a change to your listings.

Luckily, you can automate this entire process with Amazon Web Services.

The Code

This ASP script requests your latest items for sale on Amazon and formats them as a local web page. You'll need an Amazon developer's token Section 6.4 to run the script, and you can include an affiliate tag. You'll also need to find your Seller ID and set it at the top of the code.

  ' Set Associate ID, Developer Token, and Seller ID
  Const AFF_TAG = "insert associate tag"
  Const DEV_TOKEN = "insert developer token"
  Const SELLER_ID = "insert seller ID"
  XMLURL = "http://xml.amazon.com/onca/xml3" & _
           "?t=" + AFF_TAG & _
           "&dev-t=" + DEV_TOKEN & _
           "&SellerSearch=" + SELLER_ID & _
           "&type=heavy" & _
           "&page=1" & _
           "&offerstatus=open" & _
  ' Issue the request and wait for the response
    Set xmlhttp = Server.CreateObject("Msxml2.SERVERXMLHTTP")
    xmlhttp.Open "GET", XMLURL, false
    If Err.Number <> 0 Then
        response.write "Unable to connect to Amazon."
    End If    

    Set XMLDoc = xmlhttp.responseXML
    'response.write "<xmp>" & XMLDoc.xml & "</xmp>"
    If XMLDoc.parseError.ErrorCode <> 0 Then
        response.write "Error: " & XMLDoc.parseError.reason
    End If

    ' Look for the ErrorMsg tag
    Set XMLError = XMLDoc.SelectNodes("//ErrorMsg")
    ' If it exists, display the message and exit
    If XMLError.length > 0 Then
       response.write XMLDoc.SelectSingleNode("//ErrorMsg").text
    End If

    ' If there's no error, loop through items for sale
    Set ProdDetail = XMLDoc.SelectNodes("//ListingProductDetails")
    For x = 0 To (ProdDetail.length - 1)
      strExID = ProdDetail(x).selectSingleNode("ExchangeID").text
      strTitle = ProdDetail(x).selectSingleNode("ExchangeTitle").text
      strPrice = ProdDetail(x).selectSingleNode("ExchangePrice").text
      strOffer = ProdDetail(x).selectSingleNode("ExchangeOfferingType").text
      response.write "<b>" & strTitle & "</b><br>"
      response.write strPrice & " (" & strOffer & ")"
      response.write "<br><br>" & vbCrLf

Running the Hack

Save the code to a file called SalesListings.asp and upload it to your IIS server. Request the file from a browser to see your product listings:


Hacking the Hack

Taking this script one step further, you can let your visitors add your items directly to their Amazon shopping carts. Include the following HTML form to print a "Buy from Amazon.com" button along with each item.

<form method="POST" action="http://www.amazon.com/o/dt/assoc/&return;
handle-buy-box=insert ASIN">
<input type=hidden 
    name="exchange.insert exchange ID.insert ASIN.insert Seller ID" 
<input type="hidden" name="tag-value" value="insert affiliate tag">
<input type="hidden" name="tag_value" value="insert affiliate tag">
<input type="submit" name="submit.add-to-cart" value="Buy From Amazon.com">

You'll need to insert the proper exchange ID, ASIN, and Seller ID, and these values are available as variables in the script. To add this form to SalesListings.asp, insert the following code just before the last line of the loop (response.write "<br><br>"), and include the necessary variables.

strASIN = ProdDetail(x).selectSingleNode("ExchangeAsin").text
strSellerID = ProdDetail(x).selectSingleNode("ExchangeSellerId").text
<form method="POST" action="http://www.amazon.com/o/dt/assoc/&return;
handle-buy-box=insert ASIN">
<input type=hidden 
    name="exchange.<%= strExID %>.<%= strASIN %>.<%= strSellerID %>" 
<input type="hidden" name="tag-value" value="insert affiliate tag">
<input type="hidden" name="tag_value" value="insert affiliate tag">
<input type="submit" name="submit.add-to-cart" value="Buy From Amazon.com">

When someone visiting the page clicks "Buy from Amazon.com," your specific item will be added to their cart and the sale will go to you.

Add a Co-Branded Checkout

For more hacking-the-hack fun, you can ease the transition from your site to Amazon's with some co-branding. By setting the following form with each item listing, the "Buy from Amazon.com" button will tell Amazon to show your logo as customers step through the checkout process on their server.

<form action="http://s1.amazon.com/exec/varzea/dt/cbop/order-checkout/"
<input type="hidden" name="purchase-navbar"
   value="insert image URL">
<input type="hidden" name="purchase-store-name" 
   value="insert your store name">
<input type="hidden" name="purchase-return-url" 
   value="insert your store URL">
<input type="hidden" name="purchase-exchange-id"
   value="insert exchange ID"/>
<input type="hidden" name="purchase-quantity" value="1">
<input type="hidden" name="purchase-storefront-name" 
   value="insert your store name"/>
<input type="submit" name="go" value="Buy from Amazon.com">

To include this co-branded checkout in SalesListings.asp, just fill in the image, store name, store URL, and exchange ID, and include the HTML just before the last line of the product loop. Be sure to turn off script processing with %> just before this form, and then turn script processing back on with <% just after this code. You can also insert the exchange ID with the variable set earlier in the script in an ASP tag, like <%= strExID %>.

Instead of sending customers from your site directly to Amazon's cart page, you can send them to a minimalist checkout process (as shown in Figure 4-8) that includes your site's logo.

Figure 4-8. A co-branded checkout page

Hack #90: Create a Wireless Wish List

Take your Wish List wherever you go with AWS, XSLT, and WAP!

How many times have you been browsing at a bookstore or video store and you can't remember which books or movies you wanted to pick up? Sure, you could write them all down on a piece of paper and take it with you, but what fun would that be? Instead, you can use powerful cell networks and web applications to do the work for you. If you track wanted books and movies with your Amazon Wish List and have a WAP-enabled cell phone, you can always have your list handy.

WAP stands for Wireless Access Protocol, and it's used for delivering information to cell phones and other handheld devices. The information itself is formatted with WML (Wireless Markup Language), an XML format.

Just as HTML pages have <body> elements that contain the bulk of the page, WML pages have <card> elements. Each WML page can contain several cards, but only one card is displayed at a time. Also, links work much the same way as in HTML. Instead of <a href> tags, WML relies on <anchor> and <go href> tags. This example creates one card with a list of items. Each item links to the Amazon WAP product detail page for that item.

The Code

Making AWS responses available to a cell phone is just a matter of converting one XML format (AWS response) to another (WML). Once again, Amazon's XSLT service makes this quick work.

Create an XSL file called wap_wishlist.xsl with the following code:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/&return;
<xsl:output method="wml" doctype-public="-//WAPFORUM//DTD WML 1.1//EN" &return;
<xsl:template match="/">
    <card id="Menu" title="Wishlist">
    <p><b>My Wishlist</b><br/><br/>
    <xsl:for-each select="ProductInfo/Details">
            <b><xsl:value-of select="Catalog" />:</b>
            <br/><xsl:value-of select="ProductName" />
            <xsl:if test="Artists">
                <xsl:value-of select="Artists/Artist" />
            <xsl:if test="Authors">
                <xsl:value-of select="Authors/Author" />
            <br/><xsl:value-of select="OurPrice" />
            <br/><anchor>Details<go><xsl:attribute name="href">http://&return;
www.amazon.com/exec/obidos/redirect?tag=insert associate tag%26creative=&return;
insert developer token%26camp=2025%26link_code=xm2%26path=ct/text/&return;
vnd.wap.wml/-/tg/aa/xml/glance-xml/-/<xsl:value-of select="Asin" />&return;

There are a few important things to note in this code. First, the output type has been set to WML with the <xsl:output> tag. Without this, the required <!DOCTYPE tag would not be included in the transformed page. Also notice the link to WAP Amazon detail pages. The URL format is the same (see Hack #72), but the ampersands have all been changed to the encoded %26. Ampersands aren't valid when used in URLs inside a WML file, and phones can't display the page if they aren't escaped.

Running the Hack

The code is just an Amazon query, so we need to get the right URL. Find your Wish List ID [Hack #18] and include it in a wish list search like this:

http://xml.amazon.com/onca/xml3?t=insert associate tag &return;
&dev-t=insert developer token&WishlistSearch=[your wishlist]&type=lite&f=xml

Upload wap_wishlist.xsl to a publicly accessible server and change the value of f= to the URL for the file:

http://xml.amazon.com/onca/xml3?t=insert associate tag &return;
&dev-t=insert developer token&WishlistSearch=[your wishlist]&type=lite &return;

There's just one more change that needs to be made. WAP browsers are looking for a certain content type. Content types are specified as HTTP headers, and there's no way to include those in our XSL file. Luckily, Amazon provides a way to specify an alternate content type header with the query itself. Add the ct= variable and set the content type to text/vnd.wap.wml.

http://xml.amazon.com/onca/xml3?t=insert associate tag &return;&dev-t=insert 
   developer token&WishlistSearch=[your wishlist]&type=lite &return;

That's all there is to it. Unfortunately this URL is a bit long to type into your phone with your keypad, but there are a couple of ways to work around this. First, see if your cell provider has a web interface for adding bookmarks to your phone's WAP browser. If so, you could copy and paste this monster into your cell provider's site, and then have 1-click (ahem) access through your phone's bookmarks. Another method is to upload a WML file to your site that contains a link to your wish list via this URL. Then you'd just need to browse to your own WAP page first and follow the link. If you go this route, try giving your WAP page a short name, like a.wml; this will save some keying.

Paul Bausch is a co-creator of the weblog software Blogger, maintains a directory of Oregon-based weblogs at ORblogs.com, and is the author of the forthcoming Yahoo! Hacks.

Return to the Web DevCenter.

Copyright © 2017 O'Reilly Media, Inc.