This is an old revision of the document!
Python
Below is the most basic thing you will need for starting API requests. Note: Currently this is only in python, if you are using something like javascript you will have to adapt this code to that language.
If you are interested in tying this in with a discord bot, you can check Fivemans Bot Github for examples, and ways to use the api. Note: It was written in python
Another Note: This is all for python 3.8.3+, I have no idea if this will work for earlier versions, or even later versions in some cases
Basic interacting with API
import requests # requests is a library that allows us to interact with a site api_key = "api_key_goes_here" # variable name, makes things easier headers = {"api-key": api_key} # headers are where you store the api-key for this API, this can be different for other sites or games API, make sure to read the documentation about it params = { 'game': 1, # these are the parameters, almost all api requests will need some sort of parameters to work, for this request we only need to specify which game we want } # note: the games and styles are all integer values, check https://wiki.strafes.net/api for more info about these response = requests.get("https://api.strafes.net/v1/map", headers=headers, params=params) # the actual part that will interact with the site r = response.json() # this will make the response readable, if you do not add this it will only display the response code. Ex: <response: [200]> if the request was valid
API Requests with user input
import requests from pprint import pprint # this is just to make print look nice, not needed api_key = "api_key_goes_here" headers = {"api-key": api_key} style = input("What style would you like to use?") game = input("What game would you like to use?") params = { 'style': style 'game': game 'whitelist': True # theres a few cases where players with default status can get into this request with cheated times, so this is to prevent that } response = requests.get("https://api.strafes.net/v1/time/recent/wr", headers=headers, params=params) r = response.json() pprint(r)
Iterating API Requests
So you need to sort through the response the API gives you, heres one way, it might not be the best but it will work.
On a side note the API response will look something like this
[{'Date': 1604778657, 'Game': 1, 'ID': 4821514, 'Map': 5692118131, 'Mode': 1, 'Style': 1, 'Time': 22220, 'User': 1146744574}, {'Date': 1604778582, 'Game': 1, 'ID': 2619547, 'Map': 5692145037, 'Mode': 1, 'Style': 1, 'Time': 25960, 'User': 605153371}]
The times are Not stored in seconds, they are the complete number, so 25960 will be 25.960
The 'ID' value given is not really used or needed for anything, every time will give you an ID with it but it is not needed unless requesting a specific time with its ID from the API
for i in r: # looping through each dictionary in the reponse # say we need the user id, time, style, and map user_id = i['User'] time = i['Time'] style = i['Style'] map_id = i['Map'] # these print statements will work the same, its just whichever you like best print(f'User is:{user_id}, time is:{time}, style is:{style}, map is:{map_id}') print('User is:{}, time is:{}, style is:{}, map is:{}'.format(user_id, time, style, map_id))