1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Legion TD 2 API - V2 (Beta)

Discussion in 'Announcements' started by EpvpDani, May 25, 2020.

  1. EpvpDani

    EpvpDani Developer Staff Member

    Messages:
    776
    Likes Received:
    253
    Trophy Points:
    63
    Hello everyone!
    Today we are announcing that we will re-launch our game data API with a new version next week (June 2nd). The previous version will be deprecated/discontinued due to some breaking changes.

    1. Introduction

    The API enables developers to use specific data from game sessions in their applications.
    An example would be something like an external match history, different profile stats, average unit performance, custom leaderboards or other similiar features.

    A great example for the API is LTDStats.com (made by @GvR Mr Mister)

    Initially the API launched in 2018 in an alpha status but didn't have a high priority and was developed externally by one of our players.
    Now with the v2 revamp it got taken over from us and hopefully it will receive more regular updates and fixes as needed.

    If you have specific feature requests for the API, please head to API Feature Requests

    2. Requirements

    To use the API you need to request an API key at legiontd2.com/api
    Once we (manually) reviewed your request you will be granted a key (that you shouldn't share with anyone)

    Our API uses GraphQL to process API requests. This means you first have to get familiar with GraphQL query syntax.
    This thread features a small introduction to GraphQL and will help you understand the basics (it's really easy).

    3. Getting started

    Now to the interesting part: What do you need to get started with the API?

    The easiest way to build an API query is to visit portal.legiontd2.com and explore the schema on the right site
    [​IMG]

    The schema explorer shows all available query types. Each type contains different fields.

    When you click on the individual types it should also explain the fields and some comments contain examples to make usage easier.
    [​IMG]

    Now that we know which queries are available we can write our first query. Let's start with something simple like requesting the ID of a player

    To query a specific player we can pass an argument to the player type like this:

    Code:
    query{
      player(name:"Lisk"){
          id
      }
    }
    Now we should have received exactly one result with the ID of the requested player.

    Next we can add some more fields to our query. Let's try to get the 5 last games of our requested player including all players of the match and the end result of each game:

    Code:
    query{
      player(name:"Lisk"){
        id,
        games(limit:5){
          games{
            id,
            endingWave,
            gameDetails{
              gameResult,
              playerProfile{
                  name
              }
            }
          }
        }
      }
    }
    This should return the last 5 games of the requested player with the gameid, each player that was in the game and their game result (won/lost).
    You can add any fields you want and the API will always only return the fields you specified in your query.

    4. Using the API

    Now that we know the basics we can move to the actual API usage.

    To make API calls you have to hit the endpoint api.legiontd2.com via a POST request. (Most development environments should have some library to do HTTP requests)

    The request header should contain following information:

    "Content-Type" => "application/json"
    "x-api-key" => [your_api_key]

    The content body should contain a json with your query:

    Code:
    {"query":"query{player(name:\"jules\"){id,games(limit: 10){count,games{id,version,queueType,ts,gameDetails{playerProfile{id,name},gameResult,overallElo,isCross}}}}}"}
    The current API limit is 200 requests per second and 10.000 total requests per day. If you need a higher limit, please reach out to us.
     
    GvR Mr Mister and Jules like this.