Marketplaces Scripts
OnBuy API
12min
introduction this section of the marketplaces documentation will explain how we utilize the onbuy api to list products for clients the onbuy api is based on the rest architecture you can find more information about the api here https //docs api onbuy com/#intro i will provide an overview of how the code operates, and the process we adhere to ensure products are listed seamlessly code overview authentication to access the onbuy api, you need to obtain authentication keys (secret and consumer key) from your client's onbuy api once you have these keys, you can fetch an access token and start interacting with the api url = "https //api onbuy com/v2/auth/request token" payload = {'secret key' secret key, 'consumer key' consumer key} headers = { 'content type' 'application/x www form urlencoded' } response = requests post(url, headers=headers, data=payload, timeout=30) json() access token str = response\['access token'] return access token products the onbuy marketplace operates as a catalogue, which means that an item belonging to another client cannot be listed twice however, if an item is already listed, a client can list the same item under the original seller’s listing new products can be listed directly on the marketplace, provided that their ean does not already exist on the onbuy marketplace this ensures that there are no duplicate listings for the same product the process we use to list products on onbuy marketplace is get products 1 fetch the products from our database that are to be listed on onbuy using the get listing data method which we have looked at in the handling marketplaces data and ftp operations docid 5kvrxzan8gdvckqv4pl l 2 retrieve all the products currently listed on the client’s account for this step we use the endpoint https //api onbuy com/v2/listings the get products function is designed to fetch the list of products asynchronously as this endpoint is paginated and fetching the proucst synchronously will be a slow process after the products are fetched they are save to a csv file more explantation on this endpoint https //docs api onbuy com/#356663d3 57e5 4a55 aa3c a2c482265bcb data = \[] has more data = true while has more data urls = \[url + "listings?site id={}\&limit={}\&offset={}" format( site id, limit, offset + i limit) for i in range(7)] responses = await asyncio gather( (fetch with retry(url, headers) for url in urls), return exceptions=true) for response in responses response data = response json() data extend(response data\['results']) has more data = response data\[ 'metadata']\['total rows'] > offset + limit offset += 7 limit return data note this just a code snippet showing how the function works, you can the see real implementation of the code in our github repository 3 compare these products with our data and the data obtained from onbuy to identify new and old products we save the data from our database and onbuy into csv files and use the pandas library to analyze new and old products since the sku serves as the common identifier, it is the basis for our comparison as a result of this comparison, we generate two files one that contains the data already listed in the client's account and another that comprises data not yet existing in the client's account linns df = pd read csv(linns filename) onbuy df = pd read csv(onbuy filename) linns df\['sku'] = linns df\['sku'] onbuy df\['sku'] = onbuy df\['sku'] common skus = pd merge(linns df, onbuy df, how='inner', on='sku') linns only skus = linns df\[ linns df sku isin(onbuy df sku)] common skus to csv(listed filename, index=false) linns only skus to csv(new filename, index=false) 4 perform an ean check on onbuy to verify if the ean of these products is already in use for this we use the endpoint https //api onbuy com/v2/products the get existing opc function is designed to asynchronously retrieve existing ean data and add it to a dataframe it takes advantage of asynchronous programming to efficiently fetch and process a large amount of data more explantation on this endpoint https //docs api onbuy com/#67862c5f 841a 4d0b 9869 76da5f147c02 url = url + "products" new df\['opc'] = '' tasks = \[] for i, row in new df iterrows() if pd isnull(row\['ean']) continue params = { "site id" 2000, "filter\[field]" "product code", "filter\[query]" int(row\["ean"]) } tasks append( fetch with retry(url + '?' + '&' join(\[f'{k}={v}' for k, v in params items()]), headers)) to run both the get products and get product opc we create separate functions in the onbuy main py module to run them in order to rtetrieve the data \ get already listed barcodes this function runs the get existing opc function and creates files from the data returned from the function this function creates a file containing new products that are not yet listed on onbuy and another file containing products that are already listed on onbuy but not on the client’s account def get already listed barcodes() > none df = asyncio run(get existing opc(config base url, config headers, config new filename)) df to csv(config new filename, index=false) opc df = df\[df\['opc'] != ''] opc df to csv(config opc filename, index=false) \ get onbuy products this function is used to run the get products function and create a file from the data returned def get onbuy products() > none data = asyncio run(get products(config base url, config headers)) df = pd dataframe(data) df to csv(config onbuy filename, index=false) create products this part explains how the new products and already listed products are listed to the onbuy marketplace 1 new products to do this in the api we use the endpoint https //api onbuy com/v2/products the create new products function is designed to list new products on onbuy using data from a csv file it processes the product data in the csv file, generates the required payload, and sends the data to the onbuy api for listing more explantation on this endpoint https //docs api onbuy com/#00f5cff1 19ea 4564 89a8 16d50ca1ab7d for parent sku, product in products dict items() payload = { product\['product info'], "variants" product\['variants'] } if payload and any(payload values()) response = send request('post', url, payload, headers) new log info("response for create new products is \n%s\nsku %s", json dumps(response, indent=4), row\['parentsku']) else new log info("payload is empty for new products no new products") example usage create new products(url, headers, "new products csv") 2 already listed products we list products under sellers who already have listed the product on onbuy we use the endpoint https //api onbuy com/v2/listings the process product listings function is designed to interact with product listings on onbuy by reading data from a csv file and performing either updates or creating new listings, depending on the specified action more explantation on this endpoint https //docs api onbuy com/#a689b83a eb1a 4a24 979f 3fd15f78eee2 for parent sku, listings in products dict items() payload = { "site id" 2000, "listings" listings } if action == 'update' response = send request('put', url, payload, headers) update log info("response for process product listings is \n%s", json dumps(response, indent=4)) elif action == 'create' if payload and any(payload values()) response = send request('post', url, payload, headers) opc log info("response for process product listings is \n%s", json dumps(response, indent=4)) else opc log info("payload is empty for opc products") else raise valueerror("invalid action specified") example usage process product listings(url, headers, "product data csv", 'create', 'listings') this doc provides a general overview of how to interact with the onbuy api for both retrieving and listing products main module the main module is used to combine different methods for execution and scheduling it ensures the smooth flow of the listing process def main() if not os path exists(config onbuy file dir) os makedirs(config onbuy file dir) get listing data('onbuy', product sql, config linns filename) get onbuy products() get listing files(config linns filename, config onbuy filename, config listed filename, config new filename) get already listed barcodes() create new products(config base url, config headers, config new filename) process product listings(config base url, config headers, config opc filename, 'create', 'listings') conclusion to understand the process better go through the onbuy documentaion and also the code in the repository this present a good understanding of the listing process