Querying The Riot API with Python: A Simple Introduction

Introduction to using the Riot API in Python Part 3, Getting Player Profile Data in Python

TheConnoisseur
6 min readSep 16, 2023

This is part 3 of a tutorial series where I walkthrough creating a mini project using the Riot API in Python.

Here is a link to the first part of the series if you want to go from the beginning: Using The Riot API with Python. Part 1, Defining our mini project and… | by TheConnoisseur | Sep, 2023 | Medium

Welcome to the 3rd part of this small tutorial series on using the Riot API in Python.

In this segment, we’re going to get into the exciting part of the mini project: the programming.

The Structure of Our Mini Project

For this mini project, I’m going to assume you have an idea of how to create and run a Python script as well as install dependencies using pip.

Additionally, you can also setup and virtual environment and requirements.txt if you wish to, but this is totally optional and you shouldn’t worry if you don’t.

Use whatever IDE you want, but if you’re interested I’ll be using PyCharm.

The final structure of this mini project will comprise of 3 main Python files:

  • settings.py, holds information like the API key
  • helpers.py, holds functions used to access the API
  • __main__.py, file which is run to execute script

So, go into the root of your project and create these 3 Python files. They can be blank for now.

As you can see in the screenshot below, I have also included a requirements.txt and venv in my project but you do not have to.

File structure for our mini project

Additionally, we will be using the Python Requests Module to make queries to the API through the web.

If you are unfamiliar with this module, don’t worry we’ll only be using the basic features of it.

To install the requests module, run this pip command in the base of your project:

pip install requests

Adding to Settings.py File

First, let’s place our API key in the settings.py file:

This centralises the input of your API key to one place so you can easily change it when you need to.

Additionally, I have added two constants, DEFAULT_REGION_CODE and DEFAULT_REGION, which we can use as placeholders in our script in case our user doesn’t enter a region to place their query against.

Generally, it’s useful to have a settings.py file in most of your projects to hold constants and widely accesses variables, like an API key.

Creating Our Script

Let’s code out retrieving a player’s profile data from the API in Python. We’ll do this inside a function in our helpers.py file.

First, import your settings.py file and the requests module:

We will also import urlencode to format the URL neatly for making requests to the API.

Our function signature will look like this:

We have included a docstring to outline what the function will do. It’s a good habit to do this, even in solo projects, as it helps you keep track of what a specific bits of code do.

The function takes two optional parameters:

  • summoner_name, if not passed in we will ask the user for input inside the function instead
  • region, if not passed in then it will be set to the default region specified in our settings.py file

Next, let’s get that summoner_name from the user if it wasn’t passed in:

If a summoner_name wasn’t passed into the function then it’s default value will be None. We can check for this and then prompt the user to enter a summoner_name inside the function instead.

Now we can prepare the URL we’re going to use to query the API:

Remember from the last part of the tutorial, we went into the Riot developer portal and generated a URL we could use to query the API; that’s where the api_url in our code comes from.

Looking at the URL you can see it’s making a request to the SUMMONER-V4 endpoint and it’s getting that information by a summoner’s name.

All we need to do is edit this URL to include the region our summoner is in, the summoner’s name and our API key to validate the request.

We include the summoner’s name and region in the actual URL itself using Python f-strings but for the API key we include it in it’s own Python dict. This is because we will make our requests to the API through the web using the Python requests library’s get method, which sends a GET request to a URL. The get method accepts URL parameters as optional arguments, which is where we will be passing in our API key. This makes the code neater.

Let’s make the request to the API and return the response:

We have encased this in a try except block to catch any errors that may occur when fetching data from the API over the web. If any errors do occur, we print an error message and return None to the user.

On line 21 you can see the actual request being made to the API. We pass in our api_url and our params which include our API key. We use urlencode to convert the contents of the params dict to URL parameter form.

We then raise any errors with the raise_for_status function and, if there aren’t any, we return our response JSON.

With just these few lines of code we now have a function capable of getting a player’s League of Legends profile information from the Riot API.

The full function looks like this:

Running our function is super simple: we just need to go into our __main__.py file, import our function and use it.

The below snippet of code does just that:

Once your run this code, you will see an output like this:

This shows the JSON returned from the API containing our player’s profile information, which we now have access to in our Python scripts.

That all for now!

We looked at how you can make calls to the API in Python.

In the next part, we’ll use our player’s profile data obtained through our function and use it to get information on their recently played matches.

Next Part: Getting a Player’s League Match History Using the Riot API in Python | by TheConnoisseur | Sep, 2023 | Medium

Previous Part: Getting Game Data Using the Riot API in Your Web Browser | by TheConnoisseur | Sep, 2023 | Medium

Thanks for reading!
If you enjoyed this article then feel free to clap it and check out some of my other articles :)

--

--

TheConnoisseur

Avid Computer Enjoyer, I write about topics I'm interested in, mainly programming.