Skip to main content

JSON-RPC Transport

Ethereum nodes communicate with applications through a JSON-RPC API — a stateless, transport-agnostic protocol. Nethereum wraps every standard JSON-RPC method into typed C# methods so you rarely need to think about the raw protocol.

Connecting to Ethereum

HTTP (Most Common)

using Nethereum.Web3;
using Nethereum.Web3.Accounts;

// Read-only (no signing)
var web3 = new Web3("https://mainnet.infura.io/v3/YOUR_API_KEY");

// With signing account
var account = new Account("YOUR_PRIVATE_KEY");
var web3 = new Web3(account, "https://mainnet.infura.io/v3/YOUR_API_KEY");

In-Process DevChain (No External Node)

using Nethereum.DevChain;
using Nethereum.Web3.Accounts;

var account = new Account("0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7");
var devChain = new DevChainNode();
await devChain.StartAsync(account);
var web3 = devChain.CreateWeb3(account);

WebSocket (Real-Time Subscriptions)

using Nethereum.JsonRpc.WebSocketStreamingClient;
using Nethereum.RPC.Reactive.Eth.Subscriptions;

var client = new StreamingWebSocketClient("wss://mainnet.infura.io/ws/v3/YOUR_API_KEY");
await client.StartAsync();

var subscription = new EthNewBlockHeadersObservableSubscription(client);
subscription.GetSubscriptionDataResponsesAsObservable().Subscribe(block =>
{
Console.WriteLine($"New block: {block.BlockHash}");
});
await subscription.SubscribeAsync();

IPC (Local Node)

using Nethereum.JsonRpc.IpcClient;

var ipcClient = new IpcClient("./geth.ipc");
var web3 = new Web3(ipcClient);

Choosing a Provider

ProviderUse CasePackage
HTTPMost applications, hosted nodesNethereum.JsonRpc.RpcClient (included in Web3)
DevChainLocal development and testingNethereum.DevChain
WebSocketReal-time subscriptionsNethereum.JsonRpc.WebSocketStreamingClient
IPCLocal node, lowest latencyNethereum.JsonRpc.IpcClient
System.Text.Json HTTPAOT-friendly / trimmed appsNethereum.JsonRpc.SystemTextJsonRpcClient

Nethereum's RPC Abstraction

JSON-RPC MethodNethereum API
eth_getBalanceweb3.Eth.GetBalance.SendRequestAsync(address)
eth_blockNumberweb3.Eth.Blocks.GetBlockNumber.SendRequestAsync()
eth_callweb3.Eth.Transactions.Call.SendRequestAsync(callInput)
eth_sendRawTransactionweb3.Eth.Transactions.SendRawTransaction.SendRequestAsync(signedTx)
eth_getTransactionReceiptweb3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txHash)
eth_getLogsweb3.Eth.Filters.GetLogs.SendRequestAsync(filterInput)

Finding RPC Endpoints

Use Chainlist to find RPC endpoints for any EVM-compatible network. For production use:

  • Infura — Ethereum, L2s, and testnets
  • Alchemy — Ethereum, L2s, and enhanced APIs
  • QuickNode — Multi-chain with global edge network
  • Ankr — Decentralised RPC with public and premium tiers

Error Handling

try
{
var receipt = await web3.Eth.Transactions.GetTransactionReceipt
.SendRequestAsync("0xInvalidHash");
}
catch (RpcResponseException ex)
{
Console.WriteLine($"RPC Error {ex.RpcError.Code}: {ex.RpcError.Message}");
}