What are APIs and REST APIs? - A Simple Explanation by@ismailtlem

What are APIs and REST APIs? - A Simple Explanation

image
Ismail Tlemcani Hacker Noon profile picture

Ismail Tlemcani

A human being, passionated about music, history and coding.

In this post, I will try to demystify the word API and REST APIs in very simple terms so that this post can hopefully help whoever is struggling to understand those concepts.

What is an API?

In very simple terms, an “Application programming interface” refers to any software that allows 2 code entities to talk to each other and to, most importantly, exchange data.

image

A brief history of the term "API"

The term "application program interface" was first cited in the research paper "Data structures and techniques for remote computer graphics” presented at an AFIPS conference in 1968. The authors of the paper used this term to describe the interaction of a computer graphics program with the rest of the computer system.

How do web applications communicate?

Just like humans need a language to communicate with each other, web applications need some rules to send and receive data back and forth. The equivalent of the human language for web applications is the HTTP protocol. Web applications communicate by sending HTTP requests back and forth.

Why do we use APIs?

The core purpose of an API is in making some data available to use for any third-party software in an easy and smooth way. 

Free public APIs

There are many free-to-use public APIs. This great github repository lists many popular free-to-use public APIs for anyone interested in trying them.

API constraints

On top of the HTTP protocol, web applications use additional rules for sending and consuming the data sent in a network. Those rules include the type of data sent and the way this data will be sent in addition to other rules. One of the popular rules set up for this purpose are the rules that makes RESTful APIs.

What does REST Mean?

REST is in simple terms, a set of rules that developers use when designing web applications that are communicating over a network. One of these rules states that you should be able to get a piece of data (called a resource) when you link to a specific URL.

If a system violates any rule of the REST standard, it cannot be considered as RESTful. One of the most popular data formats used with REST APIs is the JSON format.

A Little History of REST

REST was first defined in the PHD dissertation of the computer scientist Roy Fielding named “Architectural Styles and the Design of Network-based Software Architectures” at the University of California in Irvine.

REST Rules

In order for an API to be considered as RESTful, it has to follow the six following rules :

  1. Client server architecture: There must be a clear separation between the backend and the frontend of the application in a client-server architecture
  2. Stateless: No client information is stored between requests and each request should be separate
  3. Uniform interface: The REST API should provide a uniform interface for any client that may want to consume its data.
  4. Layered system: There can be some intermediate backend programs between the client and the server making hierarchical layers in a way that each layer only sees the layer it is interacting with.
  5. Code on demand: The server can send code that can be executed on the client-side, which extends the client functionality.
  6. Cacheability: Every response should be declared as cacheable whenever possible, avoiding unnecessary processing and significantly increasing performance
    image

APIs with React

React apps do usually use some external software components to make API requests. React job is only about rendering HTML and handling user interactions. Two of the most popular ways to consume an API in a React app is the fetch function and the axios library.

  • Axios : It's a standalone 3rd party package that can be installed with npm or yarn. It's my personal recommendation because of its ease of use
  • Fetch : It's a general javascript function used to make http requests

Making an HTTP request with Axios

First, the axios package should be installed in your project using any package manager. In the case of a project using npm, you can install axios using the following command :

npm i axios

For making an http request, you can refer to their documentation to see how you can make use of their different methods to make requests. Here is a simple example of how to do get requests in a react app using axios :

const axios = require('axios')

//make a get request
axios.get('API_URL').then(function (response) {
//instructions to do if the request is successful
}).catch(function (error) {
//handle error
 })

Final words

I would say that APIs are not a difficult concept to grasp. Put simply, an API is just a software interface that allows two programs to communicate with each other. Those communications are based on a set of rules that developers follow in order to make the consumption of the data as easy and as smooth as possible.

If this post was helpful to you in any way, please don't hesitate to share it. You can also follow my Twitter account if you would like to see more content related to web programming.

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.