Skip to content

getPublicClient

Action for getting Viem PublicClient instance.

Import

ts
import { getPublicClient } from '@wagmi/core'

Usage

ts
import { getPublicClient } from '@wagmi/core'
import { config } from './config'

const client = getPublicClient(config)
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

WARNING

If you want to optimize bundle size, you should use getClient along with Viem's tree-shakable actions instead. Since Public Client has all public actions attached directly to it.

Parameters

ts
import { type GetClientParameters } from '@wagmi/core'

chainId

config['chains'][number]['id'] | undefined

ID of chain to use when getting Viem Public Client.

ts
import { getPublicClient } from '@wagmi/core'
import { mainnet } from '@wagmi/core/chains'
import { config } from './config'

const client = getPublicClient(config, {
  chainId: mainnet.id, 
})
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

Return Type

ts
import { type GetPublicClientReturnType } from '@wagmi/core'

PublicClient | undefined

Viem PublicClient instance.

Released under the MIT License.