How ToTips and Tricks

Definitive Guide on How to Use Flipkart Affiliate API to Get Latest Offers [Demo Included]

Ecommerce was a sector which was not that very grown a decade ago. Its popularity rose tremendously and ecommerce is a very well established sector now. Brick and Mortar stores  took a hit when the online marketplaces started emerging. The reason why people tend to shop online is that they are able to get everything  with a touch of a button. And there are way more options to choose online.

Flipkart is among the most popular E-commerce website in India. Amazon is the main competitor for Flipkart in India. Amazon is a giant MNC company with world wide reach, while Flipkart is a homegrown ecommerce platform.  Flipkart started off as a marketplace to buy books which later transformed into the best online shopping website out there in India. The website was founded by Sachin Bansal and Binny Bansal in the year 2007.

Flipkart announced that it will be acquired by the US giant Walmart for a  jaw dropping amount of $16 Billion. The acquisition will be completed  by the end of 2018.

Flipkart is an online marketplace for all kinds of stuffs from fashion to groceries. There is literally all the things needed for a household. All the items can be brought from their online store at www.flipkart.com at affordable prices. With tons of offers and discounts, it is the best place to shop online. We can find good quality stuff at great prices.

Flipkart Affiliate Marketing?

Flipkart Affiliate Marketing

Flipkart being a huge marketplace, one of the reason why it became this popular is due to users who share the word among the others through their blogs and websites. You can see there are tons of websites that recommends products from a particular website. They often share the links to products. All these comes under Affiliate Marketing.

The websites pays the users a small commission for the sales which they bring. That is when a user brings others to the platform and if the other party makes a purchase, the company will share a percentage of the sale to the referrer who introduced the second party to the company website. This is how affiliate marketing works.

Users promote a website or products and brings sales to the company. The company pays the referrers a fees for bringing users and helping in sale of products. Flipkart also has an Affiliate program. It is simply called the Flipkart Affiliate Program(FAP). Affiliate users can promote products from Flipkart and bring in sales to the company. When the sale is complete, Flipkart will pay the referrer a fees.

This will mutually help both the company and the affiliate user. Both of them are benefiting from the sale. Flipkart provides many tools and options to bring in sales. There are Offer Banners which will take the user to the landing page of the website. Then there are Coupon Codes, by which the users can avail discount by entering the Coupon Codes.

The users will be assigned some kind of tags to identify themselves and helps the company to track who brought the sale. These kind of links with tags to identify the referrer is often referred to as Affiliate Links.

What is Flipkart  Affiliate API?

As i mentioned above, the company provides the affiliate marketers with Affiliate Tools and options for promoting products and offers….right! One of the tools which Flipkart has provided its users is the Flipkart Affiliate API or just Flipkart API.

Now you might be wondering What is this API? Well API Stands for Application Programming Interface. Basically it is a medium which allows to piece of softwares to communicate with each other( In this case, It is between Flipkart Website and Your Website/Blog). It is a set of programming instructions and standards that are mentioned for accessing information from a web service. Why a company releases its API? is to help other developers use their services to build  other services.

Flipkart API helps developers to leverage information from the Flipkart Website to help them promote offers and products in a better way. Flipkart just needs sales, so developers can develop custom applications to showcase details from Flipkart. The final result will be sales.

Developers use Flipkart API to get information about products and get the affiliate links to the product ( Links which contains the tags to identify the referrer). This makes the affiliate user to easily promote products and offers. This makes promotion very easy.

Why Use Flipkart Affiliate API?

You can see there are many websites which shows latest products on sale from a website with the offer prices. How do you think they are able to get these offers on time with updated informations? Its because they use APIs.

Say in a website I have added a product with my affiliate link. I got the images from other website and i added the price which was available at the time. But if the company had reduced the prices, the information on my website would become outdated. So i have to regularly check for changes and update it manually.

But with the help of API we can get the pictures and exact price of the product from flipkart. This means that the prices on my website will automatically change when the price on flipkart changes. This helps my users get a better idea of the product and hopefully buy it.

How to Use Flipkart Affiliate API?

Use Flipkart Api to get the Latest Deal of the Day( DOTD) offers going on.Flipkart API is very powerful and we can use it to get the details of products and offers available on the platform. We are going to see how to use the Flipkart API to get the DOTD offers that are live on the Flipkart Site using a php webpage. We will be using cURL to call the endpoints and later parse the received response in a beautiful card layout using Bootstrap.

Get Started on Flipkart Affiliate API

For using the Flipkart API, you will have to sign-up for an Affiliate account. Visit Flipkart Affiliate to Sign up for a free account. Once you have signed up for the affiliate account, its time to get the API key.

Flipkart Affiliate Official Documentation

