The Startup
Published in

The Startup

Using and calling an API with Python

Hi everyone! In this article, I’ll be showing you how to set up a link to an API and make calls to it. I’ll be working from a Jupyter Notebook/Lab but feel free to follow along in your environment of choice.

I will be using the ROM Exchange API, a publicly available API that has been keeping track of the market price of items on two different servers of the game Ragnarok Mobile.

What is an API?

API’s are a very common set of tools and protocols that allow interaction between applications, code, projects and machines. API’s allow these things to access information from each other.

As ProgammableWeb puts it, we are all familiar with a user interface like the one on your phone. An API is another kind of user interface but the user is not a human being, it’s a machine, a software application running on your computer for example. It allows a machine to access information and is designed so it can do it easily. If one device or machine wants to talk to another or get information from another it can do this through an API.

What do I mean by calling an API?

When we want to access information using the API we make requests. The action of sending a request to an endpoint on a server is known as making a call to an API. Endpoint, what is that? We don’t know that word. An endpoint is a destination perhaps on a server or service. It is where the resource we are looking for lives.

For example take the following URL: http://www.google.com/images, our endpoint images at google.com will take us to the images search page on google.

Here is another: https://www.romexchange.com/?q=Amethyst

This will take us to romexchange.com and load the market price data for the item Amethyst. when we make requests to an API we receive responses. Requests responses and endpoints. That about sums up the intro.

How to start:

  • Setting up in the notebook
  • Look at the API Documentation

Ok first let’s fire up a notebook or lab with Jupyter. Now, I happen to know we need to import the Python Requests library first. If you were doing this completely on your own you could google search ‘python how to make requests’ and you’d stumble upon it in no time through stack exchange or another helpful medium. Make sure the library is up to date* and run this line:

import requests

*Remember you can update your python libraries with the following command, just swap ‘library’ for requests in this case.

pip install --upgrade ‘library’ 

Step 1 done. The library that is going to facilitate all this API back and forth is at our fingertips and ready to go!

Before going any further let's take a look at both the documentation for the RomExchange API and for the requests library.

The Request library docs have an installation guide and quickstart. Let’s see the quickstart. Oh good it tells us to start with import requests, check. Then it shows us how to make a call to a webpage.

Let’s try it with the romexchange website:

r = requests.get('https://www.romexchange.com/')

We declare ‘r’ as our response object. we use the .get() method from the requests library to obtain a response from the URL of our choice.

We can see what kind of a response we got using the status code command:

r.status_code

Remember by aligning the cursor after the ‘.’ and pressing tab, we can see all the available functions and tools we can do with the requests library.

Oh noes. We got a 406 status code — Not Acceptable!

Well now it might be time to turn to the RomExchange API Docs. They give us some sample requests and tell us about queries we can use when making requests. On the right side if we click the get button we can see an example request and it says underneath the content-type is application/json.

Back to the Requests documentation. Okay, it seems that we can make a slightly different approach with a custom header to make sure that the content-type we’re expecting is going to be recognised as application/json:

url = ‘https://www.romexchange.com/' 

headers = { ‘Content-type’: ‘application/json’}

now if we make a request like before but instead of typing out the url we can use the URL stored in our ‘url’ variable and pass in the headers as a parameter:

r = requests.get(url, headers = headers)

Did it work? No still the same error but we’re getting close to the bee's knees here. Let's google this one. I googled ‘python requests 406'. Stack Overflow was the first result.

The Top solution :

Solved by using a different User-Agent. The default Python User-Agent 'python-requests/2.21.0' was being probably blocked by the hosting company. Some of the possible agents: List of User Agent strings

Maybe we’re having the same issue here? Let's add the User-Agent change found in that solution code to our headers params:

url = ‘https://www.romexchange.com/' 

headers = {'User-Agent': 'XY',‘Content-type’: ‘application/json’}
r = requests.get(url, headers=headers)

Ok and now our status code please?

r.status_code

Wrapping up

We’ve used an API to make a request for some information! We’ve only asked to get access to a couple of sites but we’ve made it a long way!

If we want to do any meaningful research on these items’ market prices we can use the query syntax we found on the ROMExchange documentation to do so change the url to something that queries an item we are interested in e.g:

url = https://www.romexchange.com/api?item=mastela&exact=falseheaders = {'User-Agent': 'XY', 'Content-type': 'application/json'}
r = requests.get(url, headers = headers)
r.status_code

once that has passed with a 200, just call .text on our response variable r:

r.text 

Thanks for reading!

You can now follow this project with the next installment where we learn about the Extract-Transform-Load process.

Sources:

  1. Programmable Web Youtube channel — APIs 101
  2. ROM Exchange — https://www.romexchange.com/
  3. Python Request library — Documentation
  4. List of status codes — Wikipedia
  5. User-Agents information — DeviceAtlas
  6. BONUS — What does ? and # mean in a URL?
  7. Follow on with this project and learn about ETL

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aleksandar Gakovic

Practicing Data Scientist. Interested in Games, Gamification, Ocean Sciences, Music, Biology.