> For the complete documentation index, see [llms.txt](https://nazeehs-org.gitbook.io/comp-kit/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nazeehs-org.gitbook.io/comp-kit/clients/public-client.md).

# Public Client

A Public Client is an interface provided by [viem](https://viem.sh/) to "public" [JSON-RPC API](https://ethereum.org/en/developers/docs/apis/json-rpc/) methods such as retrieving block numbers, transactions, reading from smart contracts, etc through [Public Actions](https://viem.sh/docs/actions/public/introduction.html).

You can read more about Public Client [here](https://viem.sh/docs/clients/public.html).

In `comp-kit`, you are provided with `publicClient` for each *chain* you pass into `supportedChains` props to `KitProvider`. The reason why `publicClient` is different for each chain unlike `walletClient` is mentioned [here](https://twitter.com/jakemoxey/status/1669434938771529728?s=20).

## Usage

In order to use `publicClient`, import `usePublicClient` from `"@nazeeh2000/comp-kit"`. The return value of this hook will be a `Record` containing the `publicClient` corresponding to the network name.

You can use `publicClient` in your component as per the below example.

```tsx
'use client'
import { usePublicClient } from "@nazeeh2000/comp-kit";
import { mainnet } from "viem/chains";

export default function Home() {
    const publicClient = usePublicClient();
    // publicClient for mainnet
    const mainnetPublicClient = publicClient[mainnet.name]

    return (
    // Your component
    )
}
```

You can apply all the methods to the `mainnetPublicClient` that are applicable to the `publicClient` of `viem` mentioned [here](https://viem.sh/docs/clients/public.html).

> Note: You need to add `use client` at the top of the component accessing these hooks as these hooks cant be run on the server side because they've *React Context* in the background.
