Web DevCenter
oreilly.comSafari Books Online.Conferences.
MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA

Sponsored Developer Resources

Web Columns
Adobe GoLive
Essential JavaScript

Web Topics
All Articles
Scripting Languages

Atom 1.0 Feed RSS 1.0 Feed RSS 2.0 Feed

Learning Lab

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

Pages: 1, 2

Next Pagearrow