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

Another Note: “#” is a comment in python, this allows you to write anything without it being executed when running the file

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))

How do I get a map name from its id?

Im gonna straight up steal this from fivemans bot, since hes already made it and it works very very well

Theres 2 parts to this, first one will make the map files, second will give you a way to get the maps name, and creators if you wish

Credit: Fivemans Bot Github

import json
import requests
 
API_KEY = "api_key_goes_here"
URL = "https://api.strafes.net/v1/"
 
headers = {
    "api-key":API_KEY,
}
 
# function to request from the API without having to write the same lines every time
 
def get(end_of_url, params): 
    res = requests.get(URL + end_of_url, headers=headers, params=params)
    if not res:
        print(res)
        print(res.text)
        raise Exception("request failed")
    else:
        return res
# this will pass in the API request we wanna invoke, in this case "map", and we pass the parameters in with it
bhop_maps = get("map", {
    "game":"1", 
    "page":"1" # note that alot of api requests that will give out 200 or more results will be paged, take this into account when using certain requests
}) 
total_pages = int(bhop_maps.headers["Pagination-Count"]) # cool page counting stuff to make sure we get every map
bhop_map_data = bhop_maps.json() 
for i in range(2, total_pages + 1): # looping through all the pages we need to see, in this case 2, (might need to be changed when maps get added again)
    m = get("map", {
        "game":"1",
        "page":str(i)
    })
    bhop_map_data = bhop_map_data + m.json() # adding our newly requested maps to the other list
 
# "with open()" will always close the file after its done using it, although it wont change anything to close it after just in case
with open("bhop_maps.json", "w") as file: # this will create a file if you dont already have one named this (so dont worry about making one)
    json.dump(bhop_map_data, file) # then it will dump the dictionary of maps we made into this file
file.close() # fiveman dumb and closes the file anyway
 
 
# this is the exact same stuff just for surf instead of bhop maps
surf_maps = get("map", {
    "game":"2",
    "page":"1"
})
total_pages = int(surf_maps.headers["Pagination-Count"])
surf_map_data = surf_maps.json()
for i in range(2, total_pages + 1):
    m = get("map", {
        "game":"2",
        "page":str(i)
    })
    surf_map_data = surf_map_data + m.json()
 
with open("surf_maps.json", "w") as file:
    json.dump(surf_map_data, file)
file.close()

Heres how we will get the names of the maps in the 2 new files we just made, also stolen from fivemans bot since im lazy

Credit: Fivemans Bot Github

bhop_maps = {}
with open("bhop_maps.json") as file: # this will open the file and read it then put the data into the bhop_maps variable
    data = file.read()
    bhop_maps = json.loads(data)
 
surf_maps = {}
with open("surf_maps.json") as file: # this does the same but with surf
    data = file.read()
    surf_maps = json.loads(data)
 
def map_name_from_id(map_id, game): # heres the function that will give us the map name
    game = game # this specifies game we passed in when invoking the function
    if game == 1: # simple checking which game we specified
        map_data = bhop_maps
    elif game == 2:
        map_data = surf_maps
    else: 
        raise Exception("Invalid game") # raises an error if we dont give it a valid game id
    for m in map_data: # lopping through the map info in the maps file
        if m["ID"] == map_id: # checking if the current id of the map we are looking at is the same as the one we passed into the function
            return m["DisplayName"] # if it is, it returns the display name of the map
    return "Map name not found" # if it finishes the loop and doesnt find any maps with that id, it will simply return "map not found"
 
# Example of how to invoke this function
 
map_name = map_name_from_id(map_id, game_id)
print(map_name)
Navigation