> ## 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.

# Viem - Node RPC Integration

> Connect viem to Tenderly Node RPC to read, send, trace, and simulate transactions across 100+ EVM networks over HTTPS or WebSockets.

[Viem](https://viem.sh/) is a TypeScript Interface for Ethereum that provides low-level stateless primitives for interacting with Ethereum.

You can use Tenderly with Viem to interact with the blockchain using standard RPC methods, as well as [custom methods available on Node RPC](/node-rpc/rpc-reference).

Call Viem's `createPublicClient` and pass the following arguments:

* `chain`: one of the pre-defined chains from `viem/chains`
* `transport`: either a call to `http` or `webSocket`, and initialize with the `https` or `wss` RPC URL.

<Card title="Code samples: Using Viem with Node RPC" href="" />

```js title="node/samples/viem/0-a-call.ts" showLineNumbers {16-18} theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}

const client = createPublicClient({
  chain: mainnet,
  transport: http(
    `https://mainnet.gateway.tenderly.co/${process.env.TENDERLY_NODE_ACCESS_KEY}`
  ),
});


console.log("Block Number", await client.getBlockNumber());
```

## Batching requests

Viem allows you to send [batch requests](https://viem.sh/docs/clients/transports/http#batch-json-rpc). With batching, several JSON-RPC calls are processed in a single HTTPS request. Several Viem actions will be batched and executed at the end of the current JavaScript message queue. By default, there's no time delay, but is possible to configure via an optional `wait` parameter.

This means that several viem actions will be batched and executed at the end of current JavaScript message queue. By default, there's no time-delay, but is possible to configure via an optional `wait` parameter.

```js title="node/samples/viem/0-batch-calls.ts" showLineNumbers {16-21} theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}

const client = createPublicClient({
  chain: mainnet,
  transport: http(
    `https://mainnet.gateway.tenderly.co/${process.env.TENDERLY_NODE_ACCESS_KEY}`,
    {
      batch: true,
    }
  ),
});

const [blockNumber, balance, ensName] = await Promise.all([
  client.getBlockNumber(),
  client.getBalance({ address: "0xd2135CfB216b74109775236E36d4b433F1DF507B" }),
  client.getEnsName({ address: "0xd2135CfB216b74109775236E36d4b433F1DF507B" }),
  client.getChainId(),
]);
console.log("Results are in");
console.log({ blockNumber, balance, ensName });

//  So will this
const bnPromise = client.getBlockNumber();

const balancePromise = client.getBalance({
  address: "0xd2135CfB216b74109775236E36d4b433F1DF507B",
});

console.log(await bnPromise);
console.log(await balancePromise);
```

## Simulate transactions using viem

To use [Simulation RPC methods](/node-rpc/rpc-reference?network=ethereum-mainnet\&method=tenderly_simulateTransaction), simply use the `client.request` method:

```js title="node/samples/viem/1-simulate.ts" showLineNumbers {2-15} theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const simulation = await client.request({
  method: "tenderly_simulateTransaction",
  params: [
    // transaction object
    {
      from: "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
      to: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
      gas: "0x0",
      gasPrice: "0x0",
      value: "0x0",
      data: "0xa9059cbb00000000000000000000000020a5814b73ef3537c6e099a0d45c798f4bd6e1d60000000000000000000000000000000000000000000000000000000000000001",
    },
    // the block
    "latest",
  ],
});

console.log("Trace");
console.log(JSON.stringify(simulation.trace, null, 2));
console.log("=======================================================");
console.log("Logs");
console.log(JSON.stringify(simulation.logs, null, 2));
console.log("=======================================================");
console.log("Asset Changes");
console.log(JSON.stringify(simulation.assetChanges, null, 2));
console.log("=======================================================");
console.log("Balance Changes");
console.log(JSON.stringify(simulation.balanceChanges, null, 2));
```

## Trace transactions using Viem

Use [`tenderly_traceTransaction`](/node-rpc/rpc-reference?network=ethereum-mainnet\&method=tenderly_traceTransaction) to get fully decoded transaction trace.

```ts showLineNumbers title="node/samples/viem/2-transaction-trace.ts" {2-6} theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const txTrace = await client.request({
  method: "tenderly_traceTransaction",
  params: [
    // transaction hash
    "0x6b2264fa8e28a641d834482d250080b39cbbf39251344573c7504d6137c4b793"
  ],
});


console.log("Logs");
console.log(JSON.stringify(txTrace.logs, null, 2));
console.log("=======================================================");
console.log("Asset Changes");
console.log(JSON.stringify(txTrace.assetChanges, null, 2));
console.log("=======================================================");
console.log("Balance Changes");
console.log(JSON.stringify(txTrace.balanceChanges, null, 2));
console.log("=======================================================");
```
