How To Start Building a Node.js API for Ethereum Blockchain

Initially, let’s learn how an Ethereum blockchain consists of, after which we coordinate it to one general API in Node.js. Users interact directly through the front-end and the backend vice-versa. Back-end runs alongside the blockchain upon request of the public node. Then Node.js software begins to run through the blockchain.

Since it’s important to know the core basics of a mechanism, let’s get a better understanding through an example- Building a back-end of a decentralised app for storing music. This app will have the Ethereum blockchain as its core system and is responsible for storage and distribution of songs for individual installation and live streaming.

This app will have three basic components :-

Procedure of Authenticity: this is done through an email and let the app have a strong passcode

Having Adequate Room for Data: the data on music is stored through IPFS as the root of storage is kept in the E- Blockchain, if the user wants to get the data back

Permitting Audience in Using Data that is pre-stored: strictly for ones who passed authentication.

Use Node.js to layout these components. Node.js is a cross-platform, open-source back-end runtime environment, applicable to V8 engines. It performs tasks through JavaScript programming codes. It doesn’t need a browser to work on. Now, In order to store data media in IPFS for following the steps mentioned below:-

First, find out the root address.

So as to keep this address recorded write the required steps,

Retrieve the same address from a blockchain through the use of a  programming language called Solidity.

Now the following tools assist in making or running Ethereum and node js development: –

The first essential step is to install a Node App; preferably Node.js. A compiler called Node.js, ensure the latest version of LTS is confirmed –  long term support binary

The next tool is used as a contract habitat for developing and testing, it works like a beneficial pipeline for the blockchain too.

Ganache CL, a compatible tool with Truffle as it is originally found by Truffle Suite. So after building, assembling an emulator to generate blockchain apps, lastly the distribution of smart contracts. It costs cash for the distribution of contracts so, Ganache helps deploy them for free

Remix is a tool with GUI which assists in navigating E- smart contracts throughout deployments and tests.

Web3 is a gallery of libraries and it allows users to use an Ethereum node, the nodes are remotely connected or locally in the contracts through IPC or HTTP

IPFS is a core mechanism in building decentralised apps

Next, a backend should be created with the use of Node.js. In order to make a new Node,js API and learn more about node js development refer to the following instructions: –

Set up an npm Package which is found as a bundle with Node.js binary

Make “blockchain__” a new folder

Click on Terminal in the folder’s directory and use the command :

$ npm init -y && touch server.js routes.js

The start command generates a file called package.json and type “Yes” to every prompt.

Next, create two files and name them server.js and route.js respectively. You will need mention the steps of route processing

During node js development process, install required content for the project built to be simple and systemic. The mentioned packages useful tools used in making this project work :-

  • truffle js
  • express js
  • web3 js
  • Doteny
  • @truffle/comtract
  • nodemon
  • mongodb
  • Short-Id

Try to get the global version of Truffle

Here, —save flag, is responsible for storing content inside package.json. Then -g flag manages the content globally.

In order to save MongoDB database secret URI for future provision, make a .env file through a touch.env command in Terminal. Stored variables made to be transferred to Node.js process environment from the dotenv package.

Next we layout the stages and develop them of our project through additional scripts. Include a beginning script to package.json file. Why? So that the nodemon sever runs while it reboots the server if there are changes and later generate a script that uses a direct node server. Time to start Truffle for those smart contracts, how do you suppose we do that? Through previously installed global package.

Next, type codes in server.js file. It should be cleared so as to represent simple and impartial pairing. Making routes and several factors will then be kept in files.

Libraries that are essential are imported and to permit the use of JSON in our API by app a middleware is introduced, use it, then links with MongoDB database for access, then figure out the data cluster that needs access. Next, select the function and transfer it from the routes file and lastly keep an eye out for tried connections on port 8082. It’s known that server.js serves an essential role to kickstart the app, holding the endpoints for the API created. The bundled content that is important for usage in server.js file is also imported.

For users, some major endpoints are created, they are:-

  • Identification of users through registration endpoint: through digital mail
  • POST /register
  • Requirements: email
  • Users use email to Login
  • POST /login
  • Requirements: email
  • Users can upload data of music file as the front-end converts mp3 files into audio buffer and transfers the buffer to the API
  • POST /upload

Requirements: name, title of music, music file buffer or URL stored

Any request made by a registered user, Access endpoint provide the data of music buffer, it continues to record it after access

  • GET /access/{email}/{id}
  • Requirements: email, id
  • Complete permit to Music library and the outcome given to the authenticated user
  • GET /access/{email}
  • Requirements: email

Next, make route functions in the route.js file. So that route function is transferred at the end of the file to allow the transfer, database storage, and retrieval functions play a major role to do this.

Within the above function, there are other functions that exist under its dimensions, and these API endpoint essentials allow users to find a particular endpoint in the URL. The functions are commanded individually in response to particular requests. There are four essential endpoint functions in recording operations, they are :-

  • To read: get
  • To create: post
  • To update: put
  • To delete: delete

For our project, for registration, logging in, uploading operations, and allowing data operations, add get and post operations,  Next, we shall look at giving a suitable name through the method of db.collection.

To use Solidity in the process of making a blockchain smart contract learn more from newsletters, articles that specify the methods.

After the smart contract is established, integrate it into the back-end API through Web3. The back-end is well-formed Web3 is a useful tool, as mentioned before it’s function is to help developers use remote or local Ethereum nodes for applications. This is done through ganache-cli. We then check if web3 works well in the server.js file. If this doesn’t work, run it on the network port-8545 then build a migrated JSON and truffle-contract package type of contract and link the contract provider with the web3 instance provider.

Time to develop accounts through web3.eth.getAccounts. Development begins with ganache-cli running along the background, the deployed function present in contract class instigates ganache-cli to send essential contract addresses. Once the MongoDB package is installed it’s used as a database in our API. The route page is used to define methods to complete saving and retrieval of music data  tasks.

The API can now get requests, get permits to a database and interact to nodes. The node has a running software. It shifts our function with the help of module.exports.


To launch a newly made server enter npm run start command and always test back-end endpoints on any available browser. You can make modifications after a complete test. Look into more node js development procedures, instructions, and more to be a better coder.

About the author

Hermit Chawla is a MD at AIS Technolabs which is Web Design and Web Development Company, helping global businesses to grow. He loves to share his thoughts on web development, app development, and game development.

For More:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.