What exactly is an API?

The term "API" can be difficult for beginners to grasp, but I am sure it will be clear once you read the following explanation. We will talk about the most popular API approach - the REST API. What is a webpage? A webpage is a document accessible through a URL. A webpage is meant to be accessible by humans. That's why webpages (e.g., the Wikipedia page below) are pleasant to look at. They have good-looking fonts and colors, pictures, etc. Webpages are served by a web app and the web app runs somewhere on a remote server.

On the other hand, we have REST APIs. REST APIs are also apps that run somewhere on a remote server. The difference this time is that a REST API serves data that are meant to be read by computers, not humans. In the example below, we are using a URL from the Wikipedia REST API to get information about dolphins.

I suppose it was easier for you to read the previous webpage than this one. Even though both pages give information about dolphins, the second example is meant to be accessed and consumed by computer programs, not humans. In fact, we can build a computer program that accesses that information right here with Python:

import requests
url = "https://en.wikipedia.org/w/api.php?format=json&action=" \
      "query&prop=extracts&exintro&explaintext&redirects=1&titles=Dolphins"
data = requests.get(url).json()
print(data["query"]["pages"]["9061"]["extract"])


The program above uses the Python requests library, which needs to be installed with "pip install requests" in your terminal. Once you install requests and run the program, you will get the following output:

A dolphin is an aquatic mammal within the infraorder Cetacea. Dolphin species belong to the families Delphinidae (t...

That means your program was able to communicate with the other program (the Wikipedia REST API) and extract information about dolphins. And that is what APIs are about. They provide a communication channel for two programs to communicate with each other. Once your Python program gets access to the info, you can do further operations to that piece of data. You can send it by email via Python, display it on a Flask webpage, and more. The possibilities are endless.

It is also possible to build your own REST API with Python. You will need to use a Python library/web framework such as Flask or Django. Flask and Django are both covered in the course, so don't forget to keep learning.

Next Lecture
Recommended Course

Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.