If you are an online shopaholic, then price comparison is always on top of your mind. But the manual process of doing this exercise is so tedious. You browse through multiple product listings across websites and keep eyeballing the price figures while switching browser tabs. There has to be a better way of doing it.
With the help of Price Analytics API, you can effortlessly search and get the product information from the top eCommerce portals such as Amazon and eBay. In this blog post, we show you how to automate price comparison using the Price Analytics API. You will get to know the API’s working and build a small Python-based tool to chart the pricing information for products on one of the supported platforms.
Introduction Of Price Analytics API
Price Analytics API offers product price information for numerous online eCommerce portals. It supports Amazon, eBay, Google, and Idealo.
To get started with this API, follow the steps below to subscribe and test the API.
1. Signup for RapidAPI Account
To begin using the Price Analytics API, you’ll first need to sign up for a free RapidAPI developer account. With this account, you get a universal API Key to access all APIs hosted in RapidAPI.
RapidAPI is the world’s largest API marketplace, with over 10,000 APIs and a community of over 1,000,000 developers. Our goal is to help developers find and connect to APIs to help them build amazing apps.
2. Navigate on search in RapidAPI Console:
You can search for “Price Analytics API,” on the RapidAPI homepage, or directly access the API Console.
3. Subscribe to Price Analytics API:
Once you are in the API console, click on the “Pricing” tab to take a look at the subscription tiers available for this API.
Price Analytics API has a freemium subscription model. Under the BASIC plan, you get 100 API calls per month for free. Subscribe to this plan, and you are all set to explore the API further.
4. Verify the Price Analytics API
The Price Analytics API does not return pricing data as an immediate response to the API request. It generates a unique job id for every API request session containing a search query. A subsequent API trigger with the job id returns actual data. Therefore, this API’s operation is split into two parts, using two separate API endpoints, POST Create session & GET Poll session results.
POST Create Session
This endpoint accepts a set of parameters for initiating a price query. Select the endpoint on the API console and enter the values for the request body parameters, as shown below.
These parameters signify
- source: The source marketplace from where the prices will be displayed. We have selected ‘ebay’ in this case.
- key: The key for search criteria. We have chosen ‘term’ which means a keyword term in the product description.
- country: The country where the marketplace is hosted. The value of ‘us’ limits the search to the United States.
- values: The search keyword. In this case, we are searching for ‘gopro’.
Therefore, we are searching on the eBay US website for a product identified by the term ‘gopro’.
Hit the “Test Endpoint” to trigger the API. It returns a unique job id.
In this way, the API accepts the request and returns a job id that identifies the search parameters’ unique session.
GET Poll Session Results
Now choose the GET Poll session results endpoint in the API console and enter the job id.
Click on the “Test Endpoint” button. The API response will present you with all the product listings that match the search criteria that we entered earlier as part of the POST Create session request.
You can see that each product listing contains some useful data such as pricing information and review/rating count.
Be aware that the API takes some time to return the search results for every job id. If you run the GET Poll session results immediately after POST create session, you may get a partial response indicating that the job id is still not finished.
With this step, you have successfully subscribed and verified the Price Analytics API.
It’s time to move ahead and do something interesting with this API.
Plotting the eBay Product Pricing Chat using Plotly
The information returned from the Price Analytics API contains a few quantitative data points. With the help of a charting tool, it is far easier to analyze this data instead of deciphering the API response.
Let’s build a Python program to display this data in the form of a simple bar chart. For this purpose, we will use Plotly, a Python data visualization library that is increasingly growing in popularity.
Before getting into coding, you must set up your Python programming environment and install the required libraries.
Prerequisites
To run the python code, you need to have the following prerequisites.
1. Install Anaconda: This is a complete Python data science platform with many libraries and its own set of tools. It also contains the conda CLI tool for Python environment and package management. Download and Install Anaconda from the official link.
2. Create a new Python environment: Open a command line terminal and use the conda CLI to create a new Python environment for this project, named ebaypriceplot.
# conda create —-name ebaypriceplot |
3. Activate the ebaypriceplot environment.
# conda activate ebaypriceplot |
You can now see a modified prompt on the terminal with the name of the environment you just activated. To double check, you can also run the following command.
# conda info —-envs |
4. Install the Plotly package. The Plotly library offers a full-featured data visualization toolkit. Use the conda CLI to include this library within the ebaypriceplot environment.
# conda install plotly |
5. Install the pandas library: The Pandas is a data analytics library for Python. Plotly works well with Pandas. Install this library with the following command
# conda install pandas |
6. Install requests library: The Requests library is used for triggering the API endpoints. Install this library with the following command.
# conda install requests |
With the above prerequisites, your programming environment is set up f0r executing the Python program.
Python Source Code
Launch your favorite Python editor and create a new python file named ebaypriceplot.py.
You are now ready to write the code. The entire code is split into four sections.
- Import and declarations
- API trigger function
- Data Extraction function
- Main logic
Follow the steps below to add the code section in ebaypriceplot.py.
Step 1: Import and Declarations
In the beginning, you have to add the import statements for the Python modules used in this program. Along with that, the global level declarations are also added.
import sys import json import datetime import requests import pandas as pd import plotly.express as px RAPIDAPI_HOST = "price-analytics.p.rapidapi.com" RAPIDAPI_URL = "https://price-analytics.p.rapidapi.com/job/" RAPIDAPI_KEY = "<YOUR_RAPIDAPI_KEY>"
These statements import the requests, Plotly, pandas libraries, that we have earlier installed in the environment.
Note that the placeholders <YOUR_RAPDAPI_KEY> must be replaced with the actual values obtained from the API console while you are logged in to the RapidAPI portal.
Step 2: Define the function for API trigger
Append the ebaypriceplot.py with the following function, below the import and global declarations.
def trigger_api(job_id): url = RAPIDAPI_URL + job_id headers = { 'x-rapidapi-host': RAPIDAPI_HOST, 'x-rapidapi-key': RAPIDAPI_KEY } response = requests.request("GET", url, headers=headers) if(200 == response.status_code): return json.loads(response.text) else: return None
The function trigger_api( ) accepts the job id as the argument and invokes the GET Poll sessions results API endpoint.
It returns the JSON formatted API response in the form of a Python dictionary.
Step 3: Define the function for data extraction
You need another function to extract the product listing data from API response.
Append the ebaypriceplot.py with the following function to sift through the product list to extract relevant information that will be used for generating the chart.
def extract_pricing(results): df = pd.DataFrame(columns=['Id', 'Price Type', 'Value']) product_list = data["results"][0]["content"] for record in product_list: if 'price' in record: df = df.append({'Id': record['id'] , 'Price Type' : "Price" , 'Value' : record['price'], "Description" : record['name'] }, ignore_index=True) df = df.append({'Id': record['id'] , 'Price Type' : "Shipping" , 'Value' : record['shipping'] , "Description" : record['name'] }, ignore_index=True) return df
The function extract_pricing( ) builds a pandas DataFrame to contain the price and shipping price for each product returned in the API response. It labels them using the ‘Price Type’ column. It also captures the product id and name in the ‘íd’ and ‘Description’ column.
Step 4: Define the main logic for generating chart
Now, define the main block of the program as follows.
if __name__ == "__main__": if(len(sys.argv) == 2): try: jobid_string = sys.argv[1] print("Generating Chart For Job Id " + jobid_string) data = trigger_api(jobid_string) if(data): if( "status" in data and data["status"] == "finished"): pricing_details = extract_pricing(data) fig = px.bar(pricing_details, x="Id", y="Value", color="Price Type", title="eBay Pricing", hover_name='Description') fig.show() else: print("This job is still not finished") else: print("Error in Triggering Price Analytics API") except Exception as e: print("Error in Generating Price Chart " + e) else: print("Invalid Job Id")
The main block expects a command-line argument to be passed to the program. In this case, it is the job id for an existing job, previously created with the POST Create session API endpoint.
The job id is passed to the trigger_api( ) call and the Dataframe is extracted from the extract_pricing( ) call.
The crux of this code lies in the variable fig that is defined after the call to extract_pricing( ).
It prepares a stacked bar chart that displays the price and shipping code for every product. The call to fig.show( ) launches a browser window to display the chart.
The program is now complete. You can close the code editor after saving ebaypriceplot.py.
Testing
To test this code, run this from the command line prompt after activating the ebaypriceplot Python environment under Anaconda.
(ebaypriceplot)# python ebaypriceplot.py <job_id> |
This program accepts a job id, therefore make sure that you run this program with a valid job id created earlier using the POST Create session API endpoint.
Running the program with the earlier query for ‘gopro’ will give you a graph something like this.
Now you can compare the price of all the GoPro products listed on eBay.
Try it with some other product keywords like ‘macbook’ and you should be able to plot a similar chart for all MacBooks listed on eBay.
Time for Cross Marketplace Price Comparison
Comparing prices within an eCommerce marketplace is the starting point. But wouldn’t it be awesome if we can do a comparison across multiple marketplaces.
So how about comparing the same product terms on eBay and Amazon? That would surely be a more interesting graph to look at.
With Plotly, we have already shown you the way to extract and plot the data from Price Analytics API for one marketplace. Now go ahead and tweak it to plot eBay and Amazon prices as a grouped bar chart to see instant price comparison.
If you have any comments on this demo, send us a comment. We would be happy to see you build more interesting visualizations with the Price Analytics API.
I keep getting a few errors when running the code as discribed
Generating Chart For Job Id 603**********
Traceback (most recent call last):
File “/home/code/cards/ebaypriceplot.py”, line 65, in
pricing_details = extract_pricing(data)
File “/home/code/cards/ebaypriceplot.py”, line 41, in extract_pricing
df = df.append({‘Id’: record[‘id’] , ‘Price Type’ : “Price” ,
TypeError: string indices must be integers
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/home/code/cards/ebaypriceplot.py”, line 80, in
print(“Error in Generating Price Chart ” + e)
TypeError: can only concatenate str (not “TypeError”) to str
New to python and APIs