Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A JSON webservice for Amazon's product catalogue.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 .gitignore
Octocat-spinner-32 README.textile
Octocat-spinner-32 app.rb
Octocat-spinner-32 config.ru
Octocat-spinner-32 config.yml
Octocat-spinner-32 standalone.ru
README.textile

Amazon JSON API

amazon-json-api is a JSONP-enabled webservice that serves amazon.com’s product catalogue in an easily consumable JSON format.

You can use amazon-json-api to pull data from Amazon’s product catalogue and display it on your site.

The application uses Amazon’s ItemSearch and the amazon-ecs gem behind the scenes to pull in data from Amazon.

Installing

You’ll need Sinatra, Rack::Cache, amazon-ecs and ActiveSupport:


gem install sinatra rack-cache activesupport amazon-ecs

Configuring

Put your Amazon API key and secret in config.yml:


amazon:
  debug: true
  key: xxx
  secret: xxx

Results are by default cached in memory for 24 hours. You can tweak cache settings in config.yml:


cache:
  ttl: 86400
  metastore: heap:/
  entitystore: heap:/
  #metastore: file:/tmp/cache/rack/meta
  #entitystore: file:/tmp/cache/rack/body
  verbose: false

Launching

Starting the web server is as easy as executing this command:


$ ./app.rb

Tip: use Phusion Passenger and nginx in a production environment.

How to

All request parameters are forwarded as-is to the Amazon REST-API, which means this request:
http://localhost:4567/?response_group=Medium&search_index=Books&sort=salesrank&power=title:python+and+programming

Translates to the following ItemSearch query:
http://webservices.amazon.com/onca/xml?AWSAccessKeyId=xxx&Keywords=&Operation=ItemSearch&Power=title%3Apython%20and%20programming&ResponseGroup=Medium&SearchIndex=Books&Sort=salesrank&Timestamp=2010-04-16T19%3A18%3A47Z&Signature=xxx

Note how the signature, timestamp, and access key are all added automatically.

Examples

Search books and return JSON

This URL executes a power search for books having the words python and programming in the title:

http://localhost:4567/?response_group=Medium&search_index=Books&sort=salesrank&power=title:python+and+programming

The books are sorted by salesrank. Data is returned as JSON.

See the ItemSearch documentation for details.

Search books and return JSONP

The benefit of using JSONP is that you can host the webservice on one domain and use it on as many other domains as you need.

To search for books we use the same URL as the previous one with the addition of a callback parameter, which tells the webservice to return JSONP instead of JSON:
http://localhost:4567/?response_group=Medium&search_index=Books&sort=salesrank&power=title:python+and+programming&callback=showBooks

To retrieve the data you would add this code to your HTML:


<script defer="defer" type="text/javascript" src="http://localhost:4567/?response_group=ItemAttributes,Images&search_index=Books&sort=salesrank&power=title:xxx&callback=showBooks"></script>

Or the jQuery equivalent:


$.ajax({
    type: 'get',
    url: '/?your query goes here',
    dataType: 'jsonp',
    success: function(books) {
      showBooks(books);
    }
})

To render the data you would write a JSONP callback method such as this:


 <script type="text/javascript" defer="defer">
  function showBooks(books) {
    $.each(books, function(index, book) {
        var title = book.itemattributes.title;
        var url = book.detailpageurl;
        var image = book.mediumimage.url;
        var height = book.mediumimage.height;
        var width = book.mediumimage.width;

        // Do something smart here
    });
  }
</script>

Search the electronics department

This URL searches the electronics department for items containing the keyword “bluray”:
http://localhost:4567/?response_group=Medium&search_index=Electronics&sort=salesrank&keywords=bluray&callback=showResults

See the full list of search indexes for more options.

That’s it. You can see the webservice in action on this code snippets page.

Author

Christian Hellsten (Aktagon Ltd.)

Something went wrong with that request. Please try again.