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 API | 1) URL: http://yoursitename/api/Student/TotalStudent/{Classroom} 2) Http Method: GET |
Target method in your code | public 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).

Basic sample code in .net core – Get API call
API Code flow in below Image.

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 OK | Request is Ok |
201 Created | The request is complete, and a new resource is created. |
202 Accepted | The request is accepted for processing, but the processing is not complete. |
204 No Content | A status code and a header are given in the response, but there is no entity-body in the reply. |
301 Moved Permanently | The requested page has moved to a new URL. |
400 Bad Request | The server did not understand the request. |
401 Unauthorized | The requested page needs a username and a password. |
403 Forbidden | Access is forbidden to the requested page. |
404 Not Found | The server cannot find the requested page. |
500 Internal Server Error | The request was not completed. The server met an unexpected condition. |
502 Bad Gateway | The request was not completed. The server received an invalid response from the upstream server. |
503 Service Unavailable | The request was not completed. The server is temporarily overloading or down. |