Skip to content

getChainId

Action for getting current chain ID.

Import

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

Usage

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

const chainId = getChainId(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(),
  },
})

Return Type

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

number

Current chain ID from config.state.chainId.

INFO

Only returns chain IDs for chains configured via createConfig's chains parameter.

If the active connection chainId is not from a chain included in your Wagmi Config, getChainId will return the last configured chain ID.

Released under the MIT License.