Creating a simple JSON based API using Node.js Last Updated : 07 Mar, 2024 Comments Improve Suggest changes Like Article Like Report API (Application Programming Interface) results are commonly used by applications, sometimes to withdraw information from their huge databases, check the status of a particular data, or sometimes they depend on third-party APIs to display additional information related to users. Here we will make a simple public JSON based Chemistry API, which can be used in any programming language to display the results. Steps to create API: Step 1: Install Node.js from based on your Operating System, and system requirements. Step 2: Initialize a blank folder, in which you would build the API. In that folder, create a blank JS file, called index.js Step 3: Open command prompt and go to the created directory by using CD (path of the folder). Step 4: Type npm init and go through the steps, answering the asked questions. For building the Chemistry API, we would need to create an empty JSON file, and paste the JSON data. Now we have to include a few NodeJS libraries and set up the port for the API. javascript var fs = require('fs'); // json file with the data var data = fs.readFileSync('chemistry.json'); var elements = JSON.parse(data); const express = require("express"); const app = express(); // To solve the cors issue const cors=require('cors'); app.listen(process.env.PORT, () => console.log("Server Start at the Port")); app.use(express.static('public')); app.use(cors()); Designing the endpoints of the API: javascript // when get request is made, alldata() is called app.get('/elements', alldata); function alldata(request, response) { // Returns all information about the elements response.send(elements); } Here, "/elements" is the endpoint, and when a get request is made to the API, at /elements endpoint, alldata() function is called. Now alldata() function has two parameters (request and response). Using response.send(), the response is returned to the user. javascript app.get('/elements/:element/', searchElement); function searchElement(request, response) { var word = request.params.element; word = word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); if(elements[word]) { var reply = elements[word]; } else { var reply = { status:"Not Found" } } response.send(reply); } "/:element/" is the variable endpoint, for requests regarding any specific element. Combined all three sections of code to create index.js file. Filename: index.js javascript var fs = require('fs'); // json file with the data var data = fs.readFileSync('chemistry.json'); var elements = JSON.parse(data); const express = require("express"); const app = express(); // To solve the cors issue const cors=require('cors'); app.listen(process.env.PORT, () => console.log("Server Start at the Port")); app.use(express.static('public')); app.use(cors()); // when get request is made, alldata() is called app.get('/elements', alldata); function alldata(request, response) { // Returns all information about the elements response.send(elements); } app.get('/elements/:element/', searchElement); function searchElement(request, response) { var word = request.params.element; word = word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); if(elements[word]) { var reply = elements[word]; } else { var reply = { status:"Not Found" } } response.send(reply); } Now, you all can create a simple JSON based API, and upload the whole code in GitHub, and host it on Heroku. We have included the Github repository link and API docs, feel free to explore the projec. Comment More infoAdvertise with us Next Article Creating a simple JSON based API using Node.js A aanishabhattacharyya Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies Node.js Technical Scripter 2019 Node.js-Misc +2 More Similar Reads Using Restify to Create a Simple API in Node.js Restify is an npm package that is used to create efficient and scalable RESTful APIs in Nodejs. The process of creating APIs using Restify is super simple. Building a RESTful API is a common requirement for many web applications. Restify is a popular Node.js framework that makes it easy to create RE 6 min read Creating a REST API Backend using Node.js, Express and Postgres Creating a REST API backend with Node.js, Express, and PostgreSQL offers a powerful, scalable solution for server-side development. It enables efficient data management and seamless integration with modern web applications.This backend can do Query operations on the PostgreSQL database and provide t 4 min read How to Add Data in JSON File using Node.js ? JSON stands for Javascript Object Notation. It is one of the easiest ways to exchange information between applications and is generally used by websites/APIs to communicate. For getting started with Node.js, refer this article.Prerequisites:NPM NodeApproachTo add data in JSON file using the node js 4 min read Build a Social Media REST API Using Node.js: A Complete Guide Developers build an API(Application Programming Interface) that allows other systems to interact with their Applicationâs functionalities and data. In simple words, API is a set of protocols, rules, and tools that allow different software applications to access allowed functionalities, and data and 15+ min read How to Create a Simple Server Using ExpressJS? The server plays an important role in the development of the web application. It helps in managing API requests and communication between the client and the backend. ExpressJS is the fast and famous framework of the Node.Js which is used for creating the server.In this article, we will create a simp 3 min read Travel Planning App API using Node & Express.js In this article, weâll walk through the step-by-step process of creating a Travel Planning App With Node and ExpressJS. This application will provide users with the ability to plan their trips by searching for destinations, booking flights and hotels, submitting reviews, receiving notifications, sha 10 min read How to create a REST API using json-server npm package ? This article describes how to use the json-server package as a fully working REST API.What is json-server?json-server is an npm(Node Package Manager) module/package, used for creating a REST API effortlessly. Data is communicated in JSON(JavaScript Object Notation) format between client and server.I 4 min read Convert XML data into JSON using Node.js Converting XML data to JSON in Node.js is a common task, especially when working with APIs or services that return data in XML format. This conversion allows you to manipulate the data more easily using JavaScript objects.ApproachTo convert XML to JSON in Node we will use libraries like xml2js. Firs 2 min read How to Parse JSON using Node.js? JSON stands for JavaScript Object Notation. The text-based data exchange format data exchange format allows you to transfer data between different languages and platforms. JavaScript is commonly used to interact with JSON files. JSON parsing is a common task when working with data from APIs, configu 2 min read How to Build a RESTful API Using Node, Express, and MongoDB ? This article guides developers through the process of creating a RESTful API using Node.js, Express.js, and MongoDB. It covers setting up the environment, defining routes, implementing CRUD operations, and integrating with MongoDB for data storage, providing a comprehensive introduction to building 6 min read Like