How to set up a smart account client
Once users are authenticated, you can start landing transactions onchain using a Smart Account Client. Setting up a new account client is easy using the useSmartAccountClient
hook!
This extends viem
's Client, enabling account creation, sending transactions, signing messages, sponsoring gas, and more. It's also EIP-1193 compliant, so you can swap it in for other web3 providers like window.ethereum
.
Usage
The following sets up a Smart Account Client that will allow you to create a new smart account and send transactions signed by your Alchemy Signer.
import { useSmartAccountClient } from "@account-kit/react";
const { client, address, isLoadingClient } = useSmartAccountClient({});
The default smart account type is Modular Account V2 — the most cost-effective and advanced smart account option. You can specify other account types with additional configuration.
Note: React hooks only support Alchemy Signer. If you want to use a 3rd party signer, see this guide.
Configuration Options
You can customize your client by specifying additional parameters to change account type, add gas sponsorship, etc. These parameters are optional.
Account Type
We recommend using ModularAccountV2 as it is the cheapest and most advanced Smart Account, but you can specify other smart contract account types as needed. Learn more about the different smart accounts here.
type
(string) - Defines the smart account type. Options:"ModularAccountV2"
(recommended and default)"LightAccount"
"MultiOwnerLightAccount"
"MultiOwnerModularAccount"
Looking for 7702 support? Learn how to use advanced ModularAccountV2
functionality here.
Gas Sponsorship
To improve transaction UX, we recommend sponsoring gas for users.
policyId
(string) - Gas Manager Policy ID that enables gas sponsorship within policy limits
See the full guide for gas sponsorship.
Advanced options
Options
Usage | Parameter | Type |
---|---|---|
Smart contract account implementation | type | One of "ModularAccountV2" , "LightAccount" , "MultiOwnerLightAccount" , "MultiOwnerModularAccount" |
Sponsor gas for users. Get your policy ID here | policyId | string |
Simulate user operations before sending (i.e. use alchemyUserOperationSimulator ) | useSimulation | boolean |
Custom middleware to run before requesting sponsorship | customMiddleware | ClientMiddlewareFn |
Override fee estimation middleware (if you are using our RPCs, it's important to use the default alchemyFeeEstimator ) | feeEstimator | ClientMiddlewareFn |
Override gas estimation middleware (the default middleware calls the underlying bundler RPC to eth_estimateUserOperationGas ) | gasEstimator | ClientMiddlewareFn |
Override middleware that signs user operation(s) | signUserOperation | ClientMiddlewareFn |
Advanced account params | accountParams | AccountConfig with the following fields:
|