clawchain/viem/site/pages/docs/getting-started.mdx

71 lines
2.2 KiB
Plaintext

# Getting Started [Get started with viem in just a few lines of code.]
## Overview
viem is a TypeScript interface for Ethereum that provides low-level stateless primitives for interacting with Ethereum. viem is focused on developer experience, stability, bundle size, and performance:
- **Developer experience** Automatic [type safety and inference](/docs/typescript), comprehensive documentation, composable APIs.
- **Stability** Test suite runs against forked Ethereum networks, complete [test coverage](https://app.codecov.io/gh/wevm/viem).
- **Bundle size** Tree-shakable lightweight modules.
- **Performance** Optimized encoding/parsing, async tasks only when necessary.
You can learn more about the rationale behind the project in the [Why viem](/docs/introduction) section.
## Installation
:::code-group
```bash [npm]
npm i viem
```
```bash [pnpm]
pnpm i viem
```
```bash [bun]
bun i viem
```
:::
## Quick Start
### 1. Set up your Client & Transport
Firstly, set up your [Client](/docs/clients/intro) with a desired [Transport](/docs/clients/intro) & [Chain](/docs/chains/introduction).
```ts twoslash
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
const client = createPublicClient({ // [!code focus]
chain: mainnet, // [!code focus]
transport: http(), // [!code focus]
}) // [!code focus]
```
:::info
In a production app, it is highly recommended to pass through your authenticated RPC provider URL (Infura, thirdweb, etc). If no URL is provided, viem will default to a public RPC provider. [Read more](/docs/clients/transports/http#usage).
:::
### 2. Consume Actions
Now that you have a Client set up, you can now interact with Ethereum and consume [Actions](/docs/actions/public/introduction)!
```ts twoslash
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
const client = createPublicClient({
chain: mainnet,
transport: http(),
})
const blockNumber = await client.getBlockNumber() // [!code focus]
```
## Live example
<iframe className="mt-6" width="100%" height="600px" frameBorder="0" src="https://stackblitz.com/edit/viem-getting-started?embed=1&file=index.ts&hideNavigation=1&hideDevTools=true&terminalHeight=1&hideExplorer=1&devtoolsheight=1&ctl=1"></iframe>