What is API?
API (Application Programming Interface) is a particular set of rules (‘code’) and specifications that software programs can follow to communicate with each other. For example, you have a hardware button on your TV which turns it ON/OFF. Now suppose you want to turn ON the TV from a distance. You just press the ON/OFF button on a remote control and the TV set action on the command given by the remote control. This is how API works! Think of TV as a software program having services and resources, and the TV Remote control as another software program which use these resources. The method (or radio/infra red waves) through which a remote control communicates with a TV set is an API.
When and where it is used?
It’s used when 2 or more separate application need to work together to achieve something they can’t do alone.When interaction involve sending data over network, at that moment Web Service comes into the picture.
What is Web Service?
As the word sounds, they are not new fundamental business concept, it just a technology which permits old business concept to work together over the internet using some protocol. i.e. Oxford Dictionary API, in which you will send word, and in return it gives meaning of that word.
Modern web service communications are nearly always handled over HTTP. But the format of the message that are being sent and received can be different. Let’s consider one scenario for more understanding,
One service provider (A) is building application which shows latest designer clothes like amazon. For that he need to set up database to add products information (maybe it’s price, product name, images etc.) and one program to get that resources and display on application(Android, iOS, Web client) which is called as API call.
Note: Mark that this is limited to his application only, this will not allow other application to access his local data.
Now there is one another provider (B) also want to build same kind of application, So, B has two option:
- Either he do same stuff as A does. (make API)
- Or tell A to give his API & allow access to his database to retrieve & modify data. (A’s API called over web –Web Service)
So now what will happen B will request for A’s API by passing request parameter (required for API) and at the back-end API call translate to SQL queries and gives SQL result into result suitable to API response.
An API is almost always better. You’ll get significant benefits in terms of re usability of the data and portability of your code, as well as avoiding coupling (interaction of application with application’s database) between your app and the database. But this scenario is works without network.
Because as you know Web service facilitates interaction between two machines over a network. But API acts as an interface between two different applications so that they can communicate with each other.
- All Web services are APIs but all APIs are not Web services.
- API does not need to be web based.
- API Will not explore the database but Web service do
- A Web service uses only three styles of use: SOAP, REST and XML-RPC for communication whereas API may use any style for communication.
- Web services might not perform all the operations that an API would
What is REST API?
One Architecture which is used to build client/server network application. REST stands for REpresentational State Transfer. REST is not a standard it’s a style (architecture) of describing the transferring state of something by its representation. Implementing REST is very simple as compared to other methods like SOAP, CORBA, WSDL etc., it basically works on HTTP protocol.
REST describes a set of architectural principles by which data can be transmitted over a standardized protocol (such as HTTP). REST does not contain an additional messaging layer and focuses on design rules, its use http protocol, and http header include every information which you need, you just have to pass body information with representation.
The key attributes of a RESTful applications are: All communication is via http GET, POST, PUT, DELETE AND all items are addressed via a standard URL of the form http://www.optimumbrew.com/app/appname/endpoint
i.e. only a pure URL with no parameters ,URL identifies the thing. And the GET, POST, PUT, DELETE identifies what you want to do.
There are brief benefits of RESTful application:
- The RESTful Web services are completely stateless (no record of previous interaction).
- There is no standard set of rules to describe the REST Web services interface. (The service producer and service consumer need to have a common understanding of the context as well as the content being passed.)
- REST-full api is platform-independent.
- REST service is lightweight because you do not need to release changes to clients. You simply make changes to your logic, retaining URLS and the response should remain the same.
- REST services are easy to integrate with the existing websites and are exposed with XML so the HTML pages can consume the same with ease.There is hardly any need to refactor the existing website architecture. This makes developers more productive and comfortable as they will not have to rewrite everything from scratch and just need to add on the existing functionality.
- Reduce the complexity of our communication by its representation (We do not have to discuss about our needs.)
- A client can access the resource using the unique URI and a representation of the resource is returned.
- REST uses a smaller message format than SOAP.
- REST provides better performance, as well as lowers costs over time. Moreover, there is no intensive processing required, thus it’s much faster than traditional SOAP.
At the end, when you’re publishing a complex application program interface (API) to the outside world, SOAP will be more useful. But when something with a lower learning curve, and with lightweight and faster results and simple transactions (i.e., CRUD operations) is needed, REST is preferred.
How you will use REST API?
Let’s continue with our previous example of two service provider A & B. Now we consider A as an API provider & B as an API consumer.
To pass data over the internet you definitely need to deal with JSON formatted data in your application.
JSON: Java Script Object Notation is one format that is used for data interchanging in web service response instead of XML. Because it is light weight as compared to XML and less network bandwidth consuming.