Introduction to API and its usage

API and its usage
API and its usage

API is abbreviated as APPLICATION PROGRAMMING INTERFACE. When we always take the word API it sounds complex but in reality, it’s very easy to use and understand!. API is like a common language or interface used by servers to communicate with each other.

There are many types of devices and system configurations present in software eco System. They all need a common data format (language) to communicate With each Other.

Hence, we found a common way to deal with different devices. Therefore, developers started using the API. When we think about any type of web applications or software, they are all connected to remote servers. These applications need an interface to communicate with a server. Here API acts as an interface between them.

For example, When you type www.codingninjas.in into your web browser , a request goes out to codingninjas remote server, Then  your browser receives a response back, it renders the information and display it in your browser.

 Thus, Browser known as client and Coding Ninjas server sends response through API. This means that when you visit any website on your browser you tend to interact with some remote server API. They are not same as the server but it is a part of the server that receives requests and send responses.

API can be explained as a set of functions or subroutines or set of procedures or set of communication protocols, which is used to build software and applications.

Data Formats:

API uses two different formats to transfer data:

  • XML
  • JSON

XML :

XML (Extensible Markup Language) is a markup language (like HTML) which uses tags. We will embed the data inside the opening and closing tag.

The XML above is quite self-descriptive:

  • It has sender information
  • It has receiver information
  • It has a heading
  • It has a message body

Example:

JSON :

JSON (JavaScript Object Notation) derived its name from JavaScript as it implements the same concept of Objects in the JavaScript. It uses a Key-Value pair to store the Data. From the Key, we can fetch its corresponding Data.

Example:

JSON VS XML:

  • XML is much more difficult to parse than JSON
  • JSON is parsed into a ready-to-use JavaScript object
  • For AJAX applications, JSON is faster and easier than XML

Types Of APIs:

There are four main types of APIs:

  • Open APIs: Also known as Public API, there are no restrictions to access these types of APIs because they are publicly available.
  • Partner APIs: A developer needs specific rights or licenses in order to access this type of API because they are not available to the public.
  • Internal APIs: Also known as Private APIs, only internal systems expose this type of API. These are usually designed for internal use within a company. The company uses this type of API among the different internal teams to be able to improve its products and services.
  • Composite APIs: This type of API combines different data and service APIs. It is a sequence of tasks that run synchronously as a result of the execution, and not at the request of a task. Its main uses are to speed up the process of execution and improve the performance of the listeners in the web interfaces.

Why We Need API:

  • The huge benefit is that API’s can work with any common programming language, which makes it very easy and convenient for developers.
  • Imagine building a web application like Uber, which allows the user to view themselves on a map and then book a taxi. now you need access to certain information and have to manage a large set of data. Here you can develop your own API which directly supports your application or else you could use third party API’s like Google map API.
  • So, based on the above scenario, an API is nothing but an endpoint (like a URL) to which you send a request and receive back data.
  • Hence an API is nothing but storing and accessing information from databases/data warehouses. we can also use 3rd party API’s to access the data (reference: Uber example)
  • So, an API is something which eases/enhances your software development

Usage Of API:

There are four request methods:

  • GET: Request made when we need to get the data from Server
  • PUT: Sending data to the server and modify data in server
  • POST: Sending data to the server and getting back the modified data
  • DELETE: Request made when we want to delete some data in the server

What is an API Key?

API keys are used for authentication for user agents that interact with or make requests to an API. These keys can be sent to the API using a query string, a request header or as a cookie. The company that controls the API can use the keys to only allow registered users to make requests, track who makes requests, track usage of the API and block or throttle users who exceed certain limits. When the API sees the key you’re sending, it will then authenticate that you are who you say you are and authorize you to perform a certain action.

What is an API Call?

An API call is when an application uses an API to communicate with a server or another application. This is basically any time a program runs an external piece of code that isn’t part of the main program. For example, if a program is running in Windows and the program needs to open a new file window, it will call an API to tell the operating system that it needs to open a window.

 There are three steps for the usage of API’s:

  • Get an API key
  • Test API endpoints
  • Build your app

Get an API key:

An API Key is a unique string of letters and numbers. You need to add an API key for every request you make. In order to get the API key, you need to register with API server by providing your login credentials. If it is an open API (Public API ) we may not require an API key.

Test API endpoints:

After we receive the API key, we can refer to the API endpoints (according to the rules in the documentation) to check if everything works as we expected. For this, we can use a REST client like Postman. After testing endpoints, we get a response in the form of  JSON  or XML and also we can send data in the form of JSON or XML.

Build Your App:

 After we checked the endpoints and everything works as we expected, we can start creating the application, including calls to the necessary API. You can choose the preferred programming language and start to implement.

Code snippets are available through rapid API for nodeJS, java and other few programming languages.

Build An API:

Awesome! Building an API is totally within reach of any developer who knows a little bit about server-side web application programming and routing, such as with Java Spark, JavaScript Express, ASP.net for C#, Ruby on Rails and so on.

Advantages

  • Security is enhanced when sites use APIs. Whenever you send a request, you aren’t directly linked to a server. You send small amounts of information, the API delivers it, and the server sends it back. This minimises the risk of a breach or someone accessing the backend of a server.
  • Scalability, we can use a single API to build our Application for different platforms like mobile, Website and desktop Application.

Some Useful Public API’s:

To read more on APIs and web development, click here.