A tl;dr overview of an API
Tags: api, javascript, laravel, tutorial
Published: 2022-06-29 16:34:54

#### In this article I’m going to discuss the following topics related to an API.


* What is an API ?
* What is the use of an API ?
* What are the different types of API ?
* What is a Restful API ?
* What are the HTTP verbs related to an API ?
* What are the HTTP status code related to an API ?



### What is an API ?

API is more like an agreement or a contract of services between any application with distinct functions. Here, the contract of service referred as an application **INTERFACE** and application with distinct functions referred as an application **PROGRAMS**, and that’s  how API stands for **APPLICATION PROGRAMMING INTERFACE**.


### What is the use of an API ?

API is mostly used for interaction( by transferring data ) between more than two applications followed by a request-response behavior.


### What are the different types of API ?

There are two ways you can categorized an API.

1.Based on architecture, for example

- **RPC** ( Remote Procedure Call ), there are two variation  XML-RPC, JSON-RPC

- **SOAP** ( Simple Object Access Protocol )

- **REST** ( Representational  State Transfer )

- **GraphQL**

2.Based on availability, for example

- **Open API / Public API**

- **Partner API**

- **Internal API / Private API**

- **Composite API**

In today’s  world most of the APIs are REST Driven or  RESTful ( based on REST  architecture ).



### What is a Restful API ?

There are six constraints in a REST API or a RESTful API. Any API can be a RESTful  API if it follows those six constrains.

* **Uniform Interface** -  An agreement that separates clients from the server. A resource( information that returned by an API ) in the system should have only one logical URI but it should contain links (known as HATEOAS) pointing to relative URIs to fetch related information.

* **Client-Server** - Client and the Server must be able to evolve separately without depending upon each other.

* **Stateless**- The server will not store anything about the latest request that the client made. It will treat every request as a new one. Here the client is responsible  for managing the state of the application.

* **Layered System** - A application architecture should be distributed in multiple layers but each layer doesn't need to know anything about any other layer other than the intermediate one.

* **Cacheable**  - The main function of that constraint is to increase the performance of the application by storing frequently used data either in the client-side or the server side.

* **Code on demand(optional)** - Most of the cases its optional. But it works by returning a executable code to support a part of your application.


### What are the HTTP verbs related to an API ?

HTTP verbs mainly use to tell the server what to do with the data identified by the url. There are 5 most common HTTP verbs and there uses as follows.

* **POST** - Create new resource ( C )
* **GET** - Read resources ( R )
* **PUT**  -  Update/Replace existing resource  ( E ) 
* **PATCH** - Update/Modify existing  resource
* **DELETE** - Delete resources ( D )

It almost similar to the CRUD functionality with different verbs.

		


### What are the HTTP status code related to an API ?

HTTP status code divided into 4 segment and each segment has their own functionality.

**2xx series related to Successful Request.**

- 200 - Successful

- 201 - Resource Created

- 204 - Resource Updated

**3xx series related to Redirection**

- 301 - Permanent redirection ( possible to change HTTP verb )

- 307 - Temporary redirection ( can’t change HTTP verb )

- 308 - Permanent redirection ( can’t change HTTP verb )

**4xx series related to Client Error**

- 400 - Bad request 

- 401 - Wrong credentials ( unauthorized )

- 403 - Lack of  authorization ( forbidden )

- 404 - Resource not exist

- 405 - HTTP verb mismatch

- 422 - Send data in valid

**5xx series related to Server Error**

- 500 - Internal Server error

- 501 - If server don’t know how to handle the request

- 502 - Bad Gateway

- 503 - Service is down for maintenance

- 504 - Gateway timeout