python
Below are example get and post requests using python.
Note the error check: a failed request comes back as 200 with an error key in the body, so testing status_code alone is not enough. See Errors.
Request
GET
import requests
# Define the headers
headers = {
"X-API-KEY": "YOUR_API_KEY"
}
# Define the URL you want to send the GET request to
url = "https://api.srating.io/v1/game/getGames?start_date=2023-04-01&organization_id=f1c37c98-3b4c-11ef-94bc-2a93761010b8&division_id=bf602dc4-3b4a-11ef-94bc-2a93761010b8"
# Send the GET request with the headers
response = requests.get(url, headers=headers)
if response.status_code != 200:
print(f"Request failed with status code: {response.status_code}")
else:
games = response.json()
# Errors come back as 200 with an "error" key, so check the body too
if isinstance(games, dict) and "error" in games:
print(f"API error: {games['error']}")
else:
print(f"{len(games)} games")POST
import requests
# Define the headers
headers = {
"X-API-KEY": "YOUR_API_KEY"
}
# Define the URL you want to send the POST request to
url = "https://api.srating.io/v1/"
# Define the data to be sent in the request body
data = {
"class": "game",
"function": "getGames",
"arguments": {
"organization_id": "f1c37c98-3b4c-11ef-94bc-2a93761010b8",
"division_id": "bf602dc4-3b4a-11ef-94bc-2a93761010b8",
"start_date": "2023-04-01"
}
}
# Send the POST request with the headers and data
response = requests.post(url, headers=headers, json=data)
if response.status_code != 200:
print(f"Request failed with status code: {response.status_code}")
else:
games = response.json()
if isinstance(games, dict) and "error" in games:
print(f"API error: {games['error']}")
else:
print(f"{len(games)} games")Tracking your usage
Every response carries your usage in the headers, so you can log what a request cost:
print(response.headers.get("x-request-usage"), "tokens for this request")
print(response.headers.get("x-usage-remaining"), "remaining this month")See Usage tokens for how the cost is calculated.