What is API vs REST API?

W

Hello Guys,

In this article, I will try to give you detail information about the API and REST API. So, keep up with the article.

Let’s get started… 🙂

What is an API?

An application programming interface (API) is a toolset that programmers can use to help them create software. To put it simply, API’s just allow applications to communicate with one another.

To describe the concept in further detail, you need to comprehend the basics.

Let’s start with my favorite analogy of a restaurant menu.

API Example

When you go to a restaurant to eat, you choose from the selection on the menu. From this point, you convey your choices to the waiter. He/she then takes your choices to the chef, they prepare your meal and it’s returned to you for a nice meal. From this point, you may ask yourself, do you know what the chef used in your fish or steak? How was it prepared? This back and forth analogy serves as a starting point for this explanation.

Why? Because with an API, you really don’t know what’s going on behind the scenes at the restaurant. All you really know is that you gave your food order, it was processed and out came your dinner on a nice plate.

But why would we need an API?

Imagine the following scenario: You (as in, your application, or your client, this could be a web browser) wants to access another app’s data or functionality. For example, perhaps you want to access all Twitter tweets that mention the #corona hashtag.

You could email Twitter and ask for a spreadsheet of all these tweets. But then you’d have to find a way to import that spreadsheet into your application; and, even if you stored them in a database, as we have been, the data would become outdated very quickly. It would be impossible to keep it up to date.

It would be better and simpler for Twitter to provide you a way to query their application to get that data, so that you can view or use it in your own application. It would stay up to date automatically that way.

An API brokers access to a different application to provide functionality or access to data, so data can be included in different applications.

They allow us to go get data from outside sources.

  1. We can send an API a request detailing the information we want.
  2. APIs allow our sites to alter data on other applications, too. For instance, you’ve probably seen “Share on Facebook” or “Share on Twitter” buttons on miscellaneous websites. When/if you click one of these buttons, the site you’re visiting can communicate with your Facebook or Twitter account, and alter its data by adding new status or tweet.

Types of Web APIs

  • Open APIs: Also known as Public API, there are no restrictions to access these types of APIs because they are publicly available.
    • Google Maps, OpenWeatherMap, YouTube API
  • Partner APIs: A developer needs specific rights or licenses in order to access this type of API because they are not available to the public.
  • Internal APIs: Also known as Private APIs, only internal systems expose this type of API. These are usually designed for internal use within a company. The company uses this type of API among the different internal teams to be able to improve its products and services.

In case, If you have any query related API then do watch Hitesh Sir video. I have given the link below.

Let’s also study about Web Service as well.

What is a Web Service?

A Web service is a way for two machine to communicate with each other over a network.

A web server running on a computer listens for requests from other computers. When a request from another computer is received, over a network, the Web service returns the requested resources. This resource could be JSON, XML, an HTML file, Images, Audio Files, etc.

We can say that a web service is a standardized medium to propagate communication between the client and server applications on the World Wide Web (Internet).

1 ) XML (eXtensible Markup Language) is a standardized format for storing and sending data. Similar to HTML, XML stores data by wrapping it in descriptive tags.

2) JSON (JavaScript Object Notation) is similar to XML in that it also stores and enables you to send data in a standardized format. JSON just uses a different, object-based methodology for systematically storing data.

3) HTTP (HyperText Transfer Protocol) is the foundation of transferring data and communications on the internet.

4) SOAP (Simple Object Access Protocol) is a messaging protocol used for exchanging structured information [XML data] over a network.

5) REST (Representational State Transfer) is a standardized architecture style that can be used when creating a web API.


Now, you may be confused between API and Web service so let’s talk about that.

So, What’s the difference?

You might be wondering to yourself, APIs and Web services sound like the same thing. It’s a way for two computers to communicate with each other over the internet, right? Well, not quite.

As mentioned in the section about “What is an API?“, not all APIs are accessible over the internet(a network), while Web Services must always be accessed through a network. That’s the difference right there.

All Web Services are APIs, but not all APIs are Web Services.

What is REST API?

A REST API is a standardized architecture style for creating a Web Service API. One of the requirements to be a REST API is the utilization of HTTP methods to make a request over a network.

REST was officially defined by computer scientist Roy Fielding in 2000 during his Ph.D. dissertation. It essentially changed the way applications are built. The implementation of the frontend “client” can be built completely independently from the backend “server.”

