DEV Community

Julie Cheng
Julie Cheng

Posted on • Updated on

The unveiling of the APIs

waiter

What is API?

API. What does this well-known acronym actually mean? How do you explain what APIs are in plain English? API stands for Application Programming Interface. OK, it is good that we now know what API stands for. But what do these combined words mean?

Based on the Merriam-Webster dictionary, interface (noun) is defined as "the place at which independent and often unrelated systems meet and act on or communicate with each other". OK, and then what? When the words application programming are added to the interface, the definition of API would be something like a software intermediary where two independent applications are able to act on or communicate with each other, that is, the connection between two different software applications, i.e. the client and the server.

To be honest, initially I had thought of an API as the backend server, but that is not the case. The API is not the same as a server, which provides resources and services to another computer program. The API is the "part of the server that receives requests and sends responses" or you can simply think of the API as a waiter. (ref.)

api_waiter

As a customer in a restaurant, you are not expected to fetch your own meal from the kitchen. You are not even allowed going into the kitchen at all. It is the waiter's job to receive your meal request and then pass it to the cook in the kitchen. The kitchen houses all the ingredients, recipes and the cookware that are needed for preparing the meals. The requested meal is processed, and the waiter brings back the "response" to the customer. The waiter is the "API" of this process.

Why use an API?

Now that we have established that an API is like a waiter between the client and the server. Why must we go through the waiter to get our food? Why can't we just go straight to the kitchen? Not only will that be chaotic with lots of people demanding their food all at once, but a client can walk away with someone else's food, i.e. it can cause a security breach.

Without an API, the frontend client will need to import a large set of information from the server, save it to a local database, and filter out the information that is needed for the application. And by the time the information has been narrowed down, it could be outdated.

With an API, this process is simpler and faster. The API will filter and provide the most up-to-date information the client has asked for.

How do we use APIs?

Applications communicate with each other through the web by following the HTTP protocol (Hyper Text Transfer Protocol), which includes API. This communication is also known as the request-response cycle. The client sends a request to the server and then the server will send a respond to the client as an answer to the request. There are 4 different HTTP methods that requests could use ("GET", "POST", "PATCH", "DELETE"). "GET" is the default and the most common method, and it is basically asking for the data to be sent.

Where should the client send a request then? How will the API receiving the request know which information to send back? The answer is through endpoints. An endpoint is a specific URL that the client can send a request to in order to retrieve specific data.

api_endpoints

import requests

class getRandomDogs:
   def get_random_dogs(self):
        URL = "https://dog.ceo/api/breeds/image/random/3"
        ## the endpoint which will fetch 3 random dog images

        response = requests.get(URL)
        return response.context

images = getRandomDogs().get_random_dogs()
print(images)

Enter fullscreen mode Exit fullscreen mode

Once the python file is run, the terminal will print out the json response that was received. Note: results will differ.

{
    "message": [
        "https://images.dog.ceo/breeds/newfoundland/n02111277_5898.jpg",
        "https://images.dog.ceo/breeds/buhund-norwegian/hakon2.jpg",
        "https://images.dog.ceo/breeds/spaniel-brittany/n02101388_10290.jpg"
    ],
    "status": "success"
}
Enter fullscreen mode Exit fullscreen mode

What if, instead of 3 random dog pictures, I want 3 dog pictures for a specific breed. This dog API has an endpoint for that.

import requests

class getRandomDogs:
   def get_random_dogs(self):
        URL = "https://dog.ceo/api/breed/hound/images/random/3"
        ## notice the different URL - we are looking for hound only
        response = requests.get(URL)
        return response.context

images = getRandomDogs().get_random_dogs()
print(images)

Enter fullscreen mode Exit fullscreen mode
{
    "message": [
        "https://images.dog.ceo/breeds/hound-afghan/n02088094_1829.jpg",
        "https://images.dog.ceo/breeds/hound-blood/n02088466_8950.jpg",
        "https://images.dog.ceo/breeds/hound-english/n02089973_3426.jpg"
    ],
    "status": "success"
}

Enter fullscreen mode Exit fullscreen mode

Most public APIs will have documentation on how to use the API and will also list the endpoints for reference.

With APIs, data becomes easily accessible. The frontend does not need to spend time collecting the data, querying the data, presenting the data in a neat format. All of that is done by the API, while the front end can focus on other functions.

I hope this explanation is simple enough to follow along. Technical terminology is difficult to explain sometimes, but by using a simple metaphor the concept becomes easier to understand. In the context of understanding API, we just need to think of it as our waiter.


References and Additional Readings
DOG API

What is an API in English Please

What exactly is an Api explained in simple terms

How Apis Work

Api Less scary approach

Top comments (0)