Application Programming Interface(API) is a set of rules which define how different software applications can connect and exchange data with each other securely. APIs act as a connecter between web server and application. Every time we are sending a request in internet, we are using APIs. Developers need not know how an API is built, they can simply use the interface to connect with different services.
How does an API connect different applications?
A Client raises a request to access the data from a particular server. This request is processed from client to server using API’s Uniform Resource Identifier (URI) and includes a request verb, header and body.
API calls the external server after receiving a valid request.
The server responds with the requested information.
The data is sent by the API to the respective application.
APIs are used mainly because they provide easy and secure access to data. For example when we are using third party payment application, the payment process doesn’t require access to user card details, hence the API creates a unique token for this transaction and includes it in the API call for the server.
Types of API protocols.
There are certain protocols developed with a set of pre-defined rules which specifies the data types and commands used. Lets see the most commonly used APIs i.e SOAP and REST APIs.
SOAP(Simple Object Access Protocol) APIs
Soap is a protocol which was designed to ensure that different applications built on different protocols and languages could exchange data easily. It provides reliable and trusted way to exchange messages between systems. SOAP is older it was developed in 1990s when the web was emerging. SOAP gives you much more details about the structure of the request and response, the message content and the way its encoded.
SOAP uses XML format for messages being sent and received. It provides four dimensions:
- Envelope is the essential part of the message, which begins and ends with its tags enveloping it.
- Header defines the specifics.
- Body includes request or response.
- Fault shows any errors that could occur during request, response.
REST(Representational State transfer) APIs
REST is an architectural style for the application programming interface (APIs). REST API is most commonly used because it provides simple uniform interfaces. These are used to make data, media, content and other digital resources available through URL, so that they can be consumed within web.
Image : shareurcodes.com
REST is easy to use than SOAP, it uses simple http GET, PUT, POST, DELETE requests from the client to the server to access or update the information. The web services can give response in any form like CSV, JSON, RSS,XML unlike SOAP which supports only XML. The idea is that user can get output in the form which is supported by his application and the language he is using.
HTTP methods of REST API:
GET: It is used to retrieve resource information only and does not modify any data. When the resource is found then the HTTP must return response code 200 (OK) - along the response body usually in XML/JSON. But when the resource is NOT found on server then it must return HTTP response code 404 (NOT FOUND).
POST: It is used when we want to create a new subordinate resources like creating a file in a directory. When the resource is created then the HTTP response code is 201 (Created), with an entity that describes the status of the request and a location header.
PUT: It is used when we want to update the existing resource, if no resource is present then the API decides to create a new resource or not. If new resource is created, then the HTTP response code is 201 (Created), if existing resource is modified then 200 (OK) or 204 (No content) should be sent to show that the request is completed.
DELETE: It is used to delete the resources. When the resource is successfully deleted, then the HTTP response should be code 200 (OK) else 204 (No Content). Multiple DELETE requests on a particular resource, will show the HTTP response as 404 (NOT FOUND) as its already deleted.
PATCH: It is used to make partial update on a resource. PUT also modifies the resource, but PATCH method is the correct option, to partially update the resource and PUT should be used when we want to replace the resource entirely.