Get startedGet started for free

APIs and interacting with the world wide web

1. APIs and interacting with the world wide web

Congrats on making it through your crash course in JSONs! JSONs are everywhere and one of the main motivating reasons for getting to know how to work with them as a Data Scientist is that much of the data that you'll get from APIs are packaged as JSONs.

2. Herein, you’ll learn

In this video, you'll learn what APIs are, why they are so important, and see a number of illustrative examples. In the subsequent interactive exercises, you'll gain valuable practice connecting to a variety of APIs, pulling and parsing data from them.

3. What is an API?

So what is an API and why are they so important? Simply put, an API is a set of protocols and routines for building and interacting with software applications. Another way to think of it is that an API is a bunch of code that allows two software programs to communicate with each other. For example, if you wanted to stream twitter data by writing some Python code, you would use the Twitter API. If you wanted to automate pulling and processing information

4. What is an API?

from Wikipedia in your programming language of choice, you could do so using the Wikipedia API.

5. APIs are everywhere

Using such APIs have now become standard ways of interacting with such applications. Twitter has an API that is used by marketing companies and social scientists engaged in research concerning social networks.

6. APIs are everywhere

Uber,

7. APIs are everywhere

Facebook and

8. APIs are everywhere

Instagram all have APIs. Now let's figure out how to connect to an API and how to pull data from it.

9. Connecting to an API in Python

In this example, we'll pull movie data from the Open Movie Database, or OMDB, API. Once again, you'll use the ever-elegant requests library. You import requests and assign the URL of interest to the variable url. You then package and send the request to the URL, which describes your API query, and catch the response in one line of code. Thanks again, requests package! Another really cool aspect of the requests package is that the Response objects, such as r, have an associate method json, which is a built-in JSON decoder for when we're dealing with JSON data. This returns a dictionary and we can then print all the key-value pairs to check out what we pulled from the OMBD API!

10. What was that URL?

Now the last thing to discuss is how the URL we used actually pulled data from the API. To do so, lets break it up into chunks. The http signifies that we're making an HTTP request, the 'www dot omdb dot api' that we're querying the OMDB API, then there's the "?t equals hackers" which is the really interesting part and something we haven't discussed yet in this course. This string that begins with a question mark is called a Query String. Query Strings are parts of URLs that do not necessarily fit into conventional a hierarchical path structure. What follows the question mark in the query string is the query we are making to the OMBD API. The query we just made was simple : querying 't equals hackers' asked the API to return the data about the movie with the title Hackers. The 't' in the query stood for title.

11. OMDb API

We knew that this was how to perform such a query from the documentation on the OMDB API's homepage. Under "Usage" here, they state explicitly that 'Send all data requests to: http:// www dot omdbapi dot com /?'.

12. OMDb API

They also have a query string parameters table that shows how to query a particular title or a particular movie ID.

13. It’s a regular URL!

It is also worth mentioning that there is nothing special about this URL and so you can also navigate to it in your browser of choice. It will generally look like this. I like to use a Chrome extension called JSON formatter to make it a bit prettier. Alright. Now you know all about APIs and have a basic practical understanding of how to query them,

14. Let's practice!

lets get you writing some Python to extract some data from a number of APIs! Happy coding!