viem

Connect to NodeFlare using viem — the modern TypeScript-first Ethereum library.

viem is a TypeScript interface for Ethereum with a focus on reliability, efficiency, and excellent developer experience.

Installation

Terminal
npm install viem

Create a Public Client

TypeScript
import { createPublicClient, http } from "viem";
import { mainnet } from "viem/chains";

const client = createPublicClient({
  chain: mainnet,
  transport: http("https://rpc.nodeflare.app/eth/v1/YOUR_KEY"),
});

Switch chains:

TypeScript
import { base, arbitrum, bsc, optimism } from "viem/chains";

const baseClient = createPublicClient({
  chain: base,
  transport: http("https://rpc.nodeflare.app/base/v1/YOUR_KEY"),
});

Read the Latest Block

TypeScript
const block = await client.getBlockNumber();
console.log("Block:", block);

Get an Account Balance

TypeScript
const balance = await client.getBalance({
  address: "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
});
console.log(balance); // BigInt in wei

Read a Contract

TypeScript
import { parseAbi } from "viem";

const balance = await client.readContract({
  address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
  abi: parseAbi(["function balanceOf(address) view returns (uint256)"]),
  functionName: "balanceOf",
  args: ["0xYourAddress"],
});

Send a Transaction

TypeScript
import { createWalletClient } from "viem";
import { privateKeyToAccount } from "viem/accounts";

const account = privateKeyToAccount("0xYOUR_PRIVATE_KEY");

const walletClient = createWalletClient({
  account,
  chain: mainnet,
  transport: http("https://rpc.nodeflare.app/eth/v1/YOUR_KEY"),
});

const hash = await walletClient.sendTransaction({
  to: "0xRecipientAddress",
  value: 10000000000000000n, // 0.01 ETH
});

const receipt = await client.waitForTransactionReceipt({ hash });
console.log("Block:", receipt.blockNumber);

WebSocket Transport

Use the webSocket transport for real-time subscriptions and lower latency.

TypeScript
import { createPublicClient, webSocket } from "viem";
import { mainnet } from "viem/chains";

const client = createPublicClient({
  chain: mainnet,
  transport: webSocket("wss://rpc.nodeflare.app/eth/ws/v1/YOUR_KEY"),
});

For Base:

TypeScript
import { base } from "viem/chains";

const baseClient = createPublicClient({
  chain: base,
  transport: webSocket("wss://rpc.nodeflare.app/base/ws/v1/YOUR_KEY"),
});

Watch for Events

TypeScript
import { parseAbiItem } from "viem";

const unwatch = client.watchEvent({
  address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
  event: parseAbiItem("event Transfer(address indexed from, address indexed to, uint256 value)"),
  onLogs: (logs) => console.log(logs),
});

// Stop watching
unwatch();