viem

Connect to 256 Blocks using viem

viem is a TypeScript-first interface for Ethereum that provides low-level stateless primitives for interacting with the blockchain. It focuses on developer experience, stability, bundle size, and performance.

Installation

npm install viem

Or with yarn:

yarn add viem

Quick Start

Connect to 256 Blocks using the X-API-Key header with fetchOptions:

import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';
 
// Create the transport with custom headers
const transport = http('https://rpc.256blocks.com/ethereum', {
  fetchOptions: {
    headers: {
      'X-API-Key': 'your-api-key'
    }
  }
});
 
// Create the client
const client = createPublicClient({
  chain: mainnet,
  transport
});
 
// Get the latest block number
const blockNumber = await client.getBlockNumber();
console.log('Latest block:', blockNumber);

Reading Account Balance

import { createPublicClient, http, formatEther } from 'viem';
import { mainnet } from 'viem/chains';
 
const transport = http('https://rpc.256blocks.com/ethereum', {
  fetchOptions: {
    headers: {
      'X-API-Key': 'your-api-key'
    }
  }
});
 
const client = createPublicClient({
  chain: mainnet,
  transport
});
 
// Get account balance
const balance = await client.getBalance({
  address: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb'
});
 
console.log('Balance:', formatEther(balance), 'ETH');

Reading Contract Data

import { createPublicClient, http, formatUnits } from 'viem';
import { mainnet } from 'viem/chains';
 
const transport = http('https://rpc.256blocks.com/ethereum', {
  fetchOptions: {
    headers: {
      'X-API-Key': 'your-api-key'
    }
  }
});
 
const client = createPublicClient({
  chain: mainnet,
  transport
});
 
// ERC-20 ABI (minimal)
const erc20Abi = [
  {
    name: 'balanceOf',
    type: 'function',
    stateMutability: 'view',
    inputs: [{ name: 'account', type: 'address' }],
    outputs: [{ type: 'uint256' }]
  },
  {
    name: 'decimals',
    type: 'function',
    stateMutability: 'view',
    inputs: [],
    outputs: [{ type: 'uint8' }]
  }
] as const;
 
// USDC contract on Ethereum
const usdcAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
 
const balance = await client.readContract({
  address: usdcAddress,
  abi: erc20Abi,
  functionName: 'balanceOf',
  args: ['0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb']
});
 
const decimals = await client.readContract({
  address: usdcAddress,
  abi: erc20Abi,
  functionName: 'decimals'
});
 
console.log('USDC Balance:', formatUnits(balance, decimals));

Multi-Chain Support

Connect to different chains by using the appropriate chain config:

import { createPublicClient, http } from 'viem';
import { mainnet, base, arbitrum, polygon, optimism } from 'viem/chains';
 
function createClient(chain: any, chainPath: string, apiKey: string) {
  const transport = http(`https://rpc.256blocks.com/${chainPath}`, {
    fetchOptions: {
      headers: {
        'X-API-Key': apiKey
      }
    }
  });
 
  return createPublicClient({ chain, transport });
}
 
// Create clients for different chains
const ethClient = createClient(mainnet, 'ethereum', 'your-api-key');
const baseClient = createClient(base, 'base', 'your-api-key');
const arbClient = createClient(arbitrum, 'arbitrum', 'your-api-key');
const polygonClient = createClient(polygon, 'polygon', 'your-api-key');

Resources

viem | 256 Blocks