> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dynamic.xyz/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Getting a Viem PublicClient

[Viem](https://viem.sh/) is one of the most popular libraries for interacting with EVM blockchains.

A [PublicClient](https://viem.sh/docs/clients/public) is an interface to "public" JSON-RPC API methods
such as retrieving block numbers, transactions, reading from smart contracts, etc.

We provide a helper method to allow you to create a Viem PublicClient given some network data.

## Usage

> [tab] JavaScript
>
> ```javascript theme={"system"}
> import { getActiveNetworkData } from '@dynamic-labs-sdk/client';
> import { createPublicClientFromNetworkData } from '@dynamic-labs-sdk/evm/viem';
>
> const someAction = async (walletAccount) => {
>   // you can use your custom NetworkData object or get the active network data from a wallet account
>   // with the getActiveNetworkData function
>
>   const { networkData } = await getActiveNetworkData({ walletAccount });
>
>   const publicClient = createPublicClientFromNetworkData({ networkData });
>
>    // PublicClient is now ready to use
> };
> ```

> [tab] React
>
> Derive the public client from `useGetActiveNetworkData`, which automatically re-fetches when the wallet account changes or the user switches networks:
>
> ```tsx theme={"system"}
> import { createPublicClientFromNetworkData } from '@dynamic-labs-sdk/evm/viem';
> import { useGetActiveNetworkData } from '@dynamic-labs-sdk/react-hooks';
> import { useMemo } from 'react';
>
> function useEvmPublicClient(walletAccount) {
>   const { data: activeNetwork } = useGetActiveNetworkData({ walletAccount });
>
>   return useMemo(
>     () =>
>       activeNetwork?.networkData
>         ? createPublicClientFromNetworkData({ networkData: activeNetwork.networkData })
>         : null,
>     [activeNetwork],
>   );
> }
> ```

## Related functions

* [Get active network](https://docs.dynamic.xyz/docs/javascript/reference/wallets/get-active-network.md)
* [Adding EVM extensions](https://docs.dynamic.xyz/docs/javascript/reference/evm/adding-evm-extensions.md)
* [Getting Viem Wallet Client](https://docs.dynamic.xyz/docs/javascript/reference/evm/getting-viem-wallet-client.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.