![]() |
| What is a Web API? |
Hello Everyone, As a web developer, we often come across the term Web API. In this article, we will discuss what is a web API, when and why to use a web API.
Introduction
API stands for Application Programming Interface, is a way to communicate between two application. The two applications can request or exchange information using some set of rules defined in it with the help of API.
For e.g: Appliation A accepts the input in the form of JSON and it will also return the response in JSON format. So, Application B must pass the input in the required format.
What is a Web API?
Web API is also an API, used to communicate between two application over web. It uses HTTP protocol to request or exchange information. Web APIs are commonly used to enable the interaction between web applications, mobile apps, and other software components.
For example, smartphones calls Weather Web APIs to retrive weather Info. While e-commerce platform calls payment APIs to process transaction accross different channels.
As we already know, In traditional web applications, when a client (such as a web browser) sends a request to a server to perform an action, like saving user information, the server typically processes the request and returns an HTML view as a response. This HTML view contains the content and structure of the page that the browser should render.
On the other hand, using Web APIs (Application Programming Interfaces), the server provides data in a format like JSON or XML rather than a fully formatted HTML page. This allows for more flexibility in how the client (which could be a web browser, mobile app, or another server) consumes and displays the data. The client can then manipulate and integrate this data into its own user interface.
For example, suppose we want to create a weather application. For that we need real time information regarding the temperature, humidity, current weather info etc. to display them in our application. We don't need to build infrastructure to measure weather details rather we can use data from a climate data provider. We just need to integrate their Web API provided. This is an example of a use of Web API. We can integrate this API for any type of application like web, mobile, desktop etc.
Web API is not limited to a particular system. Web APIs are designed to facilitate communication and interaction between different software over the web, allowing them to work together regardless of the underlying technologies, platforms, or programming languages.
Why Web API?
To discuss the use of Web API, let us understand by using a most useful example.
Suppose, we're developing a web application for a client, where we've used the traditional way to develop a web application. Here traditional way refers to the development of web applications using a server-centric architecture, where much of the processing and rendering of content occurs on the server side. Your web application is running successfully on web browsers. But then suddenly client want to develop a mobile and desktop application as well. Now, the problem will arise that we have to recreate the business logic for each platform such as mobile, desktop as well. It will be difficult to manage when any changes comes. For that, we've to update each application logic deployed for each platform.
What is the solution?
The solution is simple. To handle such type of situation, Web API is the best solution.
By using a web API, we decouple the front end (mobile and desktop applications) from the back end (server-side logic). This separation allows for easier maintenance, updates, and scalability. Changes made to the business logic on the server side can be reflected across all platforms without the need to modify each application individually.
Let's understand this with the above example. Now, we've created a web API for our backend implementation (server-side logic). We can call that web API to fulfill the server-related task. Same API will be called from web, mobile, desktop application as well as other software components to fulfill the server related task. Every time any server-related task comes, we just need to update in one place i.e. inside our web API implementation. That's it.
How Web API Works?
Here is the visual representation of Web API working.
In the above, client such as browser/mobile/desktop calls the server using HTTP protocol named as HTTP Request. Server then process the request and return the response in the form JSON/XML format. Now the client can process the response and display it in the UI.
There are different types of architecture style used in web APIs for communication between client and server. Here are some of them:
- REST
- SOAP
- GraphQL
- RPC
REST is most commonly used due to its simplicity. We'll discuss more about REST architecture in the later sessions.
Here are some common terms used in Web API using the REST architecture:
- Endpoint: An endpoint is a specific URL or URI that represents a particular function or resource. Endpoints define the different operations that can be performed, such as reading records/a record or creating, updating, or deleting a record/records.
- HTTP Methods: HTTP methods are typically used to define the type of operation used in a resource. Common HTTP methods used:
- GET (To retrieve data)
- POST (To save data)
- PUT (To update data)
- DELETE (To delete a data)
- Request : Sending a request to a server via web or mobile app to perform an operation. The server processes the request based on the operation represented in the endpoint.
- Response: After processing the request by the server it will return the result in the form of response. It will return the response in any form such as JSON, XML, etc. instead of an html view.
Here is a simple example of Web API:
![]() |
| Web API Request and Response Example |
When to use web API?
Here are some scenarios where using web API is the best solution:-
- Dynamic content update: If you have some dynamic content that needs to be updated regularly. you could use a simple API to fetch that content from a server. This can be useful for things like news feeds, weather updates, or any data that changes frequently.
- Update data without page refresh: There is some scenarios you want to update data for a web page without reloading the whole page. Web API helps us update the data without reloading the whole page. This will improve user experience on that web application.
- Developing apps for multiple platform : If you want to develop an app for multiple platform such web, mobile, desktop etc. Web API is the best solution for the server-side processing task.
- Platform independent services or Data sharing services: When you want to develop a platform independent service, which can be used by any platform or services and can be accessed anywhere in the internet. Web API is the best solution. Weather API is an example.
- IoT (Internet of things): IoT devices can communicate with the server using web API to exchange data.
Conclusion
If your requirement is to create an application (having server side logic) for multiple platforms or want a decoupled application or want to update data without the full page reload or want to create services that can be consumed by any application, then my friend using web APIs in the backend will be the best solution. It will reduce cost, and time and is easy to maintain.
In our next article, we'll discuss on how to create a web API using ASP.NET Core. Here is the link to Create a Web API using ASP.NET Core.
Kindly share your experience with the above article. Comment if you want to know more.
.png)
.png)
.png)
Comments
Post a Comment