API programming

API & REST API

What is an API?

Api is short for Application Programming Interface. It is used in computer programs to communicate with other applications and/or services.
It acts as a bridge between different softwares and devices.
Each time we use an app like Facebook or Instagram, send an instant message or check the weather on our phone, we use nothing but an API.

Everything from sharing photos to shopping online, booking a cab, a flight or a hotel, playing games is all done via APIs.
It is API because the technology is innovating at a faster than the predicted rate.

If we look at the below diagram, which is a basic information flow diagram depicting the position of an Application Programming Interface in the whole schema of system integration.

Importance of API is –

When we search for nearby restaurants on any browser, it lays out their locations on Google Maps instead of creating separate maps.
Thus, via the Google Map API, the browser passes the information it wants – restaurant addresses, rating, timings, and a lot more –
to an internal Google Maps function that then returns a Map object with restaurant pins in it at the proper locations.
It allows programmers to create apps without having to learn how to code.

Take some examples –

So let’s take Instagram for example. When you open up Instagram, there’s this bar at the top of the screen where you can search through different hashtags or accounts. There’s also another bar at the bottom which has these buttons like “Follow”, “Like” etc. These are all links to third

Why Should You Use APIs?

It is easy to understand that APIs can allow different computer programs to share or request features, services, and other content.
Suppose you think that why different businesses are using APIs comes down to the range of benefits of APIs to consumers and providers.
The API consumers can request access to different server resources or automate different tasks internally.

Advantages of API –

The key advantages of APIs to consumers include improved operations, increased innovation, and improved customer satisfaction.
It is easy to unlock business opportunities and increase business revenue.

Rest API?

REST stands for Representational State Transfer.
So, REST means using HTTP (HyperText Transport Protocol) to transfer resources such as HTML documents, images, audio files,
A REST API is a web service (a server) which accepts requests from client applications using HTTP (HyperText Transfer Protocol).
The clients use a common protocol such as XML or JSON to send data to the server.
The server processes the request, generates a response and sends it back to the client.

Methods of Rest Api –

GET –

The GET method is used to retrieve resources from a server.
Example – HTTP GET ‘http://www.apidomain.com/users/123’

POST –

POST method is used to create a new resource into the collection of resources on a server.
Example – HTTP POST ‘http://www.apidomain.com/users’

PUT –

PUT is used to update the existing resource on the server and it updates the full resource.
Example – HTTP PUT ‘http://www.apidomain.com/users/123’

PATCH –

PATCH is used to update the existing resource on the server and it updates a portion of the resource.
Example – HTTP PATCH ‘http://www.apidomain.com/users/123’

DELETE –

DELETE Method is used to delete the resources from a server. It deletes resource identified by the Request-URI.

Creating a Web API Project

We need the following tools installed on our computer:

Open Visual Studio 2022 and select Create a new project and then select ASP.NET Core Web API:

and give a name to your project in the following screen and then click Next.

In the next screen, select .NET 6.0 as the framework and click Create:

At this point we have a starter project as follows:

In the Program.cs we see that Swagger support is added automatically to our project:

Now, let’s run (Ctlr+F5) the project to see the default output. When the browser opens and the Swagger UI is shown, select the GET method in the WeatherForecast part and then select Try It Out and Execute:

Also, you can use the curl URL shown in the Swagger UI for this method and see the result of the URL in the browser:

When we run the application, the default URL comes from the launchSettings.json:

And the result values come from the GET method of the WeatherForecastController:

As you see, values here are hard coded and randomness is added to generate different values.

In our Web API, we will create our own records in an SQL server database and will be able to view, update and delete them through REST API endpoints.

If you feel my blogs are useful then please share, follow and comment on it. Also, you can connect with me over linkedIn: https://www.linkedin.com/in/neeni-bharti-b64b9a252/