Welcome to the 2nd of Prisync API Tutorial Series aiming to clarify the API foundations.
In the first blog post, I speculated about:
- which problems Prisync (and the API V2.0) solves on behalf of you,
- creating the API key and token,
- adding your first product via API V2.0,
- adding your URL and a bunch of competitor URLs,
- and batch importing via the API.
Purpose – Prisync API Tutorial
In this post, I’ll go through gathering the data using Prisync API V2.0. Yet, you can re-visit the first post of Prisync API Tutorial Series and API documentation.
First things first, I’ll assume that you followed the first post and you’ve already added a couple of products to your Prisync account. No worries if you don’t have any products. Just go back and complete the first tutorial or you can add products on your dashboard. Besides, we’ll need the make_request method that we used in the first post so that let me recap it below:
def make_request(path, method, payload=None): | |
# Use API Key and API Token that you've copied from | |
# your settings page. | |
headers = { | |
'apikey': 'apidemo@prisync.com', | |
'apitoken': 'c18888888888888888888fa135f540', | |
} | |
# This is the root URL. All API calls are relative to this URL. | |
base_url = "https://prisync.com/api/v2" | |
# We append the path into base_url | |
url = "".join([base_url, path]) | |
# HTTP GET for getting data | |
if method == 'get': | |
result = requests.get(url, headers=headers) | |
# HTTP POST for usually creating new data and editing data. | |
elif method == 'post': | |
result = requests.post(url, data=payload, headers=headers) | |
else: | |
raise NotImplementedError | |
return result |
It’s a tiny method which calls the API endpoints, adding the authentication headers.
Listing the Products You Have
Prisync API V2.0 has /list/product endpoint, which basically lists all products on your account.
All you need to do is making a request to the /list/product endpoint:
response = make_request('/list/product', 'get') | |
print(response.status_code, response.text) |
The response is below:
{ | |
"results": [ | |
{ | |
"id": "2055805", | |
"name": "iPhone X, GSM Unlocked 5.8 - 64 GB - Space Gray", | |
"category": { | |
"id": "100358", | |
"name": "Unlocked Cell Phones" | |
}, | |
"brand": { | |
"id": "191815", | |
"name": "Apple" | |
}, | |
"product_code": "B075QJSQM5" | |
}, | |
{ | |
"id": "2055880", | |
"name": "Microsoft Surface Pro - Intel Core I5 8Gb Ram 256Gb", | |
"category": { | |
"id": "100368", | |
"name": "Computers - Tablets" | |
}, | |
"brand": { | |
"id": "191825", | |
"name": "Microsoft" | |
}, | |
"product_code": "B071SF41Y9" | |
}, | |
{ | |
"id": "2055881", | |
"name": "Samsung Galaxy Tab A 10.1-Inch 16 Gb", | |
"category": { | |
"id": "100368", | |
"name": "Computers - Tablets" | |
}, | |
"brand": { | |
"id": "191826", | |
"name": "Samsung" | |
}, | |
"product_code": "SM-T580NZKAXAR" | |
}, | |
{ | |
"id": "2055882", | |
"name": "Ipad With Wifi + Cellular, 32Gb, Gold", | |
"category": { | |
"id": "100368", | |
"name": "Computers - Tablets" | |
}, | |
"brand": { | |
"id": "191815", | |
"name": "Apple" | |
}, | |
"product_code": "B00M4L4NHY" | |
} | |
], | |
"nextURL": null | |
} |
Voilà! I got all the products (since I only have 4 of them) in just one request. results is a JSON array, returning the list of products up to 100 items. When you have more than 100 products, the result is paginated by 100. Therefore in every response, the API returns the next page’s URL so that you can iterate within a for a loop.
Each item in the results list represents a single product. So each product contains:
- ID
- Name
- Category (both name and category ID)
- Brand (both name and brand ID)
- Product Code (SKU, EAN or any other internal product code)
Each product on your Prisync account has several URLs including your own URL and competitor URLs. Another step we reached, we now need to retrieve those URLs.
This is easy to do. What you need to do is calling the get a particular product endpoint. Let’s call it for the “iPhone X, GSM Unlocked 5.8 – 64 GB – Space Gray“. I need the product id of it and it lays down in the /list/product request response:
“id”: “2055805” “name”: “iPhone X, GSM Unlocked 5.8 – 64 GB – Space Gray”,
Getting a particular product
Seems like we are almost there. Let’s put the ID into the URL and call make_request method:
https://gist.github.com/prisync/dfa24921f00cdb1eed478b3889fa2652
Here is the response:
{ | |
"id": "2055805", | |
"name": "iPhone X, GSM Unlocked 5.8 - 64 GB - Space Gray", | |
"category": { | |
"id": "100358", | |
"name": "Unlocked Cell Phones" | |
}, | |
"brand": { | |
"id": "191815", | |
"name": "Apple" | |
}, | |
"product_code": "B075QJSQM5", | |
"external_ref": null, | |
"product_cost": "0.00", | |
"smart_price": "no_rule_defined", | |
"urls": [ | |
"8273503", | |
"8273504", | |
"8273505", | |
"8273506" | |
] | |
} |
This endpoint returns more details about the product, such as product_cost, external_ref, smart_price and URLs. Consequently we are interested in URLs, so we’ll dive into others in next posts.
Since we want to focus on urls field let’s dig it up. It contains an array of integers, each pointing to a URL belonging to this product. In this product, there are 4 URLs whose IDs are 8273503, 8273504, 8273505 and 8273506.
Due to we have IDs of the URLs, next step is calling get a specific URL
endpoint.
Getting a Particular URL
It is just a simple for loop iterating over all URL IDs and getting URL details:
response = make_request('/get/product/id/2055805', 'get') | |
print(response.text) | |
product_details = json.loads(response.text) | |
urls = product_details['urls'] | |
for url in urls: | |
url_response = make_request('/get/url/id/{}'.format(url), 'get') | |
print(url_response.text) |
…and here is the output of the piece of code:
{ | |
"id": "8273504", | |
"url": "https://www.coolblue.nl/product/793610/apple-iphone-x-64gb-zilver.html", | |
"price": "1357.29", | |
"in_stock": 1, | |
"last_check": "2018-03-20 11:25:43", | |
"change_day": "199.30", | |
"currency": "EUR", | |
"last_changed": "20/03/2018", | |
"old_price": "1157.99" | |
} |
… (there are 3 other URL data, so I just removed them for the sake of simplicity.)
In the URL details, there is several fields and most of them self explanatory. For the details of each field, you can look up in the API docs.
Since we have all the competitor prices, it is easy to write a piece of code to find the items which got more than 10% discounts today:
response = make_request('/get/product/id/2055805', 'get') | |
print(response.text) | |
product_details = json.loads(response.text) | |
urls = product_details['urls'] | |
for url in urls: | |
url_response = make_request('/get/url/id/{}'.format(url), 'get') | |
print(url_response.text) | |
url_info = json.loads(url_response.text) | |
daily_change = float(url_info['change_day'] or 0) | |
price = float(url_info['price']) | |
# Let's do the calculation if only the price is available. | |
if price > 0: | |
if (daily_change / price * 100) > 10: | |
print("Discount alert at {}".format(url_info['url'])) |
Finally, the output is:
Discount alert at https://www.coolblue.nl/product/793610/apple-iphone-x-64gb-zilver.html
And finally we know that coolblue.nl discounted the price more than 10% today.
To sum up all the code of this post:
import json | |
import requests | |
def make_request(path, method, payload=None): | |
# Use API Key and API Token that you've copied from | |
# your settings page. | |
headers = { | |
'apikey': 'apidemo@prisync.com', | |
'apitoken': 'c18888888888888888888fa135f540', | |
} | |
# This is the root URL. All API calls are relative to this URL. | |
base_url = "https://prisync.com/api/v2" | |
# We append the path into base_url | |
url = "".join([base_url, path]) | |
# HTTP GET for getting data | |
if method == 'get': | |
result = requests.get(url, headers=headers) | |
# HTTP POST for usually creating new data and editing data. | |
elif method == 'post': | |
result = requests.post(url, data=payload, headers=headers) | |
else: | |
pass | |
return result | |
# Get all products, paginated by 100 items. | |
response = make_request('/list/product', 'get') | |
# Load all products into a Python List | |
products = json.loads(response.text) | |
# Iterate over all products one by one | |
for product in products['results']: | |
# For each product, get product details | |
r = make_request('/get/product/id/{}'.format(product['id']), 'get') | |
# Load product details into a Python object | |
product_details = json.loads(r.text) | |
# Get all URL IDs belonging to this product | |
urls = product_details['urls'] | |
# Iterate over all URLs | |
for url in urls: | |
# For every URL, get URL details | |
url_response = make_request('/get/url/id/{}'.format(url), 'get') | |
# Load URL info to a Python object | |
url_info = json.loads(url_response.text) | |
# Get daily price change from URL info. | |
daily_change = float(url_info['change_day'] or 0) | |
# Get current price of this URL | |
price = float(url_info['price']) | |
# Let's do the calculation if only the price is available. | |
if price > 0: | |
# If the price is discounted more than 5%, report it. | |
if (daily_change / price * 100) > 5: | |
print("Discount alert at {}".format(url_info['url'])) |
There are numerous things to do if you have access to the competitor prices, which Prisync can proudly present. Just to mention a few of them:
- First of all you can monitor your competitors actively and generate custom alerts depending on your pricing strategy.
- in addition to that, set your prices when the market is stale so that you can create volatility, which directly affects your profit margins.
- Another feature you can develop is ignoring discounts if the stock is not available on the competitor.
- in addition, you can implement other ideas that you and your team come up with!
Thanks for following up to this point. This is the second post of our API article series. See you in next posts of Prisync API Tutorial Series.
prisync