REST stands for REpresentational State Transfer, meaning when a REST API is called, the server will transfer a representation of the requested resources state to the client.

For instance, when a developer requests Twitter API to fetch a user’s object (a resource), the API will send back the state of that user, their name, followers, and posts shared on Twitter.

This representation of the state can be in a JSON, XML, or HTML format.

Typical data transfer protocols, such as SOAP (Simple Object Access Protocol), offer great capabilities in terms of data security and integrity. Moreover, SOAP offers in-built retry logic to compensate for unsuccessful communications. But such protocols are also tough to work on. REST is a simpler alternative that has developed exponentially in the past few years.

REST is usually preferred over the more robust SOAP as the former uses lesser bandwidth, making it more appropriate for the world wide web services. As it uses HTTP protocol to fetch data or carry out operations in several formats (like XML and JSON), it allows for quicker operations.

An API protocol defines the rules for API calls: it specifies accepted data types and commands. Different API architectures specify different protocol constraints. Here we are talking about REST.

REST (representational state transfer) is a very popular web API architecture. To be a REST API, an API must adhere to certain architectural constraints, or principles, including:

  • Client-server architecture: The interface is separated from the backend and data storage. This allows for flexibility, and for different components to evolve independent of each other.
  • Statelessness: It states that the server does not does not store any session data.
  • Cacheability: Caching refers to storing the server response in the client itself, so that a client need not make a server request for the same resource again and again. A server response should have information about how caching is to be done, so that a client caches the response for a time-period.
  • The Uniform Interface is a constraint that is placed on REST services in order to simplify things (and ensure that services can be managed independently from one another) and they are as follows :
    • Identification of Resources:
      • This basically states that a request will need to identify the resources that it is looking for (via a URL). Additionally, the resources themselves may not have any relationship with how they are returned to the client (i.e. you can request a given resource in JSON, XML, or some other format based on how your API was built).
    • Resource Manipulation through Representation:
      • This basically states that when a client has a given resource, along with any metadata, that they should have enough information to either modify or delete the resource (i.e. there isn’t anything left out that it would need to call to the API to do these things).
    • Self Descriptive Messages:
      • A message should have enough information to let the server know how to process it (i.e. the type of request, mime types, etc.)
    • Hypermedia as the Engine of Application State (HATEOAS):
      • Accessing an API should be similar to accessing a web page (i.e. you should be able to discover other areas of the API much as a user would discover links on a page). Basically a response can contain links and point to other areas of the API that are available. The attached link to HATEOAS goes into this in a bit more detail.

A REST request from the client to the server usually consists of the following components:

  • URL Path [https://api.example.com/customer]
  • HTTP Method [GET, PUT, POST, DELETE]
    • C (Create) -> POST
    • R (Read) -> GET
    • U (Update) -> PUT
    • D (Delete) -> DELETE
  • Header â€“ (optional) additional information that the client needs to pass along in the request such as Authorization credentials, Content-Type of the body, User-Agent to define what type of application is making the request, and more]
  • Parameters â€“ (optional) variable fields that alter how the resource will be returned.
  • Body â€“ (optional) contains data that needs to be sent to the server.

Here’s How the REST API Works:

Let’s say that you want to see what your best friend posted on Instagram. To do this, you need to go on the app and open up your friends Instagram page.

In this example, your Instagram app [the client], would make a request to Instagram’s server [the server] to request your friend’s Instagram profile. This request would be a GET request to the /users endpoint and in the parameters of the request your friend’s account ID would be included.

In the same way that you use a GET request to retrieve data, a POST request would be used to create data on a platform. So let’s use the example of posting an image to Instagram. This request would be a POST request to the /media endpoint with a body of the image and parameters with your caption.

[Disclaimer: This is not an accurate example request to the Instagram API. The post media endpoint is a private endpoint.]

Benefits of REST APIs

The reason REST is so great is that it offers a standardized methodology for making requests to an API. Once you learn one REST API, other REST APIs are going to function in a similar way.

If an API is available over the internet, there is no need to install additional software within your application. You can access the data from any application that is connected to the same network as the API.

With separated development on the client and server, the client code can be updated without affecting the server, and the server code can be updated without affecting the server. This is assuming the changes are developed in a backward-compatible way.



In the next blog, I’ll explain how to use postman tool. Till then stay tuned.

Thanks.

About the author

iamsparkler
By iamsparkler

Most common tags

%d bloggers like this: