How API works?

Oguzhan Ozturk
5 min readJun 13, 2023

--

An API, or Application Programming Interface, is a software intermediary that allows two applications to talk to each other. In other words, an API is the messenger that delivers your request to the provider that you’re requesting it from and then delivers the response back to you.

APIs are used in a wide variety of applications, from web development to mobile development to data science. They are a key part of the modern software development landscape.

Here is a simplified overview of how an API works:

  1. The client makes a request to the API. This request is typically made using an HTTP request, such as a GET, POST, PUT, or DELETE request.
  2. The API server receives the request and processes it.
  3. The API server returns a response to the client. This response is typically in JSON or XML format.
  4. The client receives the response and parses it.

Here is an example of how a client might make a request to an API:

// Make a GET request to the API
var request = new XMLHttpRequest();
request.open('GET', 'https://api.example.com/users/1');
request.send();

// Handle the response
request.onload = function() {
if (request.status === 200) {
// The request was successful
var data = JSON.parse(request.responseText);
// Do something with the data
} else {
// The request failed
}
};

In this example, the client makes a GET request to the /users/1 endpoint. The API server will return a JSON object containing the user's data. The client will then parse the JSON object and do something with the data.

APIs can be used to do a variety of things, such as:

  • Get data from a server
  • Post data to a server
  • Update data on a server
  • Delete data from a server
  • Control devices
  • Communicate with other applications

APIs are a powerful tool that can be used to build a wide variety of applications. If you are a developer, I encourage you to learn more about APIs and how they can be used.

4 Steps of API

  1. The client makes a request to the API. This request is typically made using an HTTP request, such as a GET, POST, PUT, or DELETE request.

An HTTP request is a way for a client to communicate with a server. It consists of a method, a URL, and headers. The method indicates what the client wants to do with the resource at the URL. The headers provide additional information about the request, such as the client’s identity and the type of content that it expects in the response.

The four most common HTTP methods are:

  • GET: Used to retrieve a resource from the server.
  • POST: Used to create a new resource on the server.
  • PUT: Used to update an existing resource on the server.
  • DELETE: Used to delete a resource from the server.

2. The API server receives the request and processes it. The API server is a software program that runs on a server. It is responsible for receiving requests from clients and returning responses.

When the API server receives a request, it first checks the method to see what the client wants to do. If the method is valid, the API server will then process the request. This may involve reading data from a database, performing a calculation, or calling another API.

3. The API server returns a response to the client. The response from the API server is typically in JSON or XML format. JSON is a lightweight data-interchange format that is easy to read and write. XML is a more complex format that is often used for exchanging data between different systems.

The response from the API server will include the following information:

  • The status code: This indicates whether the request was successful or not.
  • The response body: This contains the data that the client requested.
  • The headers: These provide additional information about the response, such as the content type and the length of the response body.

4. The client receives the response and parses it. The client is the software program that made the request to the API. When the client receives the response, it will first check the status code to see if the request was successful. If the request was successful, the client will then parse the response body. This means that the client will convert the data in the response body into a format that it can understand.

Once the client has parsed the response body, it can then use the data to do something, such as update its own data, display a message to the user, or perform another action.

JSON

APIs (Application Programming Interfaces) use JSON (JavaScript Object Notation) to exchange data between a client and a server. JSON is a lightweight data-interchange format that is easy to read and write. It is also language-neutral, so it can be used with any programming language.

When a client makes a request to an API, the server will typically respond with a JSON object. This object will contain the data that the client requested, as well as any other information that the server deems necessary. For example, the server might include links to other resources, or error messages if the request was invalid.

The client can then use the JSON object to access the data that it needs. This data can be used to update the client’s own data, or to perform other actions.

Here is an example of a JSON object that might be returned by an API:

{
"data": {
"id": 1,
"name": "John Doe",
"email": "johndoe@example.com"
},
"links": {
"self": "https://api.example.com/users/1",
"friends": "https://api.example.com/users/1/friends"
}
}

In this example, the data object contains the user's ID, name, and email address. The links object contains two links: one to the user's profile, and one to a list of the user's friends.

APIs that use JSON are becoming increasingly popular because they are easy to use and understand. JSON is also a very efficient format for data transfer, which can improve the performance of APIs.

Here are some of the benefits of using JSON with APIs:

  • Easy to read and write: JSON is a human-readable format, which makes it easy for developers to understand and debug.
  • Language-neutral: JSON is a language-neutral format, which means that it can be used with any programming language.
  • Efficient data transfer: JSON is a very efficient format for data transfer, which can improve the performance of APIs.

If you are developing an API, JSON is recommended to exchange data with your clients. JSON is a powerful and versatile format that can make your API easier to use and more efficient.

--

--