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

# Node RPC Access

> Configure a Web3 Action to read on-chain data and send transactions on Mainnet or testnets through Tenderly Node RPC using the context.gateways API.

You can access [Node RPC](/node-rpc/overview) directly from your Web3 Action functions. This allows you to send transactions on the Mainnet or testnets and read on-chain data automatically through Web3 Actions.

Setting up Web3 Actions to access the blockchain requires minimal setup, a single Javascript variable. No need to worry about copying/pasting RPC URLs, managing secret keys, etc. Tenderly takes care of all this for you in the background.

In this guide, you’ll learn how to configure your Web3 Action function code to use Tenderly’s production node to access the Mainnet.

## Node RPC in Context

In Web3 Actions, access to Tenderly’s node is enabled through the `context` object. Learn more about [Web3 Actions Context](/monitoring/web3-actions/references/context) here.

The `context` object contains a property called `gateways` which gives you access to the `getGateway()` method. This method requires one argument `Network`, which is used to specify the network you want to access.

The `Network` enum has a member for every network supported in Node RPC, for example:

* `Network.MAINNET`
* `Network.SEPOLIA`
* `Network.POLYGON`
* `Network.OPTIMISTIC`
* `Network.BASE`
* `Network.ARBITRUM`

For the full list, see [`networks.ts` in the `@tenderly/actions` npm package](https://github.com/Tenderly/tenderly-actions/blob/main/packages/actions/src/networks.ts).

To demonstrate how all of this comes together, here’s an example Javascript variable that is configured to access the Mainnet:

```jsx title="example.jsx" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const defaultGatewayURL = context.gateways.getGateway(Network.MAINNET);
```

<Tip>
  RPC calls made from action code work on any contract, verified or not. To read state from a
  contract without an ABI in Tenderly, supply the ABI in your action code (for example, to an
  ethers.js `Contract` instance) and call it through the gateway.
</Tip>

## Example Web3 Action function to access the Mainnet

Let’s create a simple Web3 Action that will get triggered every time a new block is mined on the Mainnet. Each time a block gets mined, the Web3 Action will execute the custom code, also referred to as “function”. Read more about [Web3 Action functions](/monitoring/web3-actions/references/functions-events-triggers#action-functions) here.

For the sake of simplicity, the example function below will get the block number of the latest block mined and log it to the console.

You can read the comments to understand what each line of code does.

```jsx title="example.jsx" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
// Required libraries that must be included for the code to work
const ethers = require('ethers');
const { Network } = require('@tenderly/actions');

// Do not change the default 'actionFn' name.
const actionFn = async (context, blockEvent) => {
  // Setting a variable that will store the Node RPC RPC URL and secret key
  const defaultGatewayURL = context.gateways.getGateway(Network.MAINNET);

  // Using the Ethere.js provider class to call the RPC URL
  const provider = new ethers.providers.JsonRpcProvider(defaultGatewayURL);

  // Logging the block number of the latest mined block
  console.log(await provider.getBlockNumber());
};

// Exporting the default module. Do not change this.
module.exports = { actionFn };
```

Next, go to the **Dashboard** → **Web3 Actions** → **Add Action**.

<Frame caption="Web3 Action set up wizard">
  <img src="https://mintcdn.com/tenderly/FHtt5ZqGFmzL5kkh/images/web3-actions/web3-gateway-access-1.webp?fit=max&auto=format&n=FHtt5ZqGFmzL5kkh&q=85&s=36aac5c00b4596e31b875b1f3093d6e7" alt="Web3 Action set up wizard" width="1600" height="1000" data-path="images/web3-actions/web3-gateway-access-1.webp" />
</Frame>

* For the Trigger Type, select **Block.**
* In the **Function** textbox, paste the code from the example above and click **Next**.

<Frame caption="Custom code that is executed when the Web3 Action is triggered">
  <img src="https://mintcdn.com/tenderly/FHtt5ZqGFmzL5kkh/images/web3-actions/web3-gateway-access-2.webp?fit=max&auto=format&n=FHtt5ZqGFmzL5kkh&q=85&s=cd29e4b2ee0352dcde30e5a54d26e498" alt="Custom code that is executed when the Web3 Action is triggered" width="1600" height="1000" data-path="images/web3-actions/web3-gateway-access-2.webp" />
</Frame>

* Set the **Trigger** by selecting the network. In our case, that is **Mainnet**.
* Also, set the **Block Period** to tell Tenderly how often to execute the Web3 Action function. For example, if you put 1, the Web3 Action will be executed once every block is mined.
* In the **Info** fields, **add a name** for the Web3 Action and an optional description.
* You can also set a **Destination** where you’ll [receive notifications](/monitoring/web3-actions/references/notifications) about failed executions of the Web3 Action.
* Lastly, **click Create** to deploy the Web3 Action.

From the **Execution History** page, we can see that the Web3 Action is being executed successfully.

<Frame caption="Web3 Action execution history">
  <img src="https://mintcdn.com/tenderly/FHtt5ZqGFmzL5kkh/images/web3-actions/web3-gateway-access-3.webp?fit=max&auto=format&n=FHtt5ZqGFmzL5kkh&q=85&s=ced4c533336be88f351e325b35ec430f" alt="Web3 Action execution history" width="1600" height="1000" data-path="images/web3-actions/web3-gateway-access-3.webp" />
</Frame>

You can click on the **Execution ID** to see details about the executed Web3 Action and the payload in JSON.

### Helpful resources

For more information on how to manage your Web3 Actions and monitor execution errors, read the following guides:

* [Managing Web3 Actions](/monitoring/web3-actions/references/lifecycle)
* [Error Reporting for Web3 Actions](/monitoring/web3-actions/references/error-reporting)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.