Everything about APIs
Tags: webdev
Published: 2021-05-07 10:39:10

![API Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10nemykkaq0i6xa5ovq3.png)

Content List:
* <a href="#beginners">**Beginner's Content**</a>
* <a href="#skillful">**Content for the Skillful**</a>
* <a href="#advance">**Advanced Content**</a>
* <a href="#conclusion">**Conclusion**</a>
* <a href="#learnMore">**Resources to Learn More**</a>
* <a href="#publicAPI">**List of Public APIs**</a>
* <a href="#aboutMe">**About Me**</a>


## <p id="beginners">API for Beginners</p>
Let’s start with some basics
### What is API?

It is called Application Programming Interface. It is used to hide something very very complex behind something as simple as a function call. Wikipedia explains it as:
> In [computing](https://en.wikipedia.org/wiki/Computing), an **application programming interface** (**API**) is an [interface](https://en.wikipedia.org/wiki/Interface_(computing)) that defines interactions between multiple [software applications](https://en.wikipedia.org/wiki/Software_application) or mixed [hardware](https://en.wikipedia.org/wiki/Computer_hardware)-software intermediaries.[[1]](https://en.wikipedia.org/wiki/API#cite_note-1) It defines the kinds of [calls](https://en.wikipedia.org/wiki/System_call) or requests that can be made, how to make them, the [data formats](https://en.wikipedia.org/wiki/Data_type) that should be used, the conventions to follow.		

### Duh! explain it more simply !!!

I’ll take an example, Our example for this article will be a restaurant. Let’s say today you had an awesome day (hope that you did) and you do not want to cook for yourself, so you are out for dinner in as awesome a restaurant as your day. What do you do here? You give the order to the waiter. What does he do? he brings you your food. So in this scenario, the waiter is an API, and he hides all the complexity of cooking food, decorating it on a plate, etc. This is what an API is (in a gist).

## <p id="skillful">API for -   Skillful</p>

### So, are there multiple types or what?

Yes, as in every programming concept, this too has multiple types.

* **Open APIs:** These are also known as public APIs (sometimes called external APIs). These almost all of the time require an API key to be accessed. They are open for everyone to be used. They can be used by developers, corporates, and anyone else.

* **Partner APIs:** Kind of very similar to Open APIs, but are restricted to be accessed only via a partnered gateway. If you have much experience in deployment over GCP or AWS or similar, and you use some APIs like SendGrid, Mailgun, etc via GCP/AWS/etc then you are using the partnered version of the APIs.

* **Composite APIs:** These are APIs that allow to access multiple APIs together. Have you ever tried ordering something from a website without registering? and have you noticed that it automatically creates an account for you based on the order details? Yes, exactly, most probably that website used a composite API to place the order and create an account.

* **Internal APIs:** These are exactly the opposite of open APIs. They are supposed to be used internally by the company itself. They allow different teams or sections of a business to consume each other’s tools, data, and programs. It helps in maintaining security, access control, and the trails of process.

These are the types of Web Service APIs (APIs that can be accessed over HTTP or the internet in simple words). They are generalized as Third Party APIs.

Another most common category of API is Browser APIs. These APIs are accessible in your browser for browser. Then there are product APIs that are meant only for the product that they are developed for and will be accessible only when you install that product.  There is a whole set of Standard APIs as well, which are the proposed standards, these can be found on Openstack API specifications and W3C, and more. Another set of APIs are called System/Embedded APIs, these APIs are more specific to the system, like Windows system APIs, mobile fingerprint API, etc.

For this article, we will only focus on Web Service APIs
## <p id="advance">API for -   Advanced Readers</p>
Getting back to our example, where you have reached the restaurant and have ordered the food. (This was the part of using the front end to call the API)

Have you ever wondered what and how does the waiter writes so that the cook knows exactly what to cook and how to cook, here comes our next question

###  Should the APIs be called in specific ways or are they smart enough to understand our natural way of speaking?

The answer here is, obviously, APIs have some architecture and protocols. So an API protocol defines the rules for API calls. 

Majorly there are 5 API architectures:
1 SOAP
2 REST
3 RPC
4 Websckets and Webhooks
5 GraphQL
I'll explain all of them in a brief.

#### 1) SOAP (Simple Object Access Protocol)
Prior to the Rest, this was one of the most used protocols, it is heavy for transport, but provides better security than the Rest API. It is intended to be extensible and neutral. It uses XML as a format to transfer data. It also uses WSDL (Web Services Definition Language) to publish a definition of its interface. So what it means is that it uses WSDL to publish the format in which the response will be received( in very general and layman terms ).

#### 2) REST (REpresentational State Transfer)
In today's internet, REST is one of the most followed architectures ( it is not considered as a protocol). Most of the services offered today use REST. It requires the application to adhere to some rules (called architectural constraints/principles).

 - The system should be implemented over **server-client architecture** (which most of the time is satisfied in general). This makes the system flexible as different components of the system can be changed independently.
 - The system should be **Stateless**: This means that the client (generally the frontend) must include all the information for the server to fulfill the request, it can be as query parameters, headers, or URI. The server should not store/maintain any states (can be thought of as sessions), all of the information should be provided by the client itself.
 - **Cacheability**: This constraint is pretty straight, but can trouble you sometimes especially while debugging. It says that a client can cache a response, so the response must explicitly state that it can be cached or not.
 - **Layered system**: It is also pretty straightforward, it says that the system must exist in layered format, so irrespective of whether the communication happens directly or via load balancer or different intermediaries, the API should be able to communicate.
 - **Uniform Interface**: This is one of the most important constraints, it says that the interaction between the client and server should be uniform across all the clients ( irrespective of device or application for eg. mobile apps, web, etc). This further gets broken down into 4 sub principles: **Resource-Based**, **Manipulation of Resources Through Representations**, **Self-descriptive Messages**, **Hypermedia as the Engine of Application State (HATEOAS)**.
  I won't be explaining these in more detail as they can be a whole blog themselves and have already have written a big para on rest, but you can visit [Stackoverflow](https://stackoverflow.com/q/25172600/8217589). 

![Richardson Maturity Model as a goalpost to achieving truly complete and useful APIs](https://content.altexsoft.com/media/2020/05/word-image-57.png)
*Richardson Maturity Model as a goalpost to achieving truly complete and useful APIs, Source: [_Kristopher Sandoval_](https://nordicapis.com/what-is-the-richardson-maturity-model)*

#### 3) RPC (Remote Procedure Calls)
This is a very interesting API Architecture. In the above APIs, the only thing that happens is the transfer of data, but RPC is different, it is designed to call a function instead of simply transferring data. So if you are familiar with programming, then the name itself will suggest that it calls procedures. If used for CRUD (Create, Read, Update, Delete) operations, it is like sending data fields like normally we would do, but there's a downside to this, and that is, the client is entirely in charge of everything. The client must know which methods (endpoints) to hit at what time, to construct its workflow out of the un-descriptive endpoints.

RPC is a concept that has a lot of specifications and is implemented using:

-   [XML-RPC](https://en.wikipedia.org/wiki/XML-RPC): To give a brief, It simply uses XML as a data transfer format. It is also older than SOAP. XML-RPC uses minimum bandwidth and is much simpler than SOAP. XML-RPC was problematic because ensuring data types of XML payloads is tough. In XML, a lot of things are just strings.
-   [JSON-RPC](https://en.wikipedia.org/wiki/JSON-RPC): This protocol is similar to XML-RPC but instead of using XML format to transfer data it uses JSON. It proved out to be better than XML-RPC because to some extent it helped with the issue of data types that XML had, but it had troubles in differentiating different data formats like integers and decimals.
-   [Simple Object Access Protocol (SOAP)](https://en.wikipedia.org/wiki/SOAP): SOAP-RPC tried to resolve the issue with JSON-RPC and XML-RPC. For JSON-RPC you need to layer metadata on top to describe things such as which fields correspond to which data types. This became part of the basis for SOAP, which used [XML Schema](https://wikipedia.org/wiki/XML_schema) and a [Web Services Description Language (WSDL)](https://wikipedia.org/wiki/Web_Services_Description_Language) to explain what went where and what it contained.

One of the standardized formats of RPC is called [gRPC](https://grpc.io) it is widely accepted and used in the industry, it has been created and standardized by Google (internally the previous version was called **Stubby**). It is [open-sourced](https://github.com/grpc/grpc). By default, gRPC uses [Protocol Buffers](https://developers.google.com/protocol-buffers/docs/overview) (although it can be used with other data formats such as JSON). [You can see who uses gRPC and why here](https://grpc.io/about/#whos-using-grpc-and-why).
#### 4) Webhooks / Websockets API
This is for real-time APIs where data is streamed back to the calling system using technology like Websockets or Webhooks. 

Sometimes **Webhooks** is referred to as a reverse API, but this isn’t entirely true. They don’t run backward, but instead, there doesn’t need to be a request initiated on your end, data is sent whenever there’s new data available. Webhooks are commonly used to perform smaller requests and tasks, however, there are situations where a webhook is more appropriate than an entire API.  


The **WebSocket API** is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

> **Note:** Even though for this article I have categorized them under the same umbrella, these two technologies are different.
> * [Webhooks](http://en.wikipedia.org/wiki/Webhook) are for server-to-server communication. They work by one server telling another server
> that it wants data sent to a certain URL when something happens.
> * [Websockets](http://en.wikipedia.org/wiki/WebSocket)  are (usually) for a server to browser communication. The server hosts a WebSocket server, and clients can open a connection to that server. This is
> popular now mostly because it is faster and less resource-hogging than
> older ways of solving the problem, like  [long-polling](https://stackoverflow.com/questions/11077857/what-are-long-polling-websockets-server-sent-events-sse-and-comet)/[COMET](http://en.wikipedia.org/wiki/Comet_%28programming%29). It is  [possible to connect 2 servers using WebSockets](https://github.com/liris/websocket-client), but that is not usually what they are used for.

This note was referenced from this [Stackoverflow Reply](https://stackoverflow.com/a/24747947/8217589)

There are also Pub/Sub APIs that I have not covered in this post. It works on similar principles as these.

Another newer technology that I haven't written in detail is WebRTC, it is somewhat similar to web sockets but one of the major differences is that it works in real-time, especially used for audio/video communications. It is an emerging technology, with its usage increasing by the day. To get some basic understanding between WebRTC and Webscokets to refer to [this article](https://bloggeek.me/webrtc-vs-websockets)


#### 5) GraphQL

> GraphQL is a query language for APIs and a runtime for fulfilling
> those queries with your existing data. GraphQL provides a complete and
> understandable description of the data in your API gives clients the
> power to ask for exactly what they need and nothing more makes it
> easier to evolve APIs over time, and enables powerful developer tools.

From the official [GraphQL website](https://graphql.org)
This is a somewhat newer API technology. GraphQL APIs have a hypermedia-like ability to use a single query to fetch the required data across multiple resources. But GraphQL APIs also borrow from concepts that we’ve observed in both REST and RPC-styled APIs.

It is a technology developed by Facebook in 2012. GraphQL was used internally for their mobile applications to reduce network usage employing its specific data-fetching capabilities. Since GraphQL specifications and reference implementation in JavaScript were open-sourced in 2015, major programming languages now support it, including Python, Java, C#, Node.js, and more. The GraphQL ecosystem is expanding with libraries and powerful tools like Apollo, GraphiQL, and GraphQL Explorer.

To understand the differences in REST and GraphQL refer to the table below:
![https://content.altexsoft.com/media/2019/03/word-image-5.png](https://content.altexsoft.com/media/2019/03/word-image-5.png)
*Major differences in GraphQL and REST endpoints (Image taken from AltexSoft article)*

## <p id="conclusion"> Conclusion </p>

So now you have a good understanding of how the waiter (in the case of the web a client) takes your order (the actions performed on UI) and organizes it accordingly so that a cook can prepare your meal (backend can process your data). These help in keeping the flow seamless even if there are tens of consecutive customers ordering (many clients interacting on the web). 

Indeed, the web is a wonderful restaurant that is open and ready 24/7/365 to serve you your order. Now with this knowledge, I'll end the article, and hope to see your amazing dish (project) served (hosted) in the restaurant (web). 

Below are few resources that I have referred to for this article and otherwise. 

I have also collected few <a href="#publicAPI">Public APIs</a> for you to learn, test, and use as per your needs. 

If you want to learn, how to test and use an API from basics, then I'll recommend [Postman API 101 Public Workspace](https://www.postman.com/postman/workspace/postman-student-api-101-workshop/overview). Follow this to get a better grasp. They also have a webinar on this you can see it on [their webinars page](https://www.postman.com/webinars/apis-101)

## <p id="learnMore"> Resources to Learn More </p>
 * [SOAP vs REST on Postman Blog](https://blog.postman.com/soap-vs-rest/)
 * [Why rest over RPC](https://apihandyman.io/do-you-really-know-why-you-prefer-rest-over-rpc)
 * [SOAP and XML-RPC](https://developer.atlassian.com/server/confluence/confluence-xml-rpc-and-soap-apis)
 * [Awesome List for resources on APIs](https://github.com/Kikobeats/awesome-api)
 * [Awesome List for APIs Security](https://github.com/arainho/awesome-api-security)
 * [AltexSoft SOAP vs REST vs Graph vs RPC](https://www.altexsoft.com/blog/soap-vs-rest-vs-graphql-vs-rpc):  It is also used as one of the references for this article.
 * [SmashingMagzine Understanding REST and RPC](https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis): A must-read to clear out the confusion and use case of RPC and REST. It is also used as one of the references for this article.
 * [Understanding RPC, REST, GraphQL](https://apisyouwonthate.com/blog/understanding-rpc-rest-and-graphql): Another must-read for a better understanding of these APIs. It is also used as one of the references for this article.
 * [Web API Checklist by Mathieu Fenniak](https://mathieu.fenniak.net/the-api-checklist)

 #### SOAP Resources
* [A wonderful blog by AltexSoft](https://www.altexsoft.com/blog/engineering/what-is-soap-formats-protocols-message-structure-and-how-soap-is-different-from-rest)
* [SOAP API 101 by Postman](https://blog.postman.com/soap-api-definition)
* [Blog on SOAP API by Stoplight](https://stoplight.io/api-types/soap-api)
* [Wikipedia](https://en.wikipedia.org/wiki/SOAP) 
#### Rest Resources
* [StackOverflow Blog](https://stackoverflow.blog/2020/03/02/best-practices-for-rest-api-design/)
* [Rest 101 on Postman Blog](https://blog.postman.com/rest-api-definition)
 * [Microsoft Rest API Design Blog](https://docs.microsoft.com/en-us/azure/architecture/best-practices/api-design)
 * [Microsoft REST API guidelines](https://github.com/Microsoft/api-guidelines/blob/master/Guidelines.md)
 * [The chapter on REST in Roy Fielding's dissertation on Network Architecture, "Architectural Styles and the Design of Network-based Software Architectures"](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm)
 * [How to design a RESTful API architecture from a human-language spec By Filipe Ximenesand Flávio Juvenal on O'Reilly](https://www.oreilly.com/content/how-to-design-a-restful-api-architecture-from-a-human-language-spec)
* [Restapitutorial](https://www.restapitutorial.com/lessons/whatisrest.html)
* [Awesome list for resources on REST APIs](https://github.com/marmelab/awesome-rest)
 * [Wikipedia](https://en.wikipedia.org/wiki/Representational_state_transfer)

#### Websockets / Webhooks

 * [Websockets on Html5rocks](https://www.html5rocks.com/en/tutorials/websockets/basics)
 * [Websockets for fun and profit on Stackoverflow blog](https://stackoverflow.blog/2019/12/18/websockets-for-fun-and-profit)
 * [Benefits of Websockets on Websocket.org](https://www.websocket.org/quantum.html)
 * [How Postman Engineering Handles a Million Concurrent Connections](https://blog.postman.com/postman-engineering-million-concurrent-connections)
 * [What are webhooks? on Zapier Blog](https://zapier.com/blog/what-are-webhooks)
 * [Webhooks on Github](https://docs.github.com/en/developers/webhooks-and-events): A amazing way to learn webhooks using Github's webhooks API.
 * [Webhook definitive Guide on Requestbin blog](https://requestbin.com/blog/working-with-webhooks)
 * [Webhook.site](https://webhook.site): With this site, you instantly get a unique, random URL that you can use to test and debug Webhooks and HTTP requests, as well as to create your own workflows using the [Custom Actions](https://docs.webhook.site/custom-actions.html) graphical editor or [WebhookScript](https://docs.webhook.site/webhookscript.html), a simple scripting language, to transform, validate and process HTTP requests.


#### GraphQL

 * [HowToGraphql](https://www.howtographql.com): A website built to teach GraphQL entirely for free, a must-visit website.
 * [Official GraphQL Docs to learn GraphQL](https://graphql.org/learn)
 * [Official Best Practices](https://graphql.org/learn/best-practices)
 * [GraphQL Weekly Newsletter](https://www.graphqlweekly.com)
 * [GraphQL Features, Pros, and Cons by AltexSoft](https://www.altexsoft.com/blog/engineering/graphql-core-features-architecture-pros-and-cons)
 * [What is GraphQL on Redis Blog](https://www.redhat.com/en/topics/api/what-is-graphql)
 * [Practical Guide of GraphQL APIs on Techbeacon](https://techbeacon.com/app-dev-testing/practical-guide-graphql-future-apis)

## <p id="publicAPI">API Collections </p>
#### Mixed Lists
* [A whole list of APIs on Postman](https://www.postman.com/explore/apis)
* [Free-for-dev List](https://free-for.dev/#/?id=apis-data-and-ml)
* [List of Public APIs](https://github.com/public-apis/public-apis)
* [APIList.fun](https://apilist.fun)
* [Any-api.com](https://any-api.com)
* [Public-apis.io](https://public-apis.io)
* [APIs guru](https://apis.guru/browse-apis)
* [Public Datasets API](https://data.world/datasets/api)
* [Twitter](https://developer.twitter.com/en/docs)
* [Google Maps](https://developers.google.com/maps)
* [Facebook](https://developers.facebook.com/docs)
* [Telegram](https://core.telegram.org/api)
* [Youtube](https://developers.google.com/youtube)
* [Pinterest](https://developers.pinterest.com)
* [Slack WebAPI](https://api.slack.com/web)
* [Most famous for example purposes, **NASA** APIs](https://api.nasa.gov)
#### SOAP
 * [You can find some public APIs and their examples  here from Postman](https://documenter.getpostman.com/view/8854915/Szf26WHn?version=latest)
 * [Public SOAP API workspace on Postman](https://www.postman.com/cs-demo/workspace/public-soap-apis/overview)
#### Rest
* [Public REST API workspace on Postman](https://www.postman.com/cs-demo/workspace/public-rest-apis/overview) 
* [JSONPlaceholder fake API for testing and prototyping](https://jsonplaceholder.typicode.com)
* [REST countries](https://restcountries.eu)

#### RPC
* [Google APIs](https://googleapis.github.io/HowToRPC.html)
* [Awesome gRPC](https://github.com/grpc-ecosystem/awesome-grpc)
* [Awesome-json-RPC](https://github.com/shanejonas/awesome-json-rpc)

#### Webhooks / Websockets

 * [Facebook Webhook API](https://developers.facebook.com/docs/graph-api/webhooks)

#### GraphQL
* [Public GraphQL API workspace on Postman](https://www.postman.com/cs-demo/workspace/public-graphql-apis/overview)
* [Facebook Graph API](https://developers.facebook.com/docs/graph-api/reference/page)
* [Moonhighway list](https://moonhighway.com/public-graphql-apis)

## <p id="aboutMe"> About Me </p>

Hey there,
I am Omkar Agrawal a freelance full-stack web developer, currently, I am in the final year of my Bachelors's in Information Technology. I am also a Postman Student Leader and Postman Student Expert. I am a tech enthusiast that loves to read and learn on anything that can increase my knowledge, but especially on technology.

I am available for Backend / Fullstack development job positions, you can find me on 
 * [Omkaragrawal.dev](https://omkaragrawal.dev)
 * [Linkedin](https://linkedin.com/in/omkaragrawal)

If you loved this blog then please support it by giving a thumbs up on this post and on other channels :
 * [Medium](https://omkaragrawal.medium.com/everything-about-apis-234f26f92a6)
 * [blogs.omkaragrawal.dev](https://blogs.omkaragrawal.dev/everything-about-apis)