API-Application Programming Interface

Aditya Narayan Tiwari
4 min readJan 25, 2023

--

How do API works?

Let’s pretend that you’re a chef and I’m a server at a restaurant. You want to make a delicious meal, but you don’t have all the ingredients in your kitchen. So, you send me a menu (or request) with a list of the ingredients you need. I go to the grocery store (or another service) and gather all the ingredients for you. Then, I bring them back to you (or return the data) and you use them to make the perfect meal (or use the data in your program).

Just like how a chef uses ingredients from a server to make a meal, a developer uses data from an API to create something new. APIs (Application Programming Interfaces) allow different software programs to communicate with each other, and share data and functionality.

So in a nutshell, API is a set of rules and protocols that allow different software to communicate and share data, and also it is like a chef who sends a menu to a server and get ingredients to make a meal.

APIs use different methods to send requests and retrieve data. The most common methods used in APIs are:

  1. GET: This method is used to retrieve data from the server. It is the most common method used in APIs and it is used to retrieve information about a resource.

2. POST: This method is used to submit data to the server. It is commonly used to create a new resource or to update an existing one.

3. PUT: This method is similar to POST, but it is used to update an existing resource.

4. DELETE: This method is used to delete a resource from the server.

5. PATCH: This method is used to partially update a resource. It allows you to update specific fields of a resource rather than the entire resource.

Components of API:

An API (Application Programming Interface) is made up of several components that work together to allow different software programs to communicate and share data. These components include:

Endpoint: This is the URL or web address that a client uses to access the API. It is the location where the API is hosted and the specific resource that the client is requesting.

Request: This is the message sent by the client to the API, asking for a specific resource or action to be performed.

Headers: This is additional information sent along with the request, such as authentication information or the type of data being sent.

Method: This is the type of action being requested, such as GET, POST, PUT, or DELETE.

Parameters: This is additional information sent along with the request, such as a query string or request body, that is used to specify the resource or action being requested.

Authentication: This is a process that verifies the identity of the client making the request. It may include an API key or other authentication mechanism.

Response: This is the message sent by the API to the client in response to a request. It may include the requested data or an error message.

Status code: This is a three-digit code that is sent along with the response, indicating the status of the request, such as success or failure.

Error handling: This is the process of handling errors that occur during the communication with the API, such as invalid requests, missing parameters, or unauthorized access.

API response:

These codes indicate the status of a request made to an API. Some common response codes include:

  • 200 OK: The request was successful and the server returned the requested data.
  • 201 Created: The request was successful and a new resource was created as a result.
  • 204 No Content: The request was successful, but there is no additional information to send back.
  • 400 Bad Request: The request was malformed or invalid.
  • 401 Unauthorized: The user is not authorized to make the request.
  • 403 Forbidden: The user is authenticated, but doesn’t have the necessary permissions to perform the request.
  • 404 Not Found: The requested resource could not be found.
  • 500 Internal Server Error: An error occurred on the server.

These codes are standardized by the HTTP protocol, and are used by many APIs. However, some APIs may use custom response codes specific to their implementation.

Types of API which are commonly used.

  1. SOAP API (Simple Object Access Protocol)
  2. REST API (Representational State Transfer)

SOAP API:

  1. Data formats: XML and JSON
  2. More Secure
  3. Message Structure: Envelope, Header, Body, Fault.
  4. Need less coding!
  5. Slow and Heavier
  6. Caching is not possible.

REST API:

  1. Data formats: JSON, HTML, XLT, Python, PHP, or plain text.
  2. Less Secure
  3. Message Structure: In the form of Postcard.
  4. Need little bit more coding!
  5. Faster and Lighter
  6. Caching is not possible.

APIs can be open and publicly available, allowing any developer to use them, or they can be private and require authentication or a key to access. Many popular websites and services, such as Facebook, Google, and Twitter, have APIs that allow developers to access data and functionality from those platforms in their own programs.

In summary, an API is a set of rules and protocols for different software programs to communicate with each other, it allows one program to request data or functionality from another program, and receive a response. It helps developer to access and use data and functionality from other sources in their own programs and applications.

--

--