DEV Community

Matt
Matt

Posted on

How to use Python's request library to retrieve data from API's

Introduction:

Python’s requests library allows developers send messages from a client, such as a web browser, to a server in order to receive data. These messages are commonly referred to as HTTP requests and are used when interacting with APIs.

An API (Application Programming Interface) allows different software applications to communicate with each other without directly accessing each other’s internal systems. Think of an API as a cashier in a fast-food restaurant. The cashier takes in the customer's order and passes it to the kitchen, where the order is being prepared.

When a company or organization wants to give people access to their data while maintaining security, they often use APIs as a middleman. This allows users, such as developers and researchers, to retrieve data without risking changes to the original dataset.

Python’s requests library makes gathering data from APIs simple and effective. In this tutorial, I will be using the FREETOGAME as an example. However, the concept learned in this tutorial can be applied to other APIs. Additionally, this tutorial uses Visual Studio Code, but you can follow along in any Python environment.

Setup:

Since requests is a third-party library, it has to be installed before use:

  1. Open Visual Studio Code and make a new file
  2. Open a terminal (Ctrl+Shift+~ or Terminal -> New Terminal)
  3. In this terminal type pip install requests and hit enter
  4. Verify installation in Python by typing import requests into the code editor. If no errors appear, installation was successful

Fetching Data:

The requests library supports 7 different HTTPS methods. However, this tutorial only focuses on the requests.get() method. This method takes in a URL as a parameter and is used to fetch a resource, such as data.

The URL used in this example can be found in the FREETOGAME API. Scroll down until you see the text live game list, and then copy the URL below that text. Afterwards, enter the following code into your editor.

     response = requests.get("https://www.freetogame.com/api/games")
Enter fullscreen mode Exit fullscreen mode

To check to see if the request was successful, print the response variable.

     print(response)
Enter fullscreen mode Exit fullscreen mode

If everything has been set up correctly, then when you run the program, **should be output in the terminal. The number 200 is a **status code that indicates that the HTTP request was a success!

Parsing JSON Data:

Although the request succeeded, the data is still not visible. Most APIs return data in JSON format, which is a structured text format that is easy for both humans and machines to read. To convert the response to JSON, use the .json() method.

     gameList = response.json()
     print(gameList)
Enter fullscreen mode Exit fullscreen mode

If done correctly, this will output a large list containing dictionaries representing different games. Although we can now see what the data contains, the output is hard to read. To view the data of one specific game. Replace the print statement in the above example with the following code and then click run.
print(gameList[0])

This outputs a dictionary containing information about the game Overwatch 2, including its description, the platforms it is on, and its developers.

Accessing specific data:

If you only need a specific piece of data in the dictionary, such as the release date of the games, you can access it using dictionary keys. For example, type in the following code and run your program.

     print(gameList[0]['release_date'])
Enter fullscreen mode Exit fullscreen mode

The output should be the date 2022-10-04. If you want to retrieve the release dates of every game in the list, use a for loop. Type in the following code.

     for game in gameList:
         print(game['release_date'])
Enter fullscreen mode Exit fullscreen mode

If done correctly, you should now be able to see the release dates of all the games in the list.

Conclusion:

The requests library is a very powerful and fairly easy library to work with APIs. To continue learning, consider exploring error handling, query parameters, and APIs that require authentication. In the real world, developers are expected to handle a wide variety of APIs.
Thank you for reading until the end of this tutorial.

Top comments (0)