Home » Web API » What is an API – Application Programming Interface

What is an API – Application Programming Interface





API

API is a software intermediator that allows two applications to talk to each other. API receives “request” from the user/application and sends back “response” with the requested data to the user/application. Most of the applications we are using in our day-to-day life like Facebook, Twitter, and Instagram uses API calls.

In simple words, you are exposing your methods (in the form of API) to anyone on the Internet so that anybody can call your published APIs(methods) from any application and perform operations like Create, Retrieve, Update, Delete (CRUD) in your database.

In other words, you will execute methods in your code with the help of URL and HTTP Methods (Verbs).

Example:

Published API1) URL: http://yoursitename/api/Student/TotalStudent/{Classroom}
2) Http Method: GET
Target method in your codepublic object GetTotalStudentCount(int classroom) { }

If someone wants to know the total number of students count for a Classroom, then the above API (or URL) will be called with the “GET” HTTP Method. It will hit & execute “GetTotalStudentCount” method in your code.

Note: {Classroom} is the mandatory input parameter for your code (int classroom) and it should be passed. You should validate it and pass the error message if it is NULL or not a valid classroom.

This is how you will pass a value for the “Classroom” parameter while requesting the “Get” API e.g., http://yoursitename/api/Student/TotalStudent/8

This API (or URL) can be called from any application and from any device.

Any application means? – which is written using Angular, React, Java, DotNet, etc. like Gmail, Facebook, Netflix.

Any Device means? – Desktop, Laptop, Mobile, Tablet.

Testing purpose, you can use the Postman tool to execute such APIs.

Download link – https://www.postman.com/downloads/



Get API “Request” & “Response” via Postman Tool

The below screenshot is the example using the Postman tool – calling the API (which is a Request) and in return, it is giving 55 as output/result (which is a Response).

Postman Get API Call



Basic sample code in .net core – Get API call

API Code flow in below Image.

API – Application Programming Interface - Code flow


Step 1: Controller (API)

public class StudentController : ControllerBase

{

                /// <summary>

                /// Get total number of students

                /// </summary>

                /// <returns>count as Integer</returns>

                /// <response code=”200″>Success</response> 

                /// <response code=”400″>If error or the item is null</response>            

                [HttpGet(“TotalStudent/{Classroom}”)]

                public IActionResult Get(int classroomId)

                {

                                ///Call Business Object/Layer

                                object resultJSON = _StudentBusiness.GetTotalStudentCount(classroomId));

                                return Ok(resultJSON);

                }

}



Step 2: Business Layer

public object GetTotalStudentCount(int classroomId)

{

                /// Your business logic

                ValidateIsExist(classroomId);

 

                /// Call DataAccess Object/Layer

                object resultObjects = _StudentServices.GetTotalStudentCount(classroomId);

                return resultObjects;

}



Step 3: Data Access Layer

public object GetTotalStudentCount(int classroomId)

{

                /// Connect Your DB and get the result

 

                object resultObjects = _dbContext.StudentTable.Where(w => w.id == classroomId).Count();

                return resultObjects;

}



Basic Question & Answers:

Q: Web API supports which protocol?

A: Web App supports HTTP protocol.

Q: What is Request Verbs or HTTP Verbs or HTTP Methods?

A: The most-commonly-used HTTP verbs (or methods) are

  • GET – To retrieve data (all or based on primary/unique Id).
  • POST – To Create a new record.
  • PUT – To update the existing record (need to pass the existing primary id as a parameter).
  • DELETE – To delete an existing record by primary Id.

Q: What are HTTP status codes?

A: Response Header of each API response contains the HTTP Status Code

Commonly seen HTTP Status Codes are:

Status Codes (Message)Description
200 OKRequest is Ok
201 CreatedThe request is complete, and a new resource is created.
202 AcceptedThe request is accepted for processing, but the processing is not complete.
204 No ContentA status code and a header are given in the response, but there is no entity-body in the reply.
301 Moved PermanentlyThe requested page has moved to a new URL.
400 Bad RequestThe server did not understand the request.
401 UnauthorizedThe requested page needs a username and a password.
403 ForbiddenAccess is forbidden to the requested page.
404 Not FoundThe server cannot find the requested page.
500 Internal Server ErrorThe request was not completed. The server met an unexpected condition.
502 Bad GatewayThe request was not completed. The server received an invalid response from the upstream server.
503 Service UnavailableThe request was not completed. The server is temporarily overloading or down.

Leave a Reply

Your email address will not be published. Required fields are marked *