How to get Flipkart Affiliate API Token and ID?

I hope you are already a FAP member. If you are not, then the first thing to do is to Sign Up for an affiliate account. Unfortunately Flipkart Affiliate Program (AFP) has paused new user registrations for the moment. There is no Official word on when the program will be resuming.

How to get Flipkart Affiliate API Token

  1. Login to Flipkart Affiliate Program Dashboard with your username and password.
  2. Click on APIAPI Token option from the left menu.
  3. Click on Generate API Token to get your API Token.
  4. Make a Note of your Affiliate Tracking ID and the API Token.

Never Share your API Token with others.

Flipkart API – How to Get Offer Details?

Flipkart has a DOTD Offers endpoint. When this endpoint is called it will send the details of all the Deals of the Day offers in JSON format. To get the response it is necessary to add our Flipkart Affiliate ID and Flipkart Affiliate Token.

The Endpoint URL for getting the DOTD offers is :

https://affiliate-api.flipkart.net/affiliate/offers/v1/dotd/json.

We have to then add two piece of information  to the request headers to get the response. If these parameters are not sent with the api call, it will return an error. We are using cURL to call the api and add the required parameters as headers.

Visit the API DOTD offers Documentation Here.

Code Snippet 1 – Making the API Call:
<?php
  $curl = curl_init();
  curl_setopt_array($curl, array(
  CURLOPT_URL => "https://affiliate-api.flipkart.net/affiliate/offers/v1/dotd/json",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_SSL_VERIFYPEER => FALSE,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
     "Cache-Control: no-cache",
     "Fk-Affiliate-Id: Your Aff ID",
     "Fk-Affiliate-Token: Your Aff Token")
  ));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
?>

Sample JSON Response of API Call

See the JSON Sample Response we get from the API Call. The  text marked in Red are the Keys and those marked in  Blue are the Values. This is the data structure of JSON. Once we know how we get the details, we can go ahead and manipulate the data to be showcased in a good way using HTML and CSS.


You can see the Sample JSON response of Flipkart DOTD Offers call.


See the image below. It shows the JSON response that is received when the DOTD endpoint is called. It is pretty easy to understand how JSON data type shows data.

Flipkart Affiliate API sample response

Explanation of the returned Data:
  1. name”  => The name of the Offer that is currently running.
  2. title” => Gives the title of the Offer.
  3. description” => Provides additional information about the offer.
  4. url” => The Affiliate URL to the Offer Landing Page.
  5. imageUrls” => Array which contains the images of the Offers in different resolutions.
    1. url” => Gives the link to the image.
    2. resolutionType” => Shows the resolution of the image.
  6. availability” => Shows the status of the Offer whether it is Live or Not.

Creating a Template to Display the Data

Now that we have got the JSON data using API call, we can now start creating the frontend . We have to design a template to showcase the data that we received. The data received is made of raw text. We have to use the data and make a good template to represent the data.

JSON to Design Map:

Flipkart Affiliate API JSON to HTML Mapping

The above image shows how I am mapping the JSON data to a HTML Card template. This is the design I am gonna use for the project. I  used BootStrap v4.0 to create a card template which will showcase the information in a nice way.

How to Parse JSON Data from Flipkart API Response

Now that we have a design in mind, It is time to get to work. This is were all the work is needed. Till now we can got the latest offers details using the API. The response is in JSON Data. We need to parse the received data in order to display it in webpage.

In this tutorial we are going to use php to parse the data.

We are going to add the data of the offers into different arrays. All these arrays will be having our required data  which will be later used to display the info.

Code Snippet 2 – Parsing the JSON Data :
$data = json_decode($response,true);
$dotd = $data["dotdList"];
$num = count($dotd);
for($x=0;$x<$num;$x++){
    $dealimage[$x] = $dotd[$x]["imageUrls"][2]["url"];
    $dealtitle[$x] = $dotd[$x]["title"];
    $dealdesc[$x] = $dotd[$x]["description"];
    $dealname[$x] = $dotd[$x]["name"];
    $deallink[$x] = $dotd[$x]["url"];
    $dealstatus[$x] = $dotd[$x]["availability"];
}
Code Explanation:

After we have made the API call, we got the response in  $response.

The JSON data in the $response is then decoded into $data. After that we check for the number of offers that are returned in the reponse. Using a for loop the corresponding data is stored into arrays.

$dealimage[0] will contain the image of the first Offer, $dealtitle[0] will contain the title of the first offer etc. Similarly $dealimage[1] will contain the image of the next offer…..This will give you an idea how we have parsed the data into arrays.

How to Display the Parsed Data in HTML

Now that we have successfully parsed the JSON data into simple php arrays. We can simply use the array elements to get the value inside them. For this we will have to use a php function called foreach This is for iterating over each array values.

