How to get data from an API using requests

1 min

In order to get data from an api, you will need to perform an HTTP call against this API.

That is  in simple words a way to make your computer communicate with another service.

To do so, we will need to use the requests Python library.

The API of interest

There exist many APIs, some are free, some other are paid.

If you wonder if a service you know has an api, try googling it with the name API at the end, you might end up finding something.

Today we are using Animechan

Because it is an easy to demonstrate, no authentication required api, which gives you a random anime quote.

Perform an HTTP call

import requests # The library used to make the call

# We define the url
random_quote_url = "https://animechan.vercel.app/api/random"

# We perform the HTTP get call against the api
anime_requests = requests.get(random_quote_url)

# We print out the response
print(anime_requests.json())
Example of getting a random quote from the anime API

Here you are, you can now work with the just you just got back from the api.