• React
  • Providers
  • JSON RPC

JSON RPC

The jsonRpcProvider configures the chains with the RPC URLs that you specify and also provides an ethers.js StaticJsonRpcProvider.

import { jsonRpcProvider } from 'wagmi/providers/jsonRpc'

Usage

import { chain, configureChains } from 'wagmi'
import { jsonRpcProvider } from 'wagmi/providers/jsonRpc'
 
const { chains, provider } = configureChains(
  [chain.mainnet, chain.polygon],
  [
    jsonRpcProvider({
      rpc: (chain) => ({
        http: `https://${chain.id}.example.com`,
      }),
    }),
  ],
)

Return Value

{
  chains: Chain[],
  provider: JsonRpcProvider,
  webSocketProvider: WebSocketProvider
}

Configuration

rpc

Accepts a function which provides the chain and expects to receive a http URL and optionally a webSocket URL.

import { chain, configureChains } from 'wagmi'
import { jsonRpcProvider } from 'wagmi/providers/jsonRpc'
 
const { chains, provider } = configureChains(
  [chain.mainnet, chain.polygon],
  [
    jsonRpcProvider({
      rpc: (chain) => ({
        http: `https://${chain.id}.example.com`,
        webSocket: `wss://${chain.id}.example.com`,
      }),
    }),
  ],
)

priority (optional)

The priority used for the provider. Lower-value priorities are favoured over higher-value priorities. If multiple providers share the same priority, they are chosen at random.

import { chain, configureChains } from 'wagmi'
import { jsonRpcProvider } from 'wagmi/providers/jsonRpc'
 
const { chains, provider } = configureChains(
  [chain.mainnet, chain.polygon],
  [
    jsonRpcProvider({
      priority: 0,
      rpc: (chain) => ({
        http: `https://${chain.id}.example.com`,
        webSocket: `wss://${chain.id}.example.com`,
      }),
    }),
    alchemyProvider({ priority: 1 }),
  ],
)

stallTimeout (optional)

The timeout in milliseconds after which another provider will be attempted.

import { chain, configureChains } from 'wagmi'
import { jsonRpcProvider } from 'wagmi/providers/jsonRpc'
 
const { chains, provider } = configureChains(
  [chain.mainnet, chain.polygon],
  [
    jsonRpcProvider({
      rpc: (chain) => ({
        http: `https://${chain.id}.example.com`,
        webSocket: `wss://${chain.id}.example.com`,
      }),
      stallTimeout: 1000,
    }),
    alchemyProvider({ priority: 1, stallTimeout: 1000 }),
  ],
)

static (optional)

Flag to indicate if the provider should return a StaticJsonRpcProvider or JsonRpcProvider. Defaults to true.

import { chain, configureChains } from 'wagmi'
import { jsonRpcProvider } from 'wagmi/providers/jsonRpc'
 
const { chains, provider } = configureChains(
  [chain.mainnet, chain.polygon],
  [
    jsonRpcProvider({
      rpc: (chain) => ({
        http: `https://${chain.id}.example.com`,
        webSocket: `wss://${chain.id}.example.com`,
      }),
      static: false,
    }),
  ],
)

weight (optional)

The weight a response from this provider provides. This can be used if a given provider is more trusted.

import { chain, configureChains } from 'wagmi'
import { jsonRpcProvider } from 'wagmi/providers/jsonRpc'
 
const { chains, provider } = configureChains(
  [chain.mainnet, chain.polygon],
  [
    jsonRpcProvider({
      rpc: (chain) => ({
        http: `https://${chain.id}.example.com`,
        webSocket: `wss://${chain.id}.example.com`,
      }),
      weight: 1,
    }),
    alchemyProvider({ priority: 1, weight: 2 }),
  ],
)