HTTP Transport
Connect to Ethereum nodes via standard JSON-RPC over HTTP/HTTPS
Connect to Ethereum nodes via standard JSON-RPC over HTTP/HTTPS
The HTTP transport connects to Ethereum nodes via standard JSON-RPC over HTTP/HTTPS. It's the most common transport type.
The transport.HTTP(...) function returns a transport.TransportFactory that you pass into a client config (e.g. client.PublicClientConfig or client.WalletClientConfig).
import ( "github.com/ChefBingbong/viem-go/client" "github.com/ChefBingbong/viem-go/client/transport")These are the most common imports for using the HTTP transport:
transport.HTTP, transport.HTTPTransportConfig).client.CreatePublicClient, client.PublicClientConfig).transport.HTTP(...) creates a factory, not an immediate connection. The client constructs the transport internally when you create the client.
Close the client when you're done: defer publicClient.Close(). This closes the underlying transport (and any batch scheduler if enabled).
publicClient, err := client.CreatePublicClient(client.PublicClientConfig{ Transport: transport.HTTP("https://eth.llamarpc.com"),})if err != nil { log.Fatal(err)}defer publicClient.Close()stringThe RPC endpoint URL.
URL resolution order is:
transport.HTTP(\"https://...\")transport.HTTP(\"\", transport.HTTPTransportConfig{ URL: \"https://...\" })params.Chain.RpcUrls[\"default\"].HTTP[0] (when a client provides a chain)If no URL can be resolved, client creation fails with transport.ErrURLRequired.
tr := transport.HTTP("https://eth.llamarpc.com")
publicClient, err := client.CreatePublicClient(client.PublicClientConfig{ Transport: tr,})time.Duration10sRequest timeout for each JSON-RPC call.
tr := transport.HTTP("https://eth.llamarpc.com", transport.HTTPTransportConfig{ Timeout: 5 * time.Second,})
publicClient, err := client.CreatePublicClient(client.PublicClientConfig{ Transport: tr,})int3Maximum number of retry attempts for retryable errors.
tr := transport.HTTP("https://eth.llamarpc.com", transport.HTTPTransportConfig{ RetryCount: 5,})
publicClient, err := client.CreatePublicClient(client.PublicClientConfig{ Transport: tr,})time.Duration150msBase delay between retries. Retries use exponential backoff (delay * 2^attempt).
If a Retry-After header is present on an HTTP error, it will be used when possible.
tr := transport.HTTP("https://eth.llamarpc.com", transport.HTTPTransportConfig{ RetryDelay: 250 * time.Millisecond,})
publicClient, err := client.CreatePublicClient(client.PublicClientConfig{ Transport: tr,})map[string]stringAdditional HTTP headers to include in requests (e.g. for authenticated endpoints).
tr := transport.HTTP("https://eth.llamarpc.com", transport.HTTPTransportConfig{ Headers: map[string]string{ "Authorization": "Bearer YOUR_API_KEY", },})
publicClient, err := client.CreatePublicClient(client.PublicClientConfig{ Transport: tr,})*transport.BatchConfigEnables JSON-RPC request batching at the transport layer.
When enabled, concurrent requests may be grouped into a single HTTP request.
tr := transport.HTTP("https://eth.llamarpc.com", transport.HTTPTransportConfig{ Batch: &transport.BatchConfig{ Enabled: true, BatchSize: 1000, Wait: 10 * time.Millisecond, },})
publicClient, err := client.CreatePublicClient(client.PublicClientConfig{ Transport: tr,})boolfalseWhether batching is enabled.
tr := transport.HTTP("https://eth.llamarpc.com", transport.HTTPTransportConfig{ Batch: &transport.BatchConfig{ Enabled: true, },})int1000Maximum number of requests per batch. If 0, viem-go defaults it to 1000.
tr := transport.HTTP("https://eth.llamarpc.com", transport.HTTPTransportConfig{ Batch: &transport.BatchConfig{ Enabled: true, BatchSize: 500, },})time.DurationMaximum time to wait before sending a batch (gives more time for concurrent requests to coalesce).
tr := transport.HTTP("https://eth.llamarpc.com", transport.HTTPTransportConfig{ Batch: &transport.BatchConfig{ Enabled: true, Wait: 10 * time.Millisecond, },})*transport.MethodFilterRestrict which JSON-RPC methods can be called by this transport.
tr := transport.HTTP("https://eth.llamarpc.com", transport.HTTPTransportConfig{ Methods: &transport.MethodFilter{ Include: []string{"eth_blockNumber", "eth_getBalance"}, Exclude: []string{"eth_sendTransaction"}, },})[]stringIf set, only methods in Include are allowed (except those blocked by Exclude).
tr := transport.HTTP("https://eth.llamarpc.com", transport.HTTPTransportConfig{ Methods: &transport.MethodFilter{ Include: []string{"eth_blockNumber", "eth_getBalance"}, },})[]stringMethods to block (checked before Include).
tr := transport.HTTP("https://eth.llamarpc.com", transport.HTTPTransportConfig{ Methods: &transport.MethodFilter{ Exclude: []string{"eth_sendTransaction", "eth_sendRawTransaction"}, },})boolfalseWhen enabled, RPC error responses are returned as (*transport.RPCResponse, nil) instead of being wrapped into an error.
This can be useful if you want to inspect resp.Error yourself.
tr := transport.HTTP("https://eth.llamarpc.com", transport.HTTPTransportConfig{ Raw: true,})func(req any) errorHook invoked before each request.
HTTPTransportConfig, but it is not currently invoked by the transport implementation.func(resp any) errorHook invoked after each response.
HTTPTransportConfig, but it is not currently invoked by the transport implementation.string"http"Transport identifier key.
tr := transport.HTTP("https://eth.llamarpc.com", transport.HTTPTransportConfig{ Key: "foo",})string"HTTP JSON-RPC"Human-readable transport name.
tr := transport.HTTP("https://eth.llamarpc.com", transport.HTTPTransportConfig{ Name: "Foo HTTP Transport",})transport.HTTP(...) returns a transport.TransportFactory.