Skip to content
On this page

Public Client

The createPublicClient function sets up a Public Client with a given Transport configured for a Chain.

The Public Client provides access to Public Actions

Import

ts
import { createPublicClient } from 'viem'

Usage

Initialize a Client with your desired Chain (e.g. mainnet) and Transport (e.g. http).

ts
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = createPublicClient({ 
  chain: mainnet,
  transport: http()
})

Parameters

transport

The Transport of the Public Client.

ts
const client = createPublicClient({
  chain: mainnet,
  transport: http(), 
})

chain (optional)

The Chain of the Public Client.

ts
const client = createPublicClient({
  chain: mainnet, 
  transport: http(),
})

key (optional)

  • Type: string
  • Default: "public"

A key for the Client.

ts
const client = createPublicClient({
  chain: mainnet,
  key: 'public', 
  transport: http(),
})

name (optional)

  • Type: string
  • Default: "Public Client"

A name for the Client.

ts
const client = createPublicClient({
  chain: mainnet,
  name: 'Public Client', 
  transport: http(),
})

pollingInterval (optional)

  • Type: number
  • Default: 4_000

Frequency (in ms) for polling enabled Actions.

ts
const client = createPublicClient({
  chain: mainnet,
  pollingInterval: 10_000, 
  transport: http(),
})

Released under the MIT License.