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 this link, 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 initand 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 data from the link chemistry data. Now we have to include a few NodeJS libraries and set up the port for the API.
Designing the endpoints of the API:
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.
“/:element/” is the variable endpoint, for requests regarding any specific element, like this https://chemistrydata.herokuapp.com/elements/Hydrogen
Combined all three sections of code to create
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 project.
GitHub Repo Link: https://github.com/Aanisha/ChemistryDataAPI
API Documentation: Link
- NodeJS | Building simple REST API in express
- Nodejs | Automatic restart NodeJs server with nodemon
- Dockerizing a simple Nodejs app
- Simple POST request using the fetch API
- How to use simple API using AJAX ?
- Simple GET and POST request using Fetch API method by making custom HTTP library
- How to make simple PUT request using fetch API by making custom HTTP library ?
- Simple DELETE request using fetch API by making custom HTTP library
- Creating a REST API Backend using Node.js, Express and Postgres
- Flask - (Creating first simple application)
- How to add API function to a simple PHP Page ?
- Difference between REST API and SOAP API
- Process-based and Thread-based Multitasking
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Cowsay in Nodejs using Requests library
- Nodejs | Web Crawling using Cheerio
- Signup Form Using Nodejs and MongoDB
- Nodejs | authentication using Passportjs and passport-local-mongoose
- NodeJs CRUD Operations Using Mongoose and MongoDB Atlas
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.