How do API calls work?

Manvi Chaddha
Last Updated: May 13, 2022


API stands for Application Programming Interface, a set of functions and procedures that allow for creating applications that access data and features of other applications, services, or operating systems. Some common examples of using API are fetching weather information from a third-party application, using authentication like login using Facebook, Google, etc., travel booking, etc. Government organizations, banks, healthcare organizations, and even individuals to share anything use and maintain an API.

Using information from an API requires an API call; in this blog, we will learn what an API Call is and how to make one. Let's first start with a brief introduction to APIs.

A Brief Introduction to APIs

Let's consider an analogy to understand API calls. Imagine visiting a restaurant and asking the waiter to bring in your order; the waiter visits the kitchen and requests to make your order. As simple as that.

Did you visit the kitchen, and are you concerned about how the order is being prepared?

The answer is NO.

When talking about APIs, it's crucial to understand that the waiter is an abstraction of an API, you are the client, and the kitchen is the webserver, as illustrated in the below flow chart.

The application doesn't need to know how the webserver works, how to use the APIs to get its display data. The end-user doesn't need to know how the APIs work or navigate the user interface to perform the tasks she needs to perform.

Now that we know a brief overview of APIs let's take a look at how API calls work,

API Calls

In the example discussed above, how did you call the waiter? You called him by a nod or by his name or raised a request, and then he asked about your requirements. Unlike ordering food from a restaurant, we are trying to make an API call; you need to follow a set of rules or stages while making one.

Making an API call works in these stages:

  1. Make API calls using URI or Uniform Resource Identifier.
  2. If the API call is valid, the API calls some external program for some data.
  3. The API gets a response from the external program.
  4. The data is given to the initial requesting program.

Let us now analyze all the stages one by one

Making API calls using URI

URI is the digital equivalent of address. Consider an analogy with your residential address; the address is not unique to you, but in addition, someone who knows the address can come to your place and request you to do something. 

To make an API call using URI, you need to initiate a Request. Requests have a type to which they adhere; GET, POST, PUT, PATCH, and DELETE are the few examples of the type of requests you can make.

All the requests need to be of a particular format with headers. Headers are meta-data for the API; they tell you about the data request and what you want as a data response. Two common headers are Content-Type and Accept. 

Content-Type: It's a commonly used header and provides information to the API about what type of information is in the request body. This header is needed because there are many possible ways to format the same piece of data. Without this information, an API might not be able to do anything even if valid data is passed to it because it does not know and has no way to decode it.

Accept: It's the opposite of Content-Type; we ask the format we would like our data response from API.

Sounds Confusing, Right?

Let's extend the restaurant example here also. Consider that the restaurant owner has changed its policy. Instead of directly calling the waiter from the reception area, you have to write down your order over a piece of paper and specify on the top which language you are writing about your order. As You wish to eat chocolate pastries and as per the restaurant policies, you write down your order in the English Language over a piece of paper. You mentioned on the top that the language is English. You even specified that you need pastries served in a definite format. Drawing an analogy to the API example, here, the Content-Type is English written over the top of the paper, and the Accept is the format you specify for the pastries to get served. Calling the waiter from the reception area and handing over your order is equivalent to calling an API using URI.

Call External Programs for Data if the request is valid,

So till now, you have requested the waiter to bring in chocolate pastries. In the restaurant analogy, if and only if the restaurant serves chocolate pastries, the waiter will call the chefs in the kitchen to make one for you. What if the restaurant does not serve chocolate pastries?

The answer is you will not get one, as simple as that.

In the API context, This means that the request should be valid. Only then does the API call an external program for data; the API calls the external program because they don't store the data themselves. Rather they provide the connection to an external program. In the restaurant analogy, if and only if the restaurant serves chocolate pastries, the waiter will call the chefs in the kitchen to make one for you. 

The API gets a response from the external program.

Getting a response from the external program is analogous to getting the chocolate pastries requested above from the chefs. A quick side note, the chef will handle the side work of preparing pastries on being asked by the waiter and once prepared will give it to the waiter.

In the API context, the API requests the external program. The external program does some of its work and passes a copy of the data to the API.

Give the data to the requesting program.

In the restaurant analogy, the waiter is now responsible for serving you with the pastries. Remember that you requested the waiter to serve chocolate pastries in a specific presentable manner. What if the chef does not know about that format?

As customers, you only need the pastries in that format, and you will not take them in any other format. What will the waiter do in such a situation?

The waiter can request the chef to change the format as requested; however, the chef has to handle many orders continuously and is majorly responsible for serving the food in a simple form.  In that case, the waiter will work on formatting the pastries as per the order format.

The API has the data in the API context, and it now has to pass it back to the requesting program. If the format of the data isn't usable by the requesting program, then it's useless. Therefore the API will consider what information was in the Headers of the request. Remember that set of meta-data (information about the data); We used the example of the Accept header to represent in what format the requesting program wants the data. If a format change was required (given that the API can change to this specific format), this is where the API would make that conversion. For example, if the requesting program wants the data to be formatted in JSON, but the external program gives out in XML, it's the job of the API to convert the XML to JSON.

That was all about how to request data using an API. Let's look at some of the frequently asked questions related to the same.

Frequently asked questions

Q1) What exactly is an API?
Ans 1) API is the acronym for Application Programming Interface, a software intermediary that allows two applications to talk to each other. Each time you use an app like Facebook, send an instant message or check the weather on your phone, you're using an API.

Q2) What can APIs do?
Ans 2) An API (Application Programming Interface) is a set of functions that allows applications to access data and interact with external software components, operating systems, or microservices. API lets a developer make a specific "call" or "request" to send or receive information.

Key Takeaways

In this blog, we discussed API calls in-depth theoretically. Now it's time for you to implement an API call by yourself. The more you learn, the more you grow.

Planning to ace the interviews of reputed product-based companies like Amazon, Google, Microsoft, and more? Attempt our Online Mock Test Series on CodeStudio now!

Was this article helpful ?


No comments yet

Be the first to share what you think