Hello and welcome back to yet another blog, and today we will be discussing one of the coolest things that you could ever think of, 'APIs'. Now, when I say coolest, I mean it. Have you ever thought, how you are actually able to book a flight ticket? Or check the availability of train tickets using any third-party application? Well, I am going to tell you how this actually happens. Cool! let’s get started.
What is an API?
API stands for Application Programming Interface, which primarily allows two applications to communicate. It is generally used to program the Graphical User Interface or the GUI. It is a set of protocols, routines, and tools that are used to build a software application. API makes it easy to develop a program by providing the building blocks, and the only thing that a developer needs to do is use that block without requiring to know the backend operations.
In simple words, suppose you are in a restaurant, and you need to order your food, from the list of items mentioned in the menu. But you need a medium to communicate, between you and the kitchen of that restaurant, and that medium is a waiter. So, the waiter takes your request, forwards it to the kitchen, and then comes back with your ordered meal. Now, you got your food, and you don’t really need to know the backend process of how the food was made.
This is what an API actually does, it provides a bunch of operation, and developers need to choose the required one and use it in their application.
But sometimes a developer may provide their own data to the API, to get the desired result. In other words, it’s like, you can provide your own ingredient to the kitchen and they will work with it. Isn’t that cool!
Types of API
API comes in many forms, and API designers can choose from various protocols and standards to design an API, based on its functionality and purpose. Here are some of the common types of API.
1. 1. Open APIs: - It is also known as public APIs or External APIs, and is available with minimal restrictions for developers and another user. It may require a simple registration, or maybe completely open to using the API Key. This kind of APIs is intended for external users to access data or services.
2. 2. Internal APIs: - Unlike the Open APIs, Internal APIs are hidden from the public(external) usage. They are designed to share resources within an organization or company. This allows different sections of the company to utilize each other’s tools, data, and programs.
3. 3. Partner APIs: - They are similar to open APIs, but have restricted access which is usually controlled by third-party API gateways. It usually includes paid services.
4. 4. Composite APIs: - This allows developers to access multiple endpoints in a single call. This could include different endpoint of a single API or can be multiple services or data sources.
Now that you are aware of different types of APIs, let’s move on to discussing the API Architecture and Protocols. However, there are different API protocols and architecture, but here I will be discussing REST API and GraphQL.1.) What Is A REST API?
REST is an acronym for “Representational State Transfer”. It is a set of rules that developers follow when they create an API. One of the rules states that you should be able to get a resource when you link to a specific URL.
Every URL is considered to be requested, while the data sent back to you is known as a response.
In simple words, suppose you open up YouTube, and search for Iron Man in the Search field, and then you see a list of videos about Iron Man. REST API is kind of a similar thing, you search for something, and then you get a list of the result from the service that you are requesting from.
Anatomy of a Request
Before moving on to anything, it is really important to know that a request is comprised of Four Things.
1. Endpoint
2. Method
3. Headers
4. Data
Let’s move forward into understanding each one of them in a simpler way.
1) What is an Endpoint?
An endpoint is a URL that you request for. Its structure looks like this.
root-endpoint/?
Where root-endpoint is the primary address of the API that you are requesting from. For example, the root-endpoint of GitHub’s API is https://api.github.com.
A Path determines the resources that you are looking for. Paths can be accessed in a similar way as we link to parts of any website. For example: Suppose you need to get a list of all posts for the label “Technology” in our website Easy Tech Gyaan, then you need to go to https://www.easytechgyaan.com/search/label/Technology .
Where https://www.easytechgyaan.com is the root-endpoint and /search/label/Technology is the path.
Now, to know what paths are available, you need to look at the documentation of that API.
Query parameters: Now, the Query parameter is not technically a part of the REST Architecture, but you can find it’s used in a lot of APIs. So, let’s talk about this in the simplest way possible, query parameters provide you with the liberty to modify your request with some key-value pairs.
Always remember that a query parameter starts with a (?), and is separated by (&).
For example: - ?query1=value1&query2=value2
Or see this: https://www.easytechgyaan.com/search/label/Technology?&max-results=7
Here this query parameter allows me to set the key-value “ ?&max-results=7 “ and I can see a maximum of 7 posts with the label Technology in a request.
2) What is a Method?
A method in simple terms is the type of request that you can send to the server. These are the five types of requests that you can send to the server.
· GET
· POST
· PUT
· PATCH
· DELETE
These five methods provide meaning to your requests and are used to perform four possible actions called CRUD (Create, Read, Update, Delete).
3) Headers
Headers are used to provide additional information with an HTTP request, it is used to provide information to both client and server. It can be used for many purposes such as providing information or authenticating the body content.
Visit this website to get a list of some valid headers on MDN’s HTTP Headers Reference.
4) Data
Data (sometimes also known as “body” or “message”) contains information, that you want it to be sent to the server. This option can only be used with POST, PUT, PATCH, or DELETE.
You can use -d or --data to send data through curl.
For example: -
curl -X POST <URL> -d property1=value1
You can also create multiple -d options to send multiple data fields.
For example: -
curl -X POST <URL> -d property1=value1 -d property2=value2
Let’s move forward into discussing a little bit about authentication.
AUTHENTICATION
Will you be ok, if someone accesses your bank account without your permission? Definitely not! The same is the case with developers, they put certain measures, so that you perform the actions when you are authorized to do so.
Since the POST, PUT, PATCH, and DELETE request is used to alter the database, they are put behind some Authentication. In some cases, even the GET request requires authentication. For Example: To check your bank balance you need access to your bank account.
There are two main ways to authenticate yourself over the internet.
With Username and Password.
With a secret token.
2.) What is GraphQL?
In recent times, GraphQL has come up to be a revolutionary alternative to REST APIs, but everything has its pros and cons. In some cases, GraphQL proves to be the best, whereas in some other, REST APIs are still preferred to be better. So, let's move forward and understand what is GraphQL.
GraphQL is considered to be one of the most modern ways of building and querying APIs. GraphQL is a syntax that elaborates on the ways to request data and is commonly used to load data from a server to a client. GraphQL is a query language for APIs and uptime for satisfying those queries with your existing data.
GraphQL is used by some of the leading companies such as Facebook, GitHub, Coursera, Shopify, Pinterest, Intuit, etc…
GraphQL has mainly three characteristics:- Client can exactly specify what data they need.
- It is easier to accommodate data from multiple sources.
- A Type system is used to describe data.
Having said that, what is the difference
between GraphQL and REST APIs.
Difference Between GraphQL and
REST APIs.
Where REST API is a structured concept for network-based software, GraphQL is a query language, specification, and a set of tools that work over a single endpoint using HTTP. Well in recent times, REST is used to create the APIs, whereas GraphQL is used to add flexibility and performance.
With this, I would like to wrap-up this article. Definitely, there are much broader aspects while using these technologies, but I have tried to keep it simple and focus on the major idea behind REST and GraphQL. I hope you enjoyed reading this article, and if you find this interesting, kindly share it with your friends, colleagues, and to whomever, this article can benefit.
No comments:
Please let me know if you liked the post. Do share it with your friends