Initialize the Nexus SDK with a connected wallet and fetch your first unified balance.
In this guide you will create a Nexus SDK instance, connect it to a wallet provider, and fetch unified balances across all supported chains. By the end you will have a minimal Next.js app with four buttons that demonstrate the full SDK lifecycle.
Prerequisites
Node.js version 18 or higher, with npm 9 or higher
A package manager — this guide uses pnpm, but npm and yarn work too
An EIP-1193 compatible wallet (e.g. MetaMask) installed in your browser
@avail-project/nexus-core installed in your project (see Installation)
What you will build
A minimal Next.js page with four buttons:
Connect Wallet — connects an EIP-1193 wallet
Initialize Nexus — passes the wallet provider to the SDK
Fetch Balances — retrieves token balances across all supported chains
De-initialize — tears down the SDK instance
Balance methods
The SDK provides two balance methods: getBalancesForBridge() returns tokens available for bridging, while getBalancesForSwap() returns tokens available for cross-chain swaps. Learn more in the Bridge vs Swap concept page.
Step 1 — Create a Next.js project
If you don't have an existing project, scaffold one:
Create a single shared SDK instance that the rest of the app will import. This file lives outside of React so it has no component lifecycle concerns.src/lib/nexus.ts
import { createNexusClient, type NexusClient } from "@avail-project/nexus-core";// Create a single client instance.// Pass { network: 'testnet' } to use testnet chains.// Defaults to 'mainnet' if omitted.export const client: NexusClient = createNexusClient({ network: "testnet" });export async function initializeWithProvider(provider: any) { if (!provider) throw new Error("No EIP-1193 provider (e.g. MetaMask) found"); if (client.hasEvmProvider) return; await client.initialize(); // loads deployment info from the middleware await client.setEVMProvider(provider); // attaches the connected wallet}export function destroy() { client.destroy();}export async function getBalancesForBridge() { return await client.getBalancesForBridge();}export async function getBalancesForSwap() { return await client.getBalancesForSwap();}
What each part does
Export
Type
Description
client
NexusClient
The shared Nexus client, created once with createNexusClient().
client.hasEvmProvider
boolean
Getter that returns true once setEVMProvider() has attached a wallet.
initializeWithProvider(provider)
(provider: any) => Promise<void>
Runs client.initialize() then client.setEVMProvider(provider). Must be called after a wallet is connected.
destroy()
() => void
Tears down the client (synchronous).
getBalancesForBridge()
() => Promise<TokenBalance[]>
Returns bridgeable token balances across all supported chains.
getBalancesForSwap()
() => Promise<TokenBalance[]>
Returns swappable token balances across all supported chains.
Two-step initialization
v2 splits startup into two calls: client.initialize() loads deployment data from the middleware, and client.setEVMProvider(provider) attaches the connected wallet. You can call them in either order, but wallet-dependent methods only work once setEVMProvider() has resolved.
Step 3 — Create the UI components
Create four button components in the src/components directory.
connect-button.tsx
Connects an EIP-1193 wallet using the browser's injected provider (window.ethereum).src/components/connect-button.tsx
"use client";export default function ConnectButton({ className,}: { className?: string;}) { const onClick = async () => { const eth = (window as any)?.ethereum; if (!eth) return alert("Install an EIP-1193 wallet (e.g. MetaMask)"); await eth.request?.({ method: "eth_requestAccounts" }); alert("Wallet connected"); }; return ( <button className={className} onClick={onClick}> Connect Wallet </button> );}
init-button.tsx
Initializes the Nexus SDK with the connected wallet's provider.src/components/init-button.tsx
This tutorial uses window.ethereum directly for simplicity. If you use wagmi, RainbowKit, or another wallet library, see Initializing Nexus with RainbowKit for how to retrieve the provider from a connector.
fetch-balances-button.tsx
Fetches the user's balances across all supported chains.src/components/fetch-balances-button.tsx
"use client";import { getBalancesForBridge, getBalancesForSwap, client } from "../lib/nexus";export default function FetchBalancesButton({ className, onResult,}: { className?: string; onResult?: (r: any) => void;}) { const onClick = async () => { if (!client.hasEvmProvider) return alert("Initialize first"); // Use getBalancesForBridge() for bridgeable tokens // Use getBalancesForSwap() for swappable tokens const bridgeBalances = await getBalancesForBridge(); const swapBalances = await getBalancesForSwap(); const res = { bridgeBalances, swapBalances }; onResult?.(res); console.log(res); }; return ( <button className={className} onClick={onClick} disabled={!client.hasEvmProvider} > Fetch Balances </button> );}
de-init-button.tsx
Destroys the client and resets state.src/components/de-init-button.tsx
Connect Wallet — approve the wallet prompt in MetaMask
Initialize Nexus — the SDK connects to the wallet provider
Fetch Balances — a JSON object appears showing your tokens across chains
De-initialize — the SDK resets
What you just built
In a few lines of code you connected to a wallet, initialized the Nexus SDK, and fetched a cross-chain view of every token the user holds. From here you can bridge tokens, execute swaps, or build any custom UI on top of the headless SDK.
Next step
Upgrade your wallet connection experience by replacing window.ethereum with RainbowKit.