Creating Faucet Transactions and Sending Messages on NeroChain Using the API

This end-to-end tutorial clarifies how to use Nero API, especially for first-time viewers.

Pre-requisites

  • Node.js v18+ LTS (which comes with Node)

  • Create a new Node.js project and initialize a package.json file by running the following commands in the terminal:

mkdir nero-api-demo
cd nero-api-demo
npm init -y
  • Run npm install request to install the request module, which is used for making HTTP requests.

Create A New JavaScript File

  • Create a new JavaScript file in the project directory, such as app.js to store the code file below.

  • Please remember to replace 0xRecipientAddress and message content in the code with the actual address and the desired message you want to send.

  • Run node app.js . This will run the code in the app.js file and output the corresponding results.

var request = require('request');
var faucetOptions = {
  'method': 'POST',
  'url': 'https://api.nerochain.io/api/v1/userFaucet',
  'headers': {
    'accept': 'application/json',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    "name": "NERO Faucet",
    "data": {
      "to": "0xRecipientAddress",//Replace 0xRecipientAddress
      "value": 0.1,
      "gas": 21000
    }
  })
};

// Create a faucet transaction
request(faucetOptions, function (error, faucetResponse) {
  if (error) throw new Error(error);
  if (faucetResponse.statusCode === 201) {
    var transactionHash = JSON.parse(faucetResponse.body).transactionHash;
    console.log("Faucet transaction created. Transaction hash:", transactionHash);

    // Send a message
    var messageOptions = {
      'method': 'POST',
      'url': 'https://api.nerochain.io/api/v1/sendMessage',
      'headers': {
        'accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        "name": "send my message",
        "data": {
          "to": "0xRecipientAddress",//Replace 0xRecipientAddress
      "value": 0.1,
          "data": "Your message"//Replace message content
      "value": 0.1,
        }
      })
    };

    request(messageOptions, function (error, messageResponse) {
      if (error) throw new Error(error);
      if (messageResponse.statusCode === 201) {
        var messageTransactionHash = JSON.parse(messageResponse.body).transactionHash;
        console.log("Message sent. Transaction hash:", messageTransactionHash);
      } else {
        console.log("Failed to send the message:", messageResponse.statusCode);
      }
    });
  } else {
    console.log("Failed to create the faucet transaction:", faucetResponse.statusCode);
  }
});

If you see the following screen, it means that your code has successfully stored the message on the blockchain, and you can use these transaction hash values to query and view the details of these transactions.

View the Transaction

To view the details of the created faucet transaction and the sent message transaction using the provided transaction hashes, you can follow three steps below:

  • Step 1: Paste your transaction hash.

  • Step 2: In the raw input field, select UTF-8 encoding.

  • Step 3: The Testnet Explorer response will provide information about the message transaction.

Conclusion

This tutorial provides a foundation for further exploration and application of the Nero API. With this knowledge, you can extend the functionality by implementing additional features and integrating the API into your own projects. Remember to refer to the Nero API documentation for more information on available endpoints, parameters, and authentication methods. Continue exploring and leveraging the power of the Nero API to unlock even more applications and functionalities in the future.

Last updated