A first dalliance with Splinterlands API V2

avatar
(Edited)

Before we begin, maybe we should explain the Application Programming Interface (API) acronym and what it means.

An interface allows you to communicate (interface) with something so an API allows us to communicate / interface programatically with an application.

Looking at this from the Splinterlands perspective, the Splinterlands API allows you to programatically communicate with Splinterlands and request information or change Splinterlands settings to effect the game. This allows automation of tasks, like claiming the SPS airdrop and Staking it, or starting a Ranked Battle and submitting a team.

Generally there is a Private side to the API which would require account authentication (supplying the account name and password). This would generally allow the changing or viewing account specific information that is not generally publicly available, like sending DEC or SPS to other players, buying or selling cards etc.
As this is more in depth, it will not be covered in this article.

There would also be a Public side to the API where publicly avaialble information can be accessed via the API without authentication. Examples would be a specified accounts battle data or card collection.

So if you are not a developer, is this any use to you?

If you can copy and paste a URL into a web browser then it could well be!

To view the Publicly available match data for @onw's (my) account the following URL can be used:

https://api.splinterlands.io/battle/history?player=onw

If you want to see this data for your account change 'onw' at the end of the URL to your account name and resubmit it.

This will fill the webpage with a chronological list of the last 50 battles for the specified account, with the most recent battle at the top of the web page.

Alternatively To view @l8n's (My rentals account) cards, if you copy and paste the following URL into the address on a browser:

https://api.splinterlands.io/cards/collection/l8n

This will return the card collection available to account 'l8n'.

Again, substituting 'l8n' with your account name will list your card details on the webpage in the browser.

If you take a closer look at the returned data, you will see there is a logical structure to repetetively supply the information for each battle or card relevant to the specified account.

Pretty simple eh,

The more advanced way of submitting these URL's to Splinterlands API would be by using Python 3 and BEEM to submit the URL to Splinterlands instead of using a web browser.

There are other articles on Hive for installing Python 3 and Beem, so I won't delve into that here.

The following code (Python commands) could be used to access the most recent 50 battles for the @onw account from Splinterlands if you have Python 3 and Beem installed on your computer.

The following commands give Python access to json and urlopen command libraries

A bit like expanding your abilities as if you learned a new language like French or Spanish, this enables Python to know JSON and urllib

import json
import urllib
from urllib.request import urlopen

The next two lines place relevant information into their respective variables

A variable is like a storage box that contains information that can change (hence it is variable) and the identifier of the variable is written on the box, so lastbattles_url is written on the box and https://api.splinterlands.io/battle/history?player= is stored inside that box.

lastbattles_url = "https://api.splinterlands.io/battle/history?player="

Set the Hive User ID so we can view that accounts card details

hiveuserid = "onw"

Join "onw" which is stored in hiveuserid to the end of the lastbattles_url and place the value in a new box with url written on it

url = lastbattles_url + hiveuserid

urlopen(url) sends the value stored in the url box to Splinterlands like when you copied and pasted the URL into the browser earlier and submitted it

However instead of the returned information being displyed in the Browser it is now stored in a variable called response

response = urlopen(url)

We can now do things with the data in the response variable

The next line performs a task on the data in the 'response' variable and the resulting processed data is stored in another variable called 'data'

data = response.read().decode("utf-8")

We can display the URL with the next command

print (url)

The next line displays the contents of the data variable which contains the list of battle details

print (data)

The following code (Python commands) could be used to access the card details for the @l8n account from Splinterlands if you have Python 3 and Beem installed on your computer.

import json
import urllib
from urllib.request import urlopen

cardcollection_url = "https://api.splinterlands.io/cards/collection/"

hiveuserid = "l8n"

url = cardcollection_url + hiveuserid

response = urlopen(url)

data = response.read().decode("utf-8")

print (url)

print (data)

Well I hope that brief and basic introduction to API programming was informative and made sense.

I have a use in mind for this knowledge, but I will have to learn more before I can realise this objective.

@onw



0
0
0.000
3 comments
avatar

Congratulations @onw! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You distributed more than 400 upvotes.
Your next target is to reach 500 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Check out the last post from @hivebuzz:

Hive Power Up Month - Feedback from May - Day 15
Support the HiveBuzz project. Vote for our proposal!
0
0
0.000
avatar

Very raw data that, but it works after a change.

print (url)

Python is case sensitive, and url needs to be lower case or it errors.

0
0
0.000
avatar

Thanks for the test run and feedback, appreciate it.

0
0
0.000