Build a Node.js API in 5 minutes

Aaksha Dubey
Fasal Engineering
Published in
7 min readOct 3, 2022


APIs are the basis of much of what keeps people digitally connected. In this article, we will learn about how to build a simple API in Node.js in just 5 minutes.


From apps on our phones to complicated flight control systems, APIs bring systems together to create single platforms. They allow applications or IoT devices to access data and create gateways to interact with external systems.

APIs are the connective tissues to bind the digital world together.

What is an API?

An API is an interface that enables data exchange between two independent software components. An API serves as a bridge between internal and external software operations, creating an exchange of information so seamless that it often goes unnoticed by the end user.

A real-world example

Every time you go to a restaurant, you will find a waiter to show you your table, bring you the menu card, help you with today’s special dish, take your order, and serve you whatever you need. In any case, you need not enter the cookhouse of a restaurant.

A waiter shields you from all the complicated stuff going behind the scenes. You don’t have to worry about the baking convection, dishes, pouring drinks, or managing stocks.

The waiter is the interface between you and all other services of the restaurant. Hence, you can definitely interact with the restaurant yet be shielded from the complexity of kitchen management.

In this manner, you can consider the waiter as an API of the restaurant, and now you can indeed understand the importance of a waiter and an API. An API plays the role of a messenger that takes your request to the system, tells the system what to do, and brings you back a response from the system.

Read more about Web APIs in the below article,

What is REST?

REST is a set of architectural constraints, not a protocol or a standard. API developers can implement REST in a variety of ways.

When a client request is made via a RESTful API, it transfers a representation of the state of the resource to the requester or endpoint. This information, or representation, is delivered in one of several formats via HTTP: JSON (Javascript Object Notation), HTML, XLT, Python, PHP, or plain text.

JSON is the most generally popular file format to use because, despite its name, it’s language-agnostic, as well as readable by both humans and machines.

Advantages of building APIs in Node.js


A key factor when using node.js is the speed that it renders to the API. Using a single thread, all the related tasks are quickly performed. Additionally, node.js also allows building an API that is scalable and secure too

Standardized development

An API may function even in unprecedented infrastructures so before you build an API, you must know the standard processes across industries. With node.js, a developer does not need to worry about development process standards that will make an API functional across multiple interfaces


Node.js best security practices make it easy for developers to catch any kind of security vulnerability. Its ORM/ODM validates every kind of access to the API database.

Node.js provides wonderful support to developers for the development of API. A real-time API that is dynamic can be built using node.js.

Creating a two-way channel for IT solutions, node.js creates circulation in a data-friendly manner. Experts believe node.js is capable of working in multiple environments and that is the major contributor to its acceptance as a framework that supports the development of an effective and efficient API.

Step by Step guide on how to build secure Node.js rest APIs in 05 minutes

Preparatory Instructions

  • For getting started, you need to download the most stable version and release of NodeJS.
  • Next, once you download the Node.js binaries, install them in your system with the help of a certain set of instructions noted on the page relative to your platform.
  • For confirming the installation process, close all or any of the cmd instances that are open or may be running in the background. Next, start a whole new instance.
  • For displaying the version of NPM and the installed Node, type the following 2 commands
npm -v
node -v

Initializing a new app

  • Primarily, you can create the files by hand. A very basic Node app involves a .js file and a package.json file. To be more precise, the package.json file comprises certain features and properties: 1) the first one is the name that carries the name of the app and 2) the second one is the version that displays the version of your app along with the description and entry point.
  • You can run some of the scripts that are included if you wish to perform dependencies, repetitive activities, author name, license, and devDependencies.
  • The next method is to make use of an NPM tool. Though it takes time and is not the fastest way, it is the simplest one. All you need to do is open your cmd in that particular folder where you wish to create your app. Subsequently, type npm init to create your package.json file collectively.
  • Next you need to install the Express tool for generating an entire Express template and not the package.json individually.
npm install -g express-generator

This helps the express-generator tool to install, thereby generating a full express app.

To install Express, use the following command,

npm install express --save

With this, you can edit the JSON file as well as add Express as a dependency because you use the save flag.

Creating an app

  • Create an app.js file and add the following code:
var express = require("express");
var app = express();
app.listen(3000, () => {
console.log("Server running on port 3000");
  • The first line of the command needs Express and makes use of the express variable to show it.
  • The next line initializes express with the use of brackets that initializes an express server and places the initialized server inside the variable app. Hence, from now, whenever you wish to use your express server, you first need to use the app variable that represents your app.
  • Moving ahead, set your app to listen to port 3000 and generate a callback. This step will verify that your server is currently running on port 3000.
  • To sum up, you can now access your app at http://localhost:3000. However, you cannot gain anything as you have not configured or customized your server for listening to any of the events.

Set up request handlers

  • Receiving, processing, and returning a response to a request is the server’s main duty. Therefore, routes should be used to handle these requests.
    - GET request for getting data
    - POST request to send the data safely and securely
    - PUT request for updating the data
    - DELETE request for deleting or erasing all the data.
  • Now let’s create a GET request that will return a list of Crops.
  • Under the var app=express(), type the code given below
app.get("/url", (req, res, next) => {res.json(["Rice","Wheat","Millet","Cotton","jute"]);});
  • This function leads the express app to use the URL handle “/URL” to activate the callback that follows. Three parameters are adopted by this callback.
    -Req or the request body that holds all the information about the request
    -Res or the response body that manages each of the response functions such as: .render() for rendering templates and .json() for returning the JSON data.

Run Your App

  • To run your app run the following command in your terminal
node app.js
  • This will appear in your terminal which makes sure that your app is now running on port 3000. In order to see the data, open your browser and type http://localhost:3000/url.
  • You will notice this on entering. This is however raw data. The data that is sent back is an array of strings.

Read more about Node JS concepts here,


In this case, all you had to do was a request for a certain endpoint and get data in return. This is the most basic level of API that you must use in order to analyze and understand how REST APIs function.

I hope this blog helped you understand why Node.js is the ideal programming language to use for creating RESTful APIs and how to build the same in 05 minutes.