Have you ever wondered where websites like gucards have data about marketplace that powers theirs website? Immutable X actually exposes an API that lets anyone fetch it. I have used it to create some of the datasets that I previously pasted on this sites such as: Comparison of mint cost of core cards with their market price or Price comparison of Gods Unchained cards in ETH/Gods. I wanted to present some cool examples of this api usage with examples of code in python. Today, I am going to show you how to get a list of card listings.
<h3>Getting cards listed for sale. <p dir="auto"><br /> (you can actually click at the link to see the type of response we get.)<br /> You will see something like this.<br /> <img src="https://images.hive.blog/768x0/https://files.peakd.com/file/peakd-hive/matimath/23t756cvHNL6qWVJoSgRwbCk7qx6NxHryGj84WU1VxW6EZNM6ehKF5WuS7dQ9g3cXbbZX.PNG" alt="Capture.PNG" srcset="https://images.hive.blog/768x0/https://files.peakd.com/file/peakd-hive/matimath/23t756cvHNL6qWVJoSgRwbCk7qx6NxHryGj84WU1VxW6EZNM6ehKF5WuS7dQ9g3cXbbZX.PNG 1x, https://images.hive.blog/1536x0/https://files.peakd.com/file/peakd-hive/matimath/23t756cvHNL6qWVJoSgRwbCk7qx6NxHryGj84WU1VxW6EZNM6ehKF5WuS7dQ9g3cXbbZX.PNG 2x" /><span>In order to get list of cards listed for sale, we need to send a get request to the following endpoint: <a href="https://api.x.immutable.com/v1/orders" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://api.x.immutable.com/v1/orders <p dir="auto">As a result we get a response in JSON format containing 100 latest listings. Each listed trade will contain - among others - buy and sell sections. Sell section specifies the type of token that is being offered, whereas buy section specifies the kind of token user wants in return. The name of the offered card will be listed under the properties section. When it comes to sell section, it specifies the type of currency and requested amount. Specified amount should be divided by normalization factor (in case of $GODS and ETH 10^18). The type of cryptocurrency required is specified by token_adress according to the table below: <div class="table-responsive"><table> <thead> <tr><th>adress<th>crypto <tbody> <tr><td>0x9ab7bb7fdc60f4357ecfef43986818a2a3569c62<td>GOG <tr><td>0xf57e7e7c23978c3caec3c3548e3d615c346e79ff<td>IMX <tr><td>0xccc8cb5229b0ac8069c51fd58367fd1e622afd97<td>GODS <tr><td>0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48<td>USD <tr><td>0xed35af169af46a02ee13b9d79eb57d6d68c1749e<td>OMI <tr><td>0xacb3c6a43d15b907e8433077b6d38ae40936fe2c<td>GU <p dir="auto">There are 2 caveats to the above. First of all, the list we get will specify both the active listings, as well as those that are not active. To get around that we just need to specify "status=active" as part of our get call. The other issue is that we only get 100 listings. We can actually change that amount by specifying the page_size argument, but unfortunately maximal accepted value is 200. How to get around that? Immutable X API uses mechanism called pagination. After providing us with 100 latest listings, it also provides us with pointer called "cursor" as part of endpoint response. This cursor actually points to the earliest item we have not been given in our previous request. If we provide this pointer in our next API call, we will get our next 100 items starting from that one. So all we need to do is to always provide the last cursor we got until we get all of the listings. <p dir="auto">The following code snippet enables you to fetch the data from an API and puts them at all_active_orders list. <pre><code>import datetime import json import requests all_active_orders = [] all_trades_ids = set() headers = {"Accept": "*/*"} url = f"https://api.x.immutable.com/v1/orders?page_size=200&status=active" while True: response = requests.request("GET", url, headers=headers) response = json.loads(response.content) all_active_orders.extend(response['result']) cursor = response['cursor'] new_trade_ids = {x['order_id'] for x in response['result']} if len(all_trades_ids.intersection(new_trade_ids)) > 0: break all_trades_ids = all_trades_ids.union(new_trade_ids) url = f"https://api.x.immutable.com/v1/orders?page_size=200&status=active&cursor={cursor}" <p dir="auto">Let me know your thoughts on the tutorial. I'd be happy to provide information about other endpoints present in Immutable X API.
Neato, I've tried looking at the IMX docs before but didn't try to build anything yet since I wasn't sure of a few parts like how card metadata was supposed to be fetched etc. Thanks for breaking it down, it seems quite manageable when you put it this way! 😄
Congratulations @matimath! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):
<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@matimath/upvotes.png?202203040520" /><td>You distributed more than 50 upvotes.<br />Your next target is to reach 100 upvotes. <tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@matimath/upvoted.png?202203040520" /><td>You received more than 500 upvotes.<br />Your next target is to reach 600 upvotes. <p dir="auto"><sub><em>You can view your badges on <a href="https://hivebuzz.me/@matimath" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">your board and compare yourself to others in the <a href="https://hivebuzz.me/ranking" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Ranking<br /> <sub><em>If you no longer want to receive notifications, reply to this comment with the word <code>STOP <p dir="auto">To support your work, I also upvoted your post! <p dir="auto"><strong><span>Check out the last post from <a href="/@hivebuzz">@hivebuzz: <table><tr><td><a href="/hive-122221/@hivebuzz/pum-202203"><img src="https://images.hive.blog/64x128/https://i.imgur.com/M9RD8KS.png" /><td><a href="/hive-122221/@hivebuzz/pum-202203">Today is the beginning of a new Hive Power Up Month!<tr><td><a href="/hive-122221/@hivebuzz/pud-202203"><img src="https://images.hive.blog/64x128/https://i.imgur.com/805FIIt.jpg" /><td><a href="/hive-122221/@hivebuzz/pud-202203">Hive Power Up Day - March 1st 2022 <h6>Support the HiveBuzz project. <a href="https://hivesigner.com/sign/update_proposal_votes?proposal_ids=%5B%22199%22%5D&approve=true" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Vote for <a href="https://peakd.com/me/proposals/199" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">our proposal!That's awsome!
Nice one! You are very knowledgeable about this subject.
Thanks, part 2 in preparation.