How to programmatically create a wallet
This recipe shows how to programmatically create a smart wallet: you’ll generate a signer, spin up a Smart Wallet Client, read the counterfactual address before deployment and deploy the wallet by sending your first gas sponsored UserOperation
(prepared → signed → sent).
Install required dependencies
You’ll need the following packages for this recipe:
- @account-kit/signer: Server wallet signer and access key generation
- @account-kit/infra: Alchemy transport & chain constants (e.g.,
arbitrumSepolia
) - @account-kit/wallet-client: Smart Wallet Client (prepare/sign/send flows)
- dotenv: Read .env for your API key & policy id
- typescript + tsx + @types/node: Run TypeScript files directly
Generate an access key
Server wallets enable backend applications to programmatically control wallets using access keys, without requiring interactive authentication. This is perfect for automated systems, batch operations, or when you need to sign transactions from your backend.
How server wallets work:
- You generate a secure access key that never leaves your server
- Alchemy derives a public key from your access key for authentication
- The access key is used to sign transactions and messages on behalf of users
- No private keys are stored or transmitted to Alchemy’s servers
Critical: Save your access key securely!
This access key is required to control your server wallet and cannot be recovered if lost. Make sure to store it in a secure location.
Get the counterfactual address
The counterfactual address is the account address associated with the given signer but the account contract hasn’t been deployed yet.
Prepare → sign → send a sponsored call
Use the capabilities pipeline with paymasterService
to sponsor gas via your policy and deploy the account contract by sending a gas sponsored UserOperation.
For non-sponsored path, remove the paymasterService
capability in
prepareCalls
and fund the account to pay gas. The rest of the flow is
unchanged.
Full script (copy-paste)
And when opened in an Arbitrum Sepolia explorer, you should see the deployment, congrats you have just learned how to programmatically create a smart wallet 🎉