Requesting all Trading post items from API — Guild Wars 2 Forums

Requesting all Trading post items from API

What is the proper way to receive all the items on the Trading post via the API? I have sent a request to " https://api.guildwars2.com/v2/commerce/listings/ " but it doesn't seem to give the response in the right format? ( i save it as a string but i cant convert it afterwards... . single ids work fine) Maybe i am doing something wrong or do i actually have to somehow find all the id's for all the items and then request every item individually? Or at least request 200 at a time since i read that that is the max the server responds to or something.

Comments

  • Eearslya.6309Eearslya.6309 Member ✭✭

    You're almost there. /v2/commerce/listings (or /v2/commerce/prices) is actually a list of item IDs, so you can already get a list of the items on the trading post right there.
    If you want more, it depends on what kind of data you're looking for. Do you only want a quick overview of the items on the trading post, or do you care about seeing each individual listing for each item?

    https://api.guildwars2.com/v2/commerce/prices?page=1&page_size=200 for a quick overview (shows the lowest buy cost and highest sell cost)
    https://api.guildwars2.com/v2/commerce/listings?page=1&page_size=200 for all the details (every individual buy/sell listing)

    These requests will give you the items, 200 at a time, and you'll have to increment the page number with each request. The API returns an HTTP Header "X-Page-Total" that will tell you how many total pages there are to go through.

  • Archomeda.6472Archomeda.6472 Member ✭✭

    Just note that by requesting the endpoint by using the page parameter, you'll have to start at 0 not 1.

  • Lumina.9648Lumina.9648 Member ✭✭

    Thanks a lot got all the info i needed :)

  • Lumina.9648Lumina.9648 Member ✭✭

    Alright now i have the problem that i only have the item ID's but i would like to show the names of the items. Is there a fast way to get the item name by id?

  • Archomeda.6472Archomeda.6472 Member ✭✭
    edited May 10, 2018

    Item information is located at https://api.guildwars2.com/v2/items. You can use ?page=<page_num> on this endpoint as well (what @Eearslya.6309 explained for the commerce endpoint), or you can use the ?ids=<max_200_comma_separated_item_ids> parameter.

  • Lumina.9648Lumina.9648 Member ✭✭

    so since https://api.guildwars2.com/v2/items doesn't return a page total how would i be able to get all the items? I want to store all the items in a file for the program to read the names from by ID.

  • Eearslya.6309Eearslya.6309 Member ✭✭

    If you request https://api.guildwars2.com/v2/items?page=0&page_size=200 it will return a page total.
    Or, the alternative solution is to use all of the IDs that are given to you on that request, break them into groups of 200, and request them that way.
    i.e. https://api.guildwars2.com/v2/items?ids=1,2,3,4,5,6,7,8,9,...

  • Lumina.9648Lumina.9648 Member ✭✭

    I thought i tried https://api.guildwars2.com/v2/items?page=0&page_size=200 but i must have had a typo in there... worked now thanks for all the help so far :D

  • Lumina.9648Lumina.9648 Member ✭✭

    Another dumb question probably.. do i actually have to make a class that can save all the data that i get from the https://api.guildwars2.com/v2/items call or is there a way to only get the name? :D I really don't want to create a class for that lol. I thought i would be enough to just save all the items to a txt file and read the Name by ID from there but im actually not really sure how to do that.

  • SlippyCheeze.5483SlippyCheeze.5483 Member ✭✭✭

    @Lumina.9648 said:
    Another dumb question probably.. do i actually have to make a class that can save all the data that i get from the https://api.guildwars2.com/v2/items call or is there a way to only get the name? :D I really don't want to create a class for that lol. I thought i would be enough to just save all the items to a txt file and read the Name by ID from there but im actually not really sure how to do that.

    Yes, you absolutely, positively should cache data you retrieve from the GW2 API appropriately. It is a very, very bad idea not to do that.

    It doesn't matter at all how you choose to implement that on your side, however. You can use a class, or procedural code, or functional code, and store it in whatever you wish, including flat files, databases, or even in ping packets sent around the local network, if that tickles your fancy.

  • Eearslya.6309Eearslya.6309 Member ✭✭

    @Lumina.9648 said:
    Another dumb question probably.. do i actually have to make a class that can save all the data that i get from the https://api.guildwars2.com/v2/items call or is there a way to only get the name? :D I really don't want to create a class for that lol. I thought i would be enough to just save all the items to a txt file and read the Name by ID from there but im actually not really sure how to do that.

    Are you trying to work with the raw text? You should try and find a proper JSON parsing library for whatever language you're doing this in. That way, you'll be able to easily pick and choose which parts you take out of the API's response.

  • Lumina.9648Lumina.9648 Member ✭✭

    @Eearslya.6309 said:

    @Lumina.9648 said:
    Another dumb question probably.. do i actually have to make a class that can save all the data that i get from the https://api.guildwars2.com/v2/items call or is there a way to only get the name? :D I really don't want to create a class for that lol. I thought i would be enough to just save all the items to a txt file and read the Name by ID from there but im actually not really sure how to do that.

    Are you trying to work with the raw text? You should try and find a proper JSON parsing library for whatever language you're doing this in. That way, you'll be able to easily pick and choose which parts you take out of the API's response.

    Well i am doing my project in C# and creating a windows application with WPF and am using "Newtonsoft.Json". I saved the JSON Respons for all the items in a text file and wanted to read the name of the item based on the ID i got from the Trading post request. But not sure if that works the way i wanted it to. My programming knowledge is pretty basic i would say so i probably don't have the best approach to things :D which is why i ask all these question.

  • Eearslya.6309Eearslya.6309 Member ✭✭

    Aha, now I understand why you were saying you had to create a class.. Boy, that JSON library is a little heavy-handed. But it looks like you should be able to get what you want with a little trick. You can create an Item class, and only put in one member, just a string for name. Then, when you're deserializing through Newtonsoft.JSON, you can do something like this (forgive me if I mess up, I don't do C#..):

    try {
        Item myItem = JsonConvert.DeserializeObject<Item>(json, new JsonSerializerSettings {
            MissingMemberHandling = MissingMemberHandling.Ignore
        });
    }
    catch (JsonSerializationException ex) {
        Console.WriteLine(ex.Message);
    }
    

    The MissingMemberHandling.Ignore part will tell Newtonsoft to simply ignore if the JSON has more data than the class can handle.

  • Lumina.9648Lumina.9648 Member ✭✭

    So after a bit of messing around and reorganizing my code it looks like i am properly saving everything to a text file now. Thanks a lot for all the help already @Eearslya.6309 , @SlippyCheeze.5483 and @Archomeda.6472.

  • Lumina.9648Lumina.9648 Member ✭✭

    Is it normal that it takes up to 6 minutes to get all the listings from https://api.guildwars2.com/v2/commerce/listings ? Because i feel like am doing something wrong .

  • Eearslya.6309Eearslya.6309 Member ✭✭

    If you're doing every request one at a time, it's entirely possible. I would recommend you find a good threading library and download multiple pages of results simultaneously.

  • Lumina.9648Lumina.9648 Member ✭✭

    Is there anywhere i can check how many items there are total for my requests? E.g. i request "https://api.guildwars2.com/v2/commerce/listings?page=0" and read the total page number from the header. Then i request every page ("https://api.guildwars2.com/v2/commerce/listings?page=" + i + "&page_size=200" ). and i would like to check if i actually get all the items. For this example i usually get around 24k.

  • Eearslya.6309Eearslya.6309 Member ✭✭

    Yep! Just like there is a "X-Page-Total" header, there is also a header for "X-Result-Total". This will tell you the total amount of things there are on that endpoint (items, listings, skins, etc.). Not to be confused with "X-Result-Count" which only tells you how many things it returned with that single request.

©2010–2018 ArenaNet, LLC. All rights reserved. Guild Wars, Guild Wars 2, Heart of Thorns, Guild Wars 2: Path of Fire, ArenaNet, NCSOFT, the Interlocking NC Logo, and all associated logos and designs are trademarks or registered trademarks of NCSOFT Corporation. All other trademarks are the property of their respective owners.