Open In App

How does an API Work ?

Last Updated : 27 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

API stands for Application Programming Interface which helps software applications communicate with each other to exchange data, features, and functionality. This exchanged data is in the form of HTML, JSON, XML, and Text. In this article, we will discuss the workings of API.

We will discuss the following components of an API and how an API works:

1. API client:

The API client sends the request to the API server. This request can be triggered in many ways. For example, when a user clicks a button or enters a search term.

2. API request:

An API request contains the following components

  • Endpoint: An API endpoint is a URL that provides access to a specific resource. For example, the article’s end point in GeeksforGeeks would contain the logic for processing all requests that are related to articles.
  • Methods: The request method includes the type of operation that the client needs to perform on a given resource. REST APIs are accessible through standard HTTP methods, which perform common actions like retrieving, creating, updating, and deleting data.
  • Parameters: Parameters are the variables that are passed to an API endpoint to provide specific instructions for the API to process. These parameters can be included in the API request as part of the URL, in the query string, or in the request body. For example, the /articles endpoint of a blogging API might accept a “topic” parameter, which it would use to access and return articles on a specific topic.
  • Request headers : Request headers are key-value pairs that provide extra details about the request, such as its content type or authentication credentials.
  • Request body: The body is the main part of the request, and it includes the actual data that is required to create, update, or delete a resource. For instance, if you were creating a new article in GeeksforGeeks, the request body would likely include the article’s content, title, and author.

3. API server:

The API client sends the request to the API server, which is responsible for handling authentication, validating input data, and retrieving or manipulating data.

4. API response:

Finally, the API server sends a response to the client. The API response typically includes the following components such as HTTP status codes, response headers and response body.

How does API work?

API’s work by sharing data between applications. The request is sent to the API, which retrieves the data and sends to the user. On the web, we use the HTTP protocol (which stands for Hyper Text Transfer Protocol).

Let us get the data from API using Postman.

Example API : https://jsonplaceholder.typicode.com/posts

Step 1 : Click the “+” button to create a new request as shown below

Screenshot-2024-03-19-181759

Step 2: In the URL bar, enter the endpoint of the API you want to interact with. Here, enter our public API https://jsonplaceholder.typicode.com/posts and click on “send” button to execute the request.

Screenshot-2024-03-16-201006

Making an API GET request in Postman

Step 3 : After clicking the send button, the server fetches for the “/posts” end point and sends the response which looks like this

Screenshot-2024-03-18-202248

Response sent by the server to the API

Types of API

There are four different types of APIs commonly used in web services: public, partner, private and composite.

  • Public API : A public API is open and available for use by any outside developer or business. These APIs can be used by anyone and these are also called as open APIs.
  • Partner API : A partner API is made available to specifically selected and authorized outside developers or API consumers. The partners have some rights and licenses to access these APIs. These cannot be accessed by the public.
  • Private API : Unlike Partner APIs, A private API is used only by internal users of an organization. Outsiders are not given any access to these APIs. These APIs are used by different teams inside the organization.
  • Composite API : When the tasks are interconnected with each other, the APIs are combined to perform such tasks. This combination of APIs is called as composite API.
    Example : Let us take an API and try to get the data from the API

API : https://jsonplaceholder.typicode.com/photos

Output :

Screenshot-2024-03-18-201327

A part of response sent by the API

Conclusion:

Application Programming Interface(API) is an intermediate between the software applications. It also helps in abstraction as it hides the internal complexity and gives the required information which we need. The API contains the URL, method, header and body. APIs follow the HTTP protocol to communicate, which has a specific request and response structure. Many third party applications use APIs to book the tickets or to make payments.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads