Skip to main content
const axios = require('axios');

// Declare the beacon provider URL and request parameters.
const BEACON_PROVIDER_URL = "CHAINSTACK_BEACON_URL";
const HEADER = { 'Content-Type': 'application/json' };
const BLOCK_ID = "head";

// Send the request
axios.get(`${BEACON_PROVIDER_URL}/eth/v2/beacon/blocks/${BLOCK_ID}`, { headers: HEADER })
  .then(response => {

    // Parse the response and log the result.
    try {
      const parsedJson = response.data;
      const slot = parsedJson.data.message.slot;
      const randao = parsedJson.data.message.body.randao_reveal;

      console.log(`randao value for slot ${slot} is: ${randao}`);
    } catch (error) {
      console.error(`An error occurred while parsing the response: ${error}`);
    }
  })
  .catch(error => {
    console.error(`An error occurred while fetching the data: ${error}`);
  });
$ node index
randao value for slot 5717752 is:
0x91bed944631c3178c3c3d2ba2b981b12fc68bfc22e66c7f77477416f9f46837b15ae6cd6feb4347484c83e83751c7cab0bf93e39698eb61ab38fff51727a03a04ee1fa427162fc9539e647619be88b8bb0db37f7d241883371dca99fa049a2be
You will need a Chainstack account and an Ethereum node to follow this recipe.
  1. Sign up with Chainstack.
  2. Deploy a node.
  3. View node access and credentials.
1

Install node.js in case it is not installed yet.
2

Create a new directory for your project.
3

Initialize an npm project:
npm init
4

Install the axios package:
npm install axios
Create a new file index.js, import the axios library, and initialize the parameters required for this request.This script will send a GET request to the Ethereum Beacon chain.Paste your Beacon Chainstack node URL in the BEACON_PROVIDER_URL const.Also, set up the request parameters. The BLOCK_ID constant identifies which block to query.
index.js
const axios = require('axios');

// Declare the beacon provider URL and request parameters.
const BEACON_PROVIDER_URL = "CHAINSTACK_BEACON_URL";
const HEADER = { 'Content-Type': 'application/json' };
const BLOCK_ID = "head";

The body of the script is the code to send the request.This part sends a request to the Beacon provider, parses the response, and prints a statement in the console.
index.js
// Send the request
axios.get(`${BEACON_PROVIDER_URL}/eth/v2/beacon/blocks/${BLOCK_ID}`, { headers: HEADER })
  .then(response => {

    // Parse the response and log the result.
    try {
      const parsedJson = response.data;
      const slot = parsedJson.data.message.slot;
      const randao = parsedJson.data.message.body.randao_reveal;

      console.log(`randao value for slot ${slot} is: ${randao}`);
    } catch (error) {
      console.error(`An error occurred while parsing the response: ${error}`);
    }
  })
  .catch(error => {
    console.error(`An error occurred while fetching the data: ${error}`);
  });
This script isolates the slot, and randao fields of the object returned.They are held in the respective constants and you can wrap this code in a function to return those values and do more processing.
index.js
const slot = parsedJson.data.message.slot;
const randao = parsedJson.data.message.body.randao_reveal;
Now you only need to save the file and run it with:node indexThis command will run the program and display the response.
const axios = require('axios');

// Declare the beacon provider URL and request parameters.
const BEACON_PROVIDER_URL = "CHAINSTACK_BEACON_URL";
const HEADER = { 'Content-Type': 'application/json' };
const BLOCK_ID = "head";

// Send the request
axios.get(`${BEACON_PROVIDER_URL}/eth/v2/beacon/blocks/${BLOCK_ID}`, { headers: HEADER })
  .then(response => {

    // Parse the response and log the result.
    try {
      const parsedJson = response.data;
      const slot = parsedJson.data.message.slot;
      const randao = parsedJson.data.message.body.randao_reveal;

      console.log(`randao value for slot ${slot} is: ${randao}`);
    } catch (error) {
      console.error(`An error occurred while parsing the response: ${error}`);
    }
  })
  .catch(error => {
    console.error(`An error occurred while fetching the data: ${error}`);
  });
$ node index
randao value for slot 5717752 is:
0x91bed944631c3178c3c3d2ba2b981b12fc68bfc22e66c7f77477416f9f46837b15ae6cd6feb4347484c83e83751c7cab0bf93e39698eb61ab38fff51727a03a04ee1fa427162fc9539e647619be88b8bb0db37f7d241883371dca99fa049a2be
I