Installation
npm i @biconomy/sdk viem @rhinestone/module-sdk
We have created quickstart templates for different environments that you can use to get started:
- Next.js Quickstart - A template using Next.js framework
- Vite Quickstart - A template using Vite + React
- Bun Quickstart - A minimal template using Bun runtime
Each template comes pre-configured with TypeScript, viem
, and @biconomy/sdk
.
Sending your first transaction with the SDK ⚡️
Set up an owner account
First, we need to set up an Owner for the Smart Account which will be used to sign User Operations (transactions) for the Smart Account.
import { privateKeyToAccount } from "viem/accounts";
const privateKey = "PRIVATE_KEY";
const account = privateKeyToAccount(`0x${privateKey}`)
Set up nexus client
A Smart Account needs access to the Network to query for information about its state (e.g. nonce, address, etc). Let's configure a client for the Smart Account.
A bundlerUrl
is required to submit User Operations to the Network, which will initialize the Smart Account.
import { privateKeyToAccount } from "viem/accounts";
import { createSmartAccountClient } from "@biconomy/sdk";
import { baseSepolia } from "viem/chains";
import { http, parseEther } from "viem";
const privateKey = "PRIVATE_KEY";
const account = privateKeyToAccount(`0x${privateKey}`)
const bundlerUrl = "https://bundler.biconomy.io/api/v3/84532/nJPK7B3ru.dd7f7861-190d-41bd-af80-6877f74b8f44";
const nexusClient = await createSmartAccountClient({
signer: account,
chain: baseSepolia,
transport: http(),
bundlerTransport: http(bundlerUrl),
});
const smartAccountAddress = await nexusClient.account.address;
Send User Operation
Next, we'll send a transaction to the Bundler. In the example below, 0 ETH will be transferred from the smart account to a random address. After sending the transaction, we'll use the waitForTransactionReceipt
function to wait for the transaction to be mined and get its receipt. This function takes the transaction hash as a parameter and returns a promise that resolves to the transaction receipt once the transaction is confirmed on the blockchain.
import { privateKeyToAccount } from "viem/accounts";
import { createSmartAccountClient } from "@biconomy/sdk";
import { baseSepolia } from "viem/chains";
import { http, parseEther } from "viem";
const privateKey = "PRIVATE_KEY";
const account = privateKeyToAccount(`0x${privateKey}`)
const bundlerUrl = "https://bundler.biconomy.io/api/v3/84532/nJPK7B3ru.dd7f7861-190d-41bd-af80-6877f74b8f44";
const nexusClient = await createSmartAccountClient({
signer: account,
chain: baseSepolia,
transport: http(),
bundlerTransport: http(bundlerUrl),
});
const smartAccountAddress = await nexusClient.account.address;
const hash = await nexusClient.sendTransaction({
calls: [
{
to: '0xf5715961C550FC497832063a98eA34673ad7C816',
value: parseEther('0'),
},
],
});
const receipt = await nexusClient.waitForTransactionReceipt({ hash });
You can now view the transaction on the Base Sepolia explorer. By submitting this user operation, you've:
- Deployed the counterfactual smart account contract.
- Executed a simple transaction using smart accounts.
Optional: Sponsor transaction
By using a Paymaster, we can add sponsorship of gas fees. Checkout sponsoring transaction for more information.