One of the most useful features invented in the modern computer era is the API or application programming interface. This may sound confusing or complicated but it’s actually not.

There are more complicated iterations of an API but for the most part most people use API’s to extract data from a database. If you think about it, most applications are just elaborate tools to access and send data. The Uber app, allows you to send data about your current location and trip plan to a driver who can then send you data on when they’ll arrive to pick you up.

Dictionaries

– Most of the time, applications will send you data in a format that will appear in the form of a data dictionary. What’s a data dictionary, we perhaps it’s easiest at the start to show you what one looks like:

Screenshot taken from w3school.

Great, that’s somewhat helpful. Imagine you’re trying to connect to a website API that tracks cars, to extract the make of the car you would type:

thisdict['brand']

…to extract “Ford.”

Now most api calls are a lot more complicated than this and usually require two more important features to work properly :

  • A proper request command.
  • A json method to interpret the response data.

Let’s use the API for Tiingo.com as an example of how to do this correctly. Tiingo is a financial services website that provides near real time trading data that can be accessed programmatically via an api.

import requests
headers = {
    'Content-Type': 'application/json'
}
requestResponse = requests.get("https://api.tiingo.com/tiingo/daily/aapl/prices?startDate=2019-01-02&token=7471448cb0d6ac0ee714d622d7cada65b28a552e", headers=headers)
print(requestResponse.json())

# https://api.tiingo.com/documentation/end-of-day

Let’s examine this code block we acquired from the Tiingo documentation website.

  • First we can see that we have to “import requests.” ‘Requests” is the name of a package that’ll handle standard https requests for Python.
  • In this case we’re modifying the headers to work with json. Not all api’s use a step like this. Consult you’re api documentation but in this case the documentation that we modify the header parameter when requesting data from the API to specify json.
  • The next step is to enact the “request.get()” command to call the api using the url structure provided by the website.
  • Then you’ll usually want to take the object your store the request and run the .json() method to explicitly output the data with the assumption of a json structure in mind.
  • Python will then export your data in a dictionary format that you can subset and manipulate in the same way you would a regular python dictionary.
From my Jupyter notebook.

Leave a comment