Getting Started ​
Overview ​
Wagmi is a collection of Vue composition utilties for Ethereum. You can learn more about the rationale behind the project in the Why Wagmi section.
Automatic Installation ​
For new projects, it is recommended to set up your Wagmi app using the create-wagmi
command line interface (CLI). This will create a new Wagmi project using TypeScript and install the required dependencies.
pnpm create wagmi
npm create wagmi@latest
yarn create wagmi
bun create wagmi
Once the command runs, you'll see some prompts to complete.
Project name: wagmi-project
Select a framework: Vue / Vanilla
...
After the prompts, create-wagmi
will create a directory with your project name and install the required dependencies. Check out the README.md
for further instructions (if required).
Manual Installation ​
To manually add Wagmi to your project, install the required packages.
pnpm add @wagmi/vue viem@2.x @tanstack/vue-query
npm install @wagmi/vue viem@2.x @tanstack/vue-query
yarn add @wagmi/vue viem@2.x @tanstack/vue-query
bun add @wagmi/vue viem@2.x @tanstack/vue-query
- Viem is a TypeScript interface for Ethereum that performs blockchain operations.
- TanStack Query is an async state manager that handles requests, caching, and more.
- TypeScript is optional, but highly recommended. Learn more about TypeScript support.
Create Config ​
Create and export a new Wagmi config using createConfig
.
import { http, createConfig } from '@wagmi/vue'
import { mainnet, sepolia } from '@wagmi/vue/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
In this example, Wagmi is configured to use the Mainnet and Sepolia chains, and injected
connector. Check out the createConfig
docs for more configuration options.
TypeScript Tip
If you are using TypeScript, you can "register" the Wagmi config or use the hook config
property to get strong type-safety in places that wouldn't normally have type info.
import { useBlockNumber } from '@wagmi/vue'
useBlockNumber({ chainId: 123 })
declare module '@wagmi/vue' {
interface Register {
config: typeof config
}
}
import { useBlockNumber } from '@wagmi/vue'
useBlockNumber({ chainId: 123, config })
By registering or using the hook config
property, useBlockNumber
's chainId
is strongly typed to only allow Mainnet and Sepolia IDs. Learn more by reading the TypeScript docs.
Add Plugin to App ​
App the WagmiPlugin
to your App
instance and pass the config
you created earlier to the plugin options.
import { WagmiPlugin } from '@wagmi/vue'
import { config } from './config'
import { http, createConfig } from '@wagmi/vue'
import { mainnet, sepolia } from '@wagmi/vue/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
Check out the WagmiPlugin
docs to learn more about the plugin API.
Setup TanStack Query ​
After the WagmiPlugin
, attach the VueQueryPlugin
to your app, and pass a new QueryClient
instance to the client
property.
import { QueryClient, VueQueryPlugin } from '@tanstack/vue-query'
import { WagmiPlugin } from '@wagmi/vue'
import { config } from './config'
const queryClient = new QueryClient()
import { http, createConfig } from '@wagmi/vue'
import { mainnet, sepolia } from '@wagmi/vue/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
Check out the TanStack Query docs to learn about the library, APIs, and more.
Use Wagmi ​
Now that everything is set up, every component inside your app can use Wagmi Vue Composables.
<script setup lang="ts">
import { useAccount } from '@wagmi/vue'
const { address } = useAccount()
</script>
<template>
</template>
import { QueryClient, VueQueryPlugin } from '@tanstack/vue-query'
import { WagmiPlugin } from '@wagmi/vue'
import { config } from './config'
const queryClient = new QueryClient()
import { http, createConfig } from '@wagmi/vue'
import { mainnet, sepolia } from '@wagmi/vue/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
Next Steps ​
For more information on what to do next, check out the following topics.
- TypeScript Learn how to get the most out of Wagmi's type-safety and inference for an enlightened developer experience.
- Connect Wallet Learn how to enable wallets to connect to and disconnect from your apps and display information about connected accounts.
- Vue Composables Browse the collection of Vue Composables and learn how to use them.
- Viem Learn about Viem and how it works with Wagmi.