×

Search anything:

Different types of API Protocols

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

In this article, we shall briefly introduce APIs and then look at the various types of API protocols that are employed for their usage. The Different types of API Protocols covered are:

  1. SOAP
  2. REST
  3. JSON-RPC
  4. gRPC
  5. GraphQL
  6. XML-RPC
  7. Apache Thrift

Let's begin!

What are APIs?

API stands for Application Programming Interface and refers to the act of communication between two or more computers or more specifically, computer programs. Like user interfaces exist to provide a platform for users to communicate with the system, an API exists to for softwares to interact with each other, such as executing instructions, fetching data from servers, etc. A software can also interact with different softwares through multiple APIs, so as to provide a one-place-for-everything kind of support and become a hub of data accessing.
APIs also offer convenience to a user's experience. It is analogous to re-usability of code. Let's see this through an example:

A good common-place instance is when we use a food-delivery application on our smart-phones and pay for it via online means, say your PayTM wallet or Amazon Pay account. When we select this wallet option, the wallet's interface opens up. We enter our PIN numbers and process the payment. This switch from the food delivery application's interface to the wallet's interface is due to usage of an API, which borrows the wallet's functionality and lets us process the payment conveniently.
Other examples include independent flight/hotel booking sites or weather widgets on our browsers. These utilities do not maintain their own copy of data but instead use APIs to fetch updated data from the airplane companies' websites or a weather data website and display it accordingly.

Working

APIs enable communication between services via request-response cycles. These cycles may/may not require an internet connection. The application in need of data from another service or server sends a request, which is conveyed through API protocols and the required data is sent back from the server.

API Protocols

APIs come with a defined set of calls and techniques which are utilized by developers to successfully integrate them in their softwares. The rules which govern the use and application of these calls are termed as API Protocols. The protocols define the accepted commands and data types among other things and set a certain standard for API usage.

Let us explore some popular API Protocols one by one:

SOAP (Simple Objects Access Protocol)

It is an API protocol which employs XML to enable API communication. It is the oldest API protocol in use, emerging in 1998. SOAP uses XML files to transfer data between web services. These XML files are sent over HTTP/HTTPS transmissions as is common on the internet. However, SOAP also provides flexibility and enables data transmission over other protocols as well such as Transmission Control Protocol(TCP), Simple Mail Transport Protocol(SMTP), User Data Protocol (UDP), etc.
Messages in SOAP are encoded in XML specifically and have a proper defined format:

  • Envelope : Literally envelopes the entire message/data in tags.
  • Header : Defines all extra information that might be needed to process this data. This is an optional element.
  • Body : This is where we actually write the request for data required/ where all the requested data is added.
  • Fault : Defines all the errors that may arise in the data during/due to transmission and measures to handle them.

While SOAP is highly prevalent on the web as well as flexible with its transmission channel, its reliance on XML makes it rigid due to its strict formatting. Difficulties in debugging XML also proves to be a hindrance.

REST (Representational State Transfer)

REST protocols overcome SOAP's dependency on XML by supporting data transmission in multiple formats such as JSON (most prominent), HTML, Python, plain text as well as media files. However, REST relies solely on HTTP/HTTPS for data transmission, taking away SOAP's adaptability to other protocols. APIs which employ the REST protocol are called RESTful APIs.

REST APIs follow a client-server architecture and must be stateless. Stateless communication implies that no client data is stored between GET requests. These GET requests must be distinct and disconnected.REST assigns every operation a unique URL, so when the server receives a request, it knows which instructions to execute to fulfil the request. REST also supports caching. So, the browser can store the results obtained from the request locally and retrieve it periodically as needed, thereby increasing speed and efficiency.
A typical REST request has the following components:

  • Endpoint : The destination URL from which data is being requested.
  • Method : We use predefined methods such as GET, POST, PUT or DELETE to fetch the data. These methods vary from one other. Ex. in when using GET, the data is appended to the end of the URL string, whereas in POST, the data is sent along with the HTTP request.
  • Headers : They define the request's details and dictate the proper format in which the response must be received.
  • body (data) : The actual data sent by the service.

REST APIs provide more freedom during implementation and are lightweight and scalable.

JSON-RPC (JavaScript Object Notation- Remote Procedural Call)

JSON-RPC protocol arrived in the early 2000's and works extensively with JSON to provide limited but straightforward implementation of API communication. While narrow scoped, JSON-RPC defines a set of calls which can easily handle all functionalities defined under this scope and shows an improved performance over REST in these circumstances.

Overall, JSON-RPC is a lightweight and stateless and employs request objects and response objects to establish communication among web services.

gRPC (Google Remote Procedural Call)

As the name indicates, gRPC was developed at Google and released publicly in 2015. It is an open source RPC framework capable of running in most environments.
Unlike above protocols, gRPC lets developers define their own custom functions to enable inter-service communication as required. gRPC uses HTTP as its transport layer and also provides additional facilities such as authentication features, timeouts, flow control, etc. Data is sent in protocol buffers, a language and platform independent mechanism which defines how data can be structured in a simpler intuitive way.

Protocol buffers begin with defining the service first, followed by defining the data structures the service will use. This is compiled by the compiler protoc, which now creates an entire class furnished with your defined data types along with basic set methods in the development language you're working on. Now, this class can be used to implement the detailed workings of the API.

GraphQL

GraphQL was developed at Facebook and released in 2015. It takes a very interesting take on enabling API communication. GraphQL stands for Graph Query Language, and so like database query languages like SQL, GraphQL essentially queries data from the server. We define the data we need as well as its format in the query and GraphQL returns the data in the same format as requested.

This saves us time and memory as only the data which is explicitly needed is queried from the server, instead of importing entire prepackaged files with a lot of extra information.

GraphQL has been developed to support a variety of web development languages.
Issues arise when it comes to caching of data. Since data is requested very specifically, we may have to query other related aspects of data separately when needed, since they weren't queried in our previous request and thus cannot be found in the cache.

XML-RPC(Extensible Markup Language Remote Procedural Call)

XML RPC is very similar to JSON RPC except that data in encoded and shared as XML files using HTTP/HTTPS for transmission. XML uses inbuilt vocabulary to specify the nature of responses and requests. The client dictates the procedure to be called and the supporting parameters in the request, which is then transmitted using HTTP and the receiver sends an XML response which can either be the data called for, or a fault is returned.
XML-RPC is limited by its dependency on XML as complex objects cannot be properly encoded in XML, which cannot contain data which hasn't been defined in its vocabulary.

Apache Thrift

Apache Thrift was also developed at Facebook. But it was built differently than GrapgQL that we saw earlier. Apache Thrift is essentially an implementation of RPC framework which uses a code generation engine combined with a software stack to enable an API. The software stack helps in writing code to define the client and server side. This is done using Thrift files. The code syntax is flexible and intuitive. After this, the code generation engine generates the required code in any programming language specified by the developer.

Thrift was developed with focus on two primary goals: Enabling communication with services written in different languages and scalability. The use of the code generation makes services flexible.

For actual transportation of data, Thrift has runtime libraries which facilitate inter-service communication. Thrift architecture defines these libraries at a different level from the service which the developer writes the code for. So, changes in Thrift can be done easily without having to recompile our modified code from scratch, since the most basic elements remain unaffected by changes. Thrift provides support for HTTP transmission as well apart from binary transport formats.

Thus, through this article at OpenGenus, we have explored the basics of API protocols and their various types. Keep learning!

Different types of API Protocols
Share this