> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chainstack.com/llms.txt
> Use this file to discover all available pages before exploring further.

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://docs.chainstack.com/feedback

```json
{
  "path": "/recipes/extract-randao-value-from-the-ethereum-beacon-chain-using-the-block-details-method-1",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

# Extract RANDAO from Ethereum Beacon Chain blocks

> Extract the RANDAO random value from Ethereum Beacon Chain block details using JavaScript and Axios for on-chain randomness verification and analysis.

<AccordionGroup>
  <Accordion title="Prerequisites">
    You will need a Chainstack account and an Ethereum node to follow this recipe.

    1. [Sign up with Chainstack](https://console.chainstack.com/user/account/create).
    2. [Deploy a node](https://docs.chainstack.com/docs/manage-your-networks).
    3. [View node access and credentials](https://docs.chainstack.com/docs/manage-your-node#view-node-access-and-credentials).
  </Accordion>

  <Accordion title="Environment setup">
    <Steps>
      <Step>
        Install [node.js](https://nodejs.org/en/) in case it is not installed yet.
      </Step>

      <Step>
        Create a new directory for your project.
      </Step>

      <Step>
        Initialize an npm project:

        ```bash theme={"system"}
        npm init
        ```
      </Step>

      <Step>
        Install the axios package:

        ```bash theme={"system"}
        npm install axios
        ```
      </Step>
    </Steps>
  </Accordion>

  <Accordion title="Import the package and set up the request details">
    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.

    ```js index.js theme={"system"}
    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";

    ```
  </Accordion>

  <Accordion title="Send the request">
    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.

    ```js index.js theme={"system"}
    // 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}`);
      });
    ```
  </Accordion>

  <Accordion title="Slot and randao">
    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.

    ```js index.js theme={"system"}
    const slot = parsedJson.data.message.slot;
    const randao = parsedJson.data.message.body.randao_reveal;
    ```
  </Accordion>

  <Accordion title="Run the script">
    Now you only need to save the file and run it with:

    `node index`

    This command will run the program and display the response.
  </Accordion>
</AccordionGroup>

<RequestExample>
  ```js index.js theme={"system"}
  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}`);
    });
  ```
</RequestExample>

<ResponseExample>
  ```bash theme={"system"}
  $ node index
  randao value for slot 5717752 is:
  0x91bed944631c3178c3c3d2ba2b981b12fc68bfc22e66c7f77477416f9f46837b15ae6cd6feb4347484c83e83751c7cab0bf93e39698eb61ab38fff51727a03a04ee1fa427162fc9539e647619be88b8bb0db37f7d241883371dca99fa049a2be
  ```
</ResponseExample>
