Article Outline

  1. What is an API? What is a web API, and how is it relevant to data science?
  2. The anatomy of an API request
  3. Unearthing the secrets of Reddit — A walkthrough with the Reddit web API using Python
  4. Discovering hidden APIs with developer tools
  5. API etiquette and best practices

What is an API?

If you have ever spent any time programming, doing data science or web development, chances are that you have come across the term “API”. Even to a complete layperson, the term may ring a bell. Regardless of whether you are acquainted with the concept, you interact with APIs on a daily basis, as they have become ubiquitous in recent years, and facilitate many of the platforms and services which you may find indispensable.

By literal definition, an API is an Application Programming Interface. It is a medium through which different applications can communicate with one another programatically.

To give a concrete example, consider the process of logging into an app such as Medium. You are given the option to login using Google or Facebook. In this instance, the Medium app is getting access to your Google or Facebook user data through an API developed by those companies. Such API’s are widely used by numerous apps and platforms, which reduces development time for the app creators, and augments ease of use for the end-users.

APIs are also helpful to data scientists because they are a straightforward avenue for accessing large amounts of structured data from various services and platforms. For instance, the TMDB API allows users to find information about films and shows, and the Spotify API gives you in-depth information about their songs (such as “danceability” and “valence”) which you would be hard-pressed to find by other means.

There are numerous other interesting things which you can do or automate with APIs, which are outside the scope of this article. For instance, you can use Spotify’s API to programatically control the music playing on your account. You can even connect multiple API’s together and have them work together in concert. The website IFTTT.com allows you to chain together APIs in order to create applets which are triggered based on certain conditions. For instance, you can get your Phillips Hue bulbs to change colour when you receive a new Twitter notification, by combining the APIs of the respective products.

Anatomy of an API request

In order to make use of web APIs, we have to make what is known as a ‘request’. Such a request will be made through the HTTP protocol. A request is essentially a communication between you and a remote server, in which you communicate what you are trying to achieve (eg. acquire some data or create a new blog post). Having received the request, the server then processes your request, and provides a response, as well as performs any additional processes in the backend, which are generally opaque to the end user.

There are various types of HTTP requests, but for the purposes of this article, I’m going to focus on the “GET request”, since our aim is to use API to get useful data.

Generally, a request will include the following components:

  • Endpoint
  • Request type
  • Parameters
  • Headers
  • Response, Response Code

In broad terms, when we make a request, we have to direct it to a certain location. The location, or ‘endpoint’, will be determined by what we are trying to do, or what information we are requesting. As we make the request, we can pass along a collection of parameters, which increase the specificity of the request, as well as some headers, which will be explained later. The server will then (hopefully) provide a response (usually in JSON form) and a response code (which indicates whether the request was successful).

To best illustrate the process in action, I’m going to walk through a simple exercise using the Reddit API and Python.

#python #data #web-development #api #data-science

A Beginner’s Guide to Accessing Data with Web APIs (using Python)
30.50 GEEK