Code Snippet 3 – Displaying the Data:
<?php foreach($dealtitle as $key=>$value): ?>
<div class="col-6 col-sm-4">
  <div class="card">
    <p class="dealstatus"><?php echo $dealstatus[$key] ?></p>
    <img class="card-img-top" src="<?php echo $dealimage[$key] ?>" alt="Card image cap">
    <div class="card-body">
     <h6 class="card-title"><?php echo $dealname[$key] ?><?php echo $dealtitle[$key] ?></h6>
     <p class="card-text"><?php echo $dealdesc[$key] ?></p>
     <a class="btn" href="<?php echo $deallink[$key] ?>">BUY NOW</a>
    </div>
 </div>
</div>
<?php endforeach; ?>

Flipkart DOTD Offers Full Code:

Now that we have all the required snippets for our full working webpage, lets start joining all the code into a single working code.

We  have 3 snippets of code:

  1. For making the call to the API endpoint.
  2. For Getting the data and storing it into suitable arrays.
  3. Displaying the values.
Final Code:
<?php
  $curl = curl_init();
  curl_setopt_array($curl, array(
  CURLOPT_URL => "https://affiliate-api.flipkart.net/affiliate/offers/v1/dotd/json",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_SSL_VERIFYPEER => FALSE,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
     "Cache-Control: no-cache",
     "Fk-Affiliate-Id: Your Aff ID",
     "Fk-Affiliate-Token: Your Aff Token")
  ));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$data = json_decode($response,true);
$dotd = $data["dotdList"];
$num = count($dotd);
for($x=0;$x<$num;$x++){
   $dealimage[$x] = $dotd[$x]["imageUrls"][2]["url"];
   $dealtitle[$x] = $dotd[$x]["title"];
   $dealdesc[$x] = $dotd[$x]["description"];
   $dealname[$x] = $dotd[$x]["name"];
   $deallink[$x] = $dotd[$x]["url"];
   $dealstatus[$x] = $dotd[$x]["availability"];
  }
?>
<div class="container-fluid">
  <div class="row row-eq-height">
    <?php foreach($dealtitle as $key=>$value): ?>
      <div class="col-6 col-sm-4">
       <div class="card">
        <p class="dealstatus"><?php echo $dealstatus[$key] ?></p>
        <img class="card-img-top" src="<?php echo $dealimage[$key] ?>" alt="Card image cap">
        <div class="card-body">
         <h6 class="card-title"><?php echo $dealname[$key] ?><?php echo $dealtitle[$key] ?></h6>
         <p class="card-text"><?php echo $dealdesc[$key] ?></p>
         <a class="btn btn-success" href="<?php echo $deallink[$key] ?>">BUY NOW</a>
        </div>
      </div>
   </div>
  <?php endforeach; ?>
 </div>
</div>

This the final code snippet that you need. Note that you have to place this inside of the body tag of an HTML document. Make sure to add BootStrap in the head section of the Page. Finally save the file as a php file. This is just the bare design. Feel free to mess around with the design using custom CSS. Let your imagination flow.

View on Github

Flipkart API Offers Listing Demo

Flipkart Affiliate API Demo

The webpage is written in php + HTML. For styling the products into a beautiful card format, I have used Bootstrap 4.0. php + cURL is used to call the Flipkart API – DOTD offer page. We requested the response in JSON format. Flipkart API supports JSON and XML. We created a cool card template for the images, title, description and a button element which will link to the product page.


Visit Flipkart Affiliate API-Offers Demo Page to see the Flipkart API in action.


The API is called and the response is broken down and stored in arrays. We need to get the title, description, the image link and the affiliate link to the product/offer landing page. 4 arrays are created and all the JSON array data is stored into their corresponding php arrays. Later these arrays are iterated to create cards for each offer listing. The cards are dynamically created according to the number of offers listed in the API response (We haven’t created a default card number).

Recommended Tools

Flipkart Affiliate API testing using PostMan

For making the calls and gettings the result, no tools are necessary. We can write the code in php or javascript or any other language and execute it. But if you want to experiment with APIs, the best tool to have is POSTMAN. Use PostMan to make calls and receive the response very easily. Its very easy to add headers and cookies with the API call. Plus it will provide you with the code in different languages so that you don’t have to write the code on your own.

For those who loves to experiment, PostMan can be a great tool to play around with APIs. Try it out.

 

Tags

Stay Up to Date with our Latest Tech News

We hate Spam as much as you do!

Adithya

Loves anything tech. Loves writing about tech and share the things he knows to the world. Interests include Gaming, Web Designing and is a die hard Pop Music Fan. Fav include Anne Marie, Ed Sheeran. Team #Android.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.