What is an API and why is it important?

What is an API and why is it important?

Let's look at what is an API, its importance, and a quick guide on how to make one.

Aashish Panthi
·Jun 22, 2022·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

API, when I heard this word for the first time, I couldn't digest but I think I am now clear enough to tell you what this technical word means in a layman's term which will also be digestible (at least I think so).

Giphy GIF

What is an API?

API stands for Application Programming Interface. API is a function or simple block of code that help to communicate two applications. For example, if you have written your frontend in React and you want to write a backend that gives life to your application. If you are writing frontend on React then probably you will choose express. And there you will make an API.

I think the above example was confusing if you are a beginner, so let's go simple. Suppose you are making a website with HTML, CSS, and vanilla javascript only. Then think for a minute about how you will get the real-time update of the weather to show in that beautiful UI. That is where my friend you need to get some data from another server that you may or may not own. In the case of weather, you will probably use this weather API. Just look at the illustration below.

API.png

Why is it so important?

I think you are satisfied with the above use case but that is just the tip of the iceberg. There are a lot more use cases, some of them are here:

  • Suppose you've different websites (it can be branches of a company) then you can use the same server to cut some cost and with some security rules, I think it will work just fine.
  • If you want to share the same backend between your website and mobile application, then API would be a great fit.
  • It's a thing that both frontend developers and backend developers love APIs, frontend developers don't need to worry about the backend code and backend developers just need to make some endpoints where the frontend can hit.

I think the insane popularity of API is due to its nature of simplicity, and ease to use. and we don't even need any frontend setup to test API, tools like postman are there for making our life easy.

How to make an API?

Let's quickly make an API and see what it looks like. The programming language I am using here is javascript and the framework is express.js. Here are a few simple steps to follow to create an API.

1. Create an express project

To create an express project, you need the following tools installed:

  • Node js
  • NPM
  • VS code Now you have all of the required tools, open VS code in a folder where you want to create a new project. Then open the terminal there and run the below command.
npm init -y

Now you will see a package.json file, and now the first thing you want to do is create a new file. The name should be index.js. Then you will need to install the express.js package with the following command.

npm install express

2. Create a server

After you are all set with your project file, now we need to create a server in order to serve our service, does that make any sense? Creating and starting an express server is not a very big deal.

const express = require("express"); // import the express package

const app = express(); 

const PORT =  5000; // Specify a port no to listen the requests

// And now listen to the specified port no
app.listen(PORT, () => {
  console.log(`Server running on ${PORT}`);
});

3. Create an API

We have our server up and running, now the only thing that remained there is to create our API. The following lines of code will listen to our get request.

app.get("/", (req,res) => {
  res.send("Hello, World!");
});

The final code looks a lot like this😅

Express server basic setup with REST API

4. Running the server

Use the following command on the terminal to start the server we just created:

node index.js

Now you see a message Server running on 5000 on the terminal. After that, you can visit localhost:5000 or if you have used another port then localhost: and respective port no. Right now if we hit get request to our API, then we get this message:

Express server get request

Congrats, you now know what is an API, why it is used, and how to create a simple API with express.js

 
Share this