Flipkart Price API with Google Apps Script

Flipkart, the popular shopping website in India that sells everything from erasers to televisions, offers no API and therefore if you were to extract the pricing information of any Flipkart product, screen scraping is the only alternative.

Flipkart stores the pricing data inside <meta> tags with “itemprop” set to “price” and it is thus relatively easy to pull this information for the price tracker tool.

Here’s the Google Apps Script code that extracts the price details, item title and the thumbnail image given the URL of the product page using Regular Expressions. You can easily use this in combination with HTMLService  to create an API that returns pricing data for Flipkart product as JSON or XML.

function priceFlipkart(url) {
  if (url !== '') {
    try {
      /* Extract the HTML source of the Flipkart Page */
      var page = UrlFetchApp.fetch(url).getContentText();

      /* Regular Expression to extract Price from the META tag */
      var regex = /<meta[^>]*itemprop\s*=\s*"price"\s*content\s*=\s*"([^"]*)"/gi;

      if ((price = regex.exec(page)) !== null) {
        regex = /<meta[^>]*name\s*=\s*"og_title".*content\s*=\s*"([^"]*)/gi;
        title = regex.exec(page);

        /* We are using Canonical URL as it containes no tracking parameters */
        regex = /<meta[^>]*name\s*=\s*"og_url".*content\s*=\s*"([^"]*)/gi;
        canonical = regex.exec(page);

        /* The thumbnail image of the Flipkart Product */
        regex = /<meta[^>]*name\s*=\s*"og_image".*content\s*=\s*"([^"]*)/gi;
        image = regex.exec(page);

        if (title && canonical && image) {
          Logger.log(title[1] + '|' + image[1] + '|' + price[1]);
        } else {
          Logger.log('Could not fetch ' + url);
        }
      }
    } catch (e) {
      Logger.log('Flipkart Error: ' + e.toString());
    }
  }
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

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

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

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

We will never send any spam emails. Promise 🫶🏻