Get the Amazon Sales Rank by ASIN in Google Spreadsheet

Given the Amazon ASIN number, the getAmazonSalesRank() method with return the overall Sales Rank of the item using the Amazon Product Advertising API. You’ll need to enter your own Amazon AWS keys and associate ID before making the API calls.

This can also be converted into a custom function for Google Spreadsheets where you can enter the product ASINs in one column the latest sales rank is displayed in another column. It is however recommended that you either use the Cache Service or store the results in Property Service to avoid making too many calls to the Amazon API.

function getAmazonSalesRank(asin) {
  try {
    var method = 'GET',
      uri = '/onca/xml',
      host = 'ecs.amazonaws.com',
      public_key = 'YOUR_PUBLIC_KEY',
      private_key = 'YOUR_PRIVATE_KEY',
      associate_tag = 'YOUR_AMAZON_ASSOCIATE_ID';

    var params = {
      Service: 'AWSECommerceService',
      Version: '2011-08-01',
      AssociateTag: associate_tag,
      Operation: 'ItemLookup',
      ItemId: asin,
      Timestamp: new Date().toISOString(),
      AWSAccessKeyId: public_key,
      ResponseGroup: 'SalesRank',
    };

    var canonicalized_query = Object.keys(params).sort();

    canonicalized_query = canonicalized_query.map(function (key) {
      return key + '=' + encodeURIComponent(params[key]);
    });

    var string_to_sign = method + '\n' + host + '\n' + uri + '\n' + canonicalized_query.join('&');

    var signature = Utilities.base64Encode(Utilities.computeHmacSha256Signature(string_to_sign, private_key));

    var request =
      'https://' + host + uri + '?' + canonicalized_query.join('&') + '&Signature=' + encodeURIComponent(signature);

    var response = UrlFetchApp.fetch(request, { muteHttpExceptions: true }).getContentText();

    var elems = XmlService.parse(response).getDescendants();

    for (var i = 0; i < elems.length; i++) {
      if (elems[i].getType() === XmlService.ContentTypes.ELEMENT)
        if (elems[i].asElement().getName() === 'SalesRank') return elems[i].asElement().getText();
    }
  } catch (f) {}

  return '';
}